安装环境
操作系统 :CentOS 6.5
数据库版本:MySQL 5.6.27
主机A:192.168.1.1 (Master)
主机B:192.168.1.2 (Slave)
Master的配置
在Linux环境下MySQL的配置文件的位置是在 /etc/my.cnf ,在该文件下指定Master的配置如下:
log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
binlog-do-db=ufind_db
这里的server-id用于标识唯一的数据库,这里设置为2,在设置从库的时候就需要设置为其他值。
binlog-ignore-db:表示同步的时候ignore的数据库
binlog-do-db:指定需要同步的数据库
1、然后重启mysql:service mysqld restart
2、进入mysql:[root@VM_221_4_centos ~]# mysql -u root -p 回车,输入mysql密码进入。
3、 赋予从库权限帐号,允许用户在主库上读取日志,赋予192.168.1.2也就是Slave机器有File权限,只赋予Slave机器有File权限还不行,还要给它REPLICATION SLAVE的权限才可以。
在Master数据库命令行中输入:
>GRANT FILE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';
>GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';
>FLUSH PRIVILEGES
4、重启mysql,登录mysql,显示主库信息
mysql> show master status;
1
1
mysql> show master status;
+------------------+----------+--------------+----------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
| mysql-bin.000004 | 28125 | ufind_db | information_schema,cluster,mysql | |
+------------------+----------+--------------+----------------------------------+-------------------+
1 row in set (0.00 sec)
Slave的配置
1、从库的配置,首先也是修改配置文件:/etc/my.cnf 如下:
log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=ufind_db
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
2、修改完/etc/my.cnf 文件之后,重启一下MySQL(service mysqld restart)
进入Slave mysql控制台,执行:
这里写图片描述
mysql> stop slave; #关闭Slave
mysql> change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000004', master_log_pos=28125;
mysql> start slave; #开启Slave
在这里指定Master的信息,master_log_file是在配置Master的时候的File选项, master_log_pos是在配置Master的Position 选项,这里要进行对应。
然后可以通过mysql> show slave status; 查看配置的信息:
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.167.1.1
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 28125
Relay_Log_File: VM_128_194_centos-relay-bin.000004
Relay_Log_Pos: 26111
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: ufind_db
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: 28125
Relay_Log_Space: 26296
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: 2
Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd
Master_Info_File: /data/mysqldb/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)
(l)、 Master_Log_File,Read_Master_Log_Pos 记录了IO thread读到的当前master binlog文 件和位置, 对应master的binlog文件和位置。
(2)、 Relay_Log_File,Relay_Log_Pos记录了SQL thread执行到relay log的那个文件和位置,对应的是slave上的relay log文件和位置。
(3)、 Relay_Master_Log_File,Exec_Master_Log_Pos记录的是SQL thread执行到master binlog的文件和位置,对应的master上binlog的文件和位置。
可以看到,已经配置成功。
------------------------------------------------------------------------------------------------------------------------------------------------------
主从不一致处理:
stop slave;
set global sql_slave_skip_counter =1;
start slave;
------------------------------------------------------------------------------------------------------------------------------------------------------
主从不一致处理:
1.先进入主库,进行锁表,防止数据写入
mysql> flush tables with read lock;
2.进行数据备份
[root@server01 mysql]#mysqldump -uroot -p test > test.sql
3.查看master 状态
mysql> show master status;
+-------------------+----------+--------------+-------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+-------------------------------+
| mysqld-bin.000001 | 3260 | | mysql,test,information_schema |
+-------------------+----------+--------------+-------------------------------+
1 row in set (0.00 sec)
4.把mysql备份文件传到从库机器,进行数据恢复
#使用scp命令
[root@server01 mysql]# scp test.sql [email protected]:/tmp/
5.停止从库的状态
mysql> stop slave;
6.然后到从库执行mysql命令,导入数据备份
mysql> source /tmp/test.sql
7.设置从库同步,注意该处的同步点,就是主库show master status信息里的| File| Position两项
change master to master_host = '192.168.128.100', master_user = 'rsync', master_port=3306, master_password='', master_log_file = 'mysqld-bin.000001', master_log_pos=3260;
8.重新开启从同步
mysql> start slave;
9.查看同步状态
mysql> show slave status\G 查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
好了,同步完成啦。