Mysql的主从复制和级联
记录一下
mysql的主主复制和级联复制。
一、环境
主机:192.25.10.71
主机:192.25.10.73
从机:192.25.10.76
DB:Mysql 5.5.31
OS:CentOS 6.3
二、架构
三、实施步骤
mysql的安装略
1.master端创建测试DB
[root@proxy1 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1959 Server version: 5.5.31-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database db_kenyon; Query OK, 1 row affected (0.03 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db_kenyon | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> use db_kenyon; Database changed mysql> create table t_kenyon(id int); Query OK, 0 rows affected (0.07 sec) mysql> insert into t_kenyon values(1),(2),(3); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from t_kenyon; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+ 3 rows in set (0.00 sec) mysql> show master status; Empty set (0.00 sec) [root@localhost ~]# 2.修改配置/etc/my.cnf [mysqld] log-bin=mysql-bin server-id =1 binlog-do-db=db_kenyon innodb_data_home_dir = /usr/local/mysql/data/ innodb_log_group_home_dir = /usr/local/mysql/data/ innodb_buffer_pool_size = 256M innodb_additional_mem_pool_size = 20M innodb_log_file_size = 64M innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 50 3.创建slave端连到master端的用户,重启master [root@localhost ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL............ SUCCESS! [root@localhost ~]# mysql mysql>grant replication slave on *.* to repl@'192.25.10.73' identified by '123456'; mysql> flush privileges; [root@localhost ~]# 4.备份master端数据,并拷贝至slave端 mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +------------------+----------+---------------+--------------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+---------------+--------------------------------------------------+-------------------+ | mysql-bin.000002 | 120 | db_kenyon | | | +------------------+----------+---------------+--------------------------------------------------+-------------------+ 1 row in set (0.00 sec) mysql> 另外一个session: [root@localhost log]# cd /usr/local/mysql/data [root@localhost data]# tar -zcvf backup.tar.gz db_kenyon [root@localhost ]# scp backup.tar.gz root@192.25.10.73:/usr/local/mysql/data/ --回到之前的session,从机搭完以后解锁,保证数据一致 mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) 5.修改slave端的数据 [mysqld] server-id = 2 master-host = 192.25.10.71 #5.5以上废弃 master-user = repl #5.5以上废弃 master-password = 123456 #5.5以上废弃 master-port = 3306 #5.5以上废弃 master-connect-retry=60 #重试时间60秒, #5.5以上废弃 replicat