今天教大家mysql主从配置。不知道为什么,非常喜欢docker,就用docker安装centos6容器进行模拟。
一台mysqlmaster,ip:172.17.0.2,
一台msyqlslave,ip:172.17.0.3
分别装好mysql5.6如果安装遇到问题,请查看看http://blog.csdn.net/u013091013/article/details/51943372。最好mysql的版本一样,没试过不一样版本的。
注意点:首先从服务器要先备份一下主服务器,因为配置主从之后,主服务器上的原始数据不会更新到从服务器。
1、master主服务器配置
修改my.cnf
vi /usr/local/mysql/my.cnf
[mysqld]
log-bin=mysql-bin //二进制文件日志
server-id=2 //服务器ID,唯一
重启服务
service mysql restart
2、slave从服务器配置
修改my.cnf
vi /usr/local/mysql/my.cnf
[mysqld]
log-bin=mysql-bin //二进制文件日志
server-id=3 //服务器ID,唯一
重启服务
service mysql restart
3、主服务器创建帐号用于授权slave
mysql -uroot -p
grant replication slave on *.* to 'backup'@'%' identified by '123456'; //表示任意客户端都可以连接,此处可用具体客户端IP代替,如172.17.0.3,加强安全。
4、查询主服务器mysql状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
纪录File和Position,配置从服务器时要用。
5、配置从服务器复制
change master to master_host='172.17.0.2',master_user='backup',master_password='123456', master_log_file='mysql-bin.000004',master_log_pos=120;
start slave;
6、检查是否配置成功
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.2
Master_User: mysync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 120
Relay_Log_File: 78fa757b2071-relay-bin.000004
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000004
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: 626
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: ca270285-4cc3-11e6-876e-0242ac110003
Master_Info_File: /usr/local/mysql/data/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)
7、测试
在主服务器上创建一张表.
mysql> create table user(ID int(11));
Query OK, 0 rows affected (0.14 sec)
mysql> show tables;
+-----------------+
| Tables_in_admin |
+-----------------+
| user |
+-----------------+
1 row in set (0.00 sec)
查看从服务器
mysql> show tables;
+-----------------+
| Tables_in_admin |
+-----------------+
| user |
+-----------------+
1 row in set (0.00 sec)
成功
常见问题: