您的位置:首页 > 数据库 > MySQL

一步一步教你配置 MySQL 5.6 主从同步

2016-07-02 19:04 543 查看
主库IP:192.168.56.111  主机名:ace

备库IP:192.168.56.123 主机名:luffy

MySQL版本:5.6

1、确保主库的binlog打开,确保主从库的server-id不相同

[root@ace mysql]# egrep 'log-bin|server-id' < /etc/my.cnf
log-bin=/var/lib/mysql/binlog
server-id=1
[root@luffy ~]# egrep 'log-bin|server-id' < /etc/my.cnf
log-bin=/var/lib/mysql/binlog
server-id=2

注:我这里备库的binlog也打开了,如果不做链式同步的话备库binlog可不必打开。

2、在主库创建备库复制时使用的用户

mysql> create user 'rep'@'%' identified by 'rep';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'rep'@'%';
Query OK, 0 rows affected (0.00 sec)
在备库进行登陆测试

[root@luffy ~]# mysql -urep -prep -h192.168.56.111
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.6.30-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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>
登陆成功。

3、主库锁表后进行全备

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test1.aaa values (123123);
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
锁表成功

然后备份所有数据库,并加--master-data=1,该参数可在备份文件中生成当前精确的position,备库change master to ... 时可省些事。

[root@ace mysql]# mysqldump -A -B --master-data=1 | gzip > /root/alldb.sql.gz


4、对主库表解锁,并增加部分数据 以 测试备库能否追加备份完成时间点和同步开始时间点之间更新的数据

[root@ace ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 61
Server version: 5.6.30-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its<pre name="code" class="sql">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> use test1;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql>
select * from aaa;+--------+| id |+--------+| 1 || 2 || 3 || 11 || 22 || 33 || 123 || 234 || 123123 |+--------+9 rows in set (0.00 sec)

主库test1库下aaa表中的数据,此时备份文件中完全包括这部分数据,接下来对改变追加数据。

mysql> insert into test1.aaa values (2222);
Query OK, 1 row affected (0.00 sec)

mysql> select * from aaa;
+--------+
| id     |
+--------+
|      1 |
|      2 |
|      3 |
|     11 |
|     22 |
|     33 |
|    123 |
|    234 |
| 123123 |
|   2222 |
+--------+
10 rows in set (0.00 sec)


此时2222这条数据是备份集中不存在的。

4、将备份传输到备库进行恢复

[root@ace ~]# scp alldb.sql.gz luffy:/root/
root@sabo's password:
alldb.sql.gz                                                                                       100%   59MB  59.2MB/s   00:01
[root@ace ~]#

传输完成

在备库change master to ...  并恢复数据

mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.56.111',
-> MASTER_PORT=3306,
-> MASTER_USER='rep',
-> MASTER_PASSWORD='rep';
Query OK, 0 rows affected, 2 warnings (0.03 sec)


[root@luffy ~]# gunzip alldb.sql.gz
[root@luffy ~]# ls
alldb.dmp        Desktop             MySQL-client-5.6.30-1.rhel5.x86_64.rpm  printABCThread    sqrtThread.c  test.c~
alldb.sql        install.log         MySQL-server-5.6.30-1.rhel5.x86_64.rpm  printABCThread.c  test
anaconda-ks.cfg  install.log.syslog  mysql.sql                               sqrtThread        test1.dmp
[root@luffy ~]# mysql < alldb.sql


可发现当前备库aaa表中无2222那条记录

mysql> select * from aaa;
+--------+
| id     |
+--------+
|      1 |
|      2 |
|      3 |
|     11 |
|     22 |
|     33 |
|    123 |
|    234 |
| 123123 |
+--------+
9 rows in set (0.00 sec)

5、开启同步

在开启之前先查看下备库状态

mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.56.111
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000036
Read_Master_Log_Pos: 1653511
Relay_Log_File: luffy-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: binlog.000036
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1653511
Relay_Log_Space: 120
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
可以看到Slave_IO_Running 和 Slave_SQL_Running 均为 No, 表明同步未开启

现在开启同步

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.111
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000036
Read_Master_Log_Pos: 1653734
Relay_Log_File: luffy-relay-bin.000002
Relay_Log_Pos: 503
Relay_Master_Log_File: binlog.000036
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1653734
Relay_Log_Space: 676
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 59a2db90-21a2-11e6-ae2e-080027dc3ab7
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)


同步一开启,最后验证下test1的aaa表

[root@luffy ~]# mysql -e 'select * from test1.aaa'
+--------+
| id     |
+--------+
|      1 |
|      2 |
|      3 |
|     11 |
|     22 |
|     33 |
|    123 |
|    234 |
| 123123 |
|   2222 |
+--------+


2222那条记录也已经同步完成。

6、同步状态检查(不再赘述)

至此主从复制配置完成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息