一. 架构图
软件版本:Linux、Mysql Server version: 5.6.15
二.操作过程
2.1 主从机各自配置my.cnf文件,此文件默认位置在/etc/my.cnf,无的可以直接从/usr/share/mysql/my-default.cnf复制过来并改名就可以了。文件后加入以下内容:
log-bin=mysql-bin //[必须]启用二进制日志 server-id=222 //[必须]服务器唯一ID,默认是1,一般取IP最后一段,主从机ID不能一样 replicate-do-db = test //[可选] 指定复制的数据库 replicate-ignore-db = mysql,information_schema //[可选]指定不复制的数据库 slave-skip-errors //[可选]在复制过程中,由于各种原因导致binlog中的sql出错,默认情况下,从库会停止复制,要用户介入。
2.2 重启mysql
***************************************主机操作******************************************
2.3 在主机上创建master用户,从机创建slave用户,注意,master用户需要有赋予slave复制的权利
2.4 登录master用户
GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by 'jan';
flush privileges;
2.5 停止对数据库的DDL和DML操作或lock起所有的数据库表,记录当前master的状态
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000005 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
***************************************从机操作******************************************
2.6 执行以下语句指定master的IP,日志,日志记录位置信息,并启动slave
mysql> stop slave; Query OK, 0 rows affected (0.03 sec) mysql> change master to -> master_host='192.168.232.128', -> master_user='master', -> master_password='jan', -> master_log_file='mysql-bin.000005', -> master_log_pos=120; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec)
2.7 查看slave状态
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.232.128 Master_User: master Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 120 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes //此状态必须为YES Slave_SQL_Running: Yes //此状态必须为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: 120 Relay_Log_Space: 460 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: 2b913f38-8dd4-11e5-aa5d-000c29271917 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)
从上述信息可知已经成功搭建主从库并联通了,但在搭建过程中遇到一些错误:
Slave_IO_Running:Yes
Slave_SQL_Running:No
解决方法:master用户没有配置权限给slave用户,授权即可
若还是无法解决问题,可查找binlog日志
show binlog event in 'master-bin.000005'\G ;
三.测试
3.1 创建数据库
master:
mysql> create database testdb; Query OK, 1 row affected (0.00 sec)
slave:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | testdb | +--------------------+ 4 rows in set (0.00 sec)
3.2 更新数据
master:
mysql> create table a(id int(10),name varchar(20)); Query OK, 0 rows affected (0.02 sec) mysql> insert into a values(1,'jan'),(2,'lulu'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0
slave:
mysql> select * from a; +------+------+ | id | name | +------+------+ | 1 | jan | | 2 | lulu | +------+------+ 2 rows in set (0.01 sec)