Mysql 5.7 版本主从复制mysql
1 [mysqld] 2 log-bin=mysql-bin 3 server-id=1 4 binlog-ignore-db=information_schema 5 binlog-ignore-db=performance_schema 6 binlog-ignore-db=sys 7 binlog-ignore-db=mysql 8 binlog-do-db=test
其中 log-bin 是日志类型(前缀)
server-id=1 是用于标识惟一的数据库,在从库必须设置为不一样的值
binlog-ignore-db 表示同步的时候忽略的数据库
binlog-do-db 指定须要同步的数据库
sql
sudo service mysql restart
只赋予Slave机器有File权限还不行,还要给它REPLICATION SLAVE的权限才能够。数据库
1 grant FILE on *.* to 'root'@'192.168.33.11' identified by 'root'; 2 grant replication slave on *.* to 'root'@'192.168.33.11' identified by 'root'; 3 flush privileges;
注: 这里的 root 是同步的时候从库使用的用户。能够本身建立新的 user 和 权限, 这里再也不赘述ubuntu
sudo service mysql restart
show master status\G;
若是出现如下信息 说明配置正确服务器
show master status\G; *************************** 1. row *************************** File: mysql-bin.000003 Position: 154 Binlog_Do_DB: test Binlog_Ignore_DB: information_schema,performance_schema,mysql,sys Executed_Gtid_Set: 1 row in set (0.00 sec)
其中须要记录的有: File 表明日志文件名
Position 表明主从复制开始的位置ide
配置从库 slave测试
修改配置文件 /etc/mysql/mysql.conf.d/mysql.cnf (根据本身安装的路径 如: /etc/my.cnf)spa
1 log-bin=mysql-bin 2 server-id=2 3 binlog-ignore-db=information_schema 4 binlog-ignore-db=performance_schema 5 binlog-ignore-db=mysql 6 replicate-do-db=test 7 replicate-ignore-db=mysql 8 log-slave-updates 9 slave-skip-errors=all 10 slave-net-timeout=60
stop slave; change master to master_host='192.168.33.10',master_user='root',master_password='root',master_log_file='mysql-bin.000003', master_log_pos=154; start slave;
其中: master_log_file 是 master 记录的 File 字段
master_log_pos 是 master 记录的 Position 字段vagrant
查看从库信息debug
show slave status\G
若是出现如下信息 说明配置正确
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.33.10 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 2602 Relay_Log_File: vagrant-ubuntu-trusty-64-relay-bin.000003 Relay_Log_Pos: 2768 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test Replicate_Ignore_DB: mysql 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: 2602 Relay_Log_Space: 2994 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: fe3645a3-d3d1-11e7-94d1-08002784ba04 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 more updates 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 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
其中 Slave_IO_Running 和 Slave_SQL_Running 都为 yes 表示正常
Seconds_Behind_Master 表示 主从延迟
值为 0 : 表示正常 没有延迟
正数: 表示 出现延迟
负数 : 理论不可能出现的值 (询问 DBA 后 , 有些状况会出现,可是这个属性的值不能够赋值为负数)
tail /var/log/mysql/error.log
若是出现如下 1593 错误
[ERROR] Slave I/O for channel '': Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593
是由于两台数据库的UUid 相同 缘由是 复制虚拟机 致使文件UUid相同
cd /var/lib/mysql mv auto.cnf auto.cnf.bk
若是不知道 auto.cnf 文件在哪里 能够进入Mysql 输入
show variables like 'datadir';
会获得
+---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | datadir | /var/lib/mysql/ | +---------------+-----------------+ 1 row in set (0.01 sec)
其中 /var/lib/mysql/ 就是 auto.cnf 的位置