环境:
http://blog.youkuaiyun.com/convict_eva/article/details/51729861
2、主库配置
2.1、修改/etc/my.cnf,并重启服务器
[mysqld]
server-id=1
log-bin=MySql-bin
binlog-do-db=test_db
#binlog-do-db=portal #需要备份数据,多个写多行 (没有就备份所有的数据库)
#binlog-ignore-db=mysql #不需要备份的数据库,多个写多行)
2.2、创建用于复制的用户
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'replPassword&';
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repl'@'%' identified by 'replPassword&';
mysql> FLUSH PRIVILEGES;
2.3、查看master 二进制文件位置
mysql> FLUSH TABLES WITH READ LOCK;
mysql>mysql> show master status;
+--------------------+----------+---------------+---------------------+-------------------+
| File | Position | Binlog_Do_DB| Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+---------------+---------------------+-------------------+
| MySql-bin.000005 | 154 | test_db | | |
+--------------------+----------+---------------+---------------------+-------------------+
二进制文件和位置,代表了从库从主库复制数据的坐标,在配置从库的时候要使用
2.4、导出主库要做备份的数据
#mysqldump -uroot -p -B test_db --master-data > dbdump.db
3、从库配置
3.1、my.cnf 添加:
server-id=2 #这个是必须的
log-bin=mysql-bin
#relay-log=/var/lib/mysql/mysql-relay-bin 指定中继日志地址
#log-slave-updates=1 允许备库将重放的事件也记录到自己的二进制日志中
#read-only 阻止任何没有特权权限的线程修改数据
重启数据库服务
3.2、导入刚从主库导出的数据
#mysql -uroot -p < dbdump.db
3.3、连接mysql,配置从库的主库信息
mysql>CHANGE MASTER TO MASTER_HOST='192.168.192.128',MASTER_USER='repl', MASTER_PASSWORD='replPassword&', MASTER_LOG_FILE='MySql-bin.000005', MASTER_LOG_POS=154 ;
mysql> start slave;
(CHANGE MASTER TO 参考:http://dev.mysql.com/doc/refman/5.7/en/change-master-to.html)
3.4、查看从库状态:
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.192.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySql-bin.000005
Read_Master_Log_Pos: 858
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 1024
Relay_Master_Log_File: MySql-bin.000005
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: 858
Relay_Log_Space: 1235
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: ef323f37-28dc-11e6-a103-000c29103532
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:
配置成功。
4、添加从库和配置从库一样,只是同步数据的坐标和从主库导入的数据不同而已。
问题:
用于复制的用户,没有授权。