A:主库 192.168.0.3
修改my.cnf
server_id = 1
log_bin = /usr/local/mysql/binlog/mysql-bin
binlog_ignore_db=mysql
binlog_ignore_db=information_schema
binlog_ignore_db=performance_schema
添加复制用户:
create user 'ruser'@'%' identified by'ruserpwd';
grant replication slave on *.* to'ruser'@'%';
flush privileges;
导出主库快照并传到从库的服务器上:
Mysqldump -uroot -p -B db mytest --master-data=1> master-dump.db
注:-B表示备份多个库,这里db和mytest
mysqldump -uroot -p db --master-data=1 >master-dump.db
注:
master-dump=1时候,CHANGEMASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=208;
master-dump=2时候,-- CHANGEMASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=208; 前面注释符。
B:从库 192.168.0.135
修改my.cnf
sever-id = 2
配置主从关系:
mysql>change master tomaster_host='192.168.0.3',master_port=3306,
master_user='ruser',master_password='ruserpwd';
导入主库快照数据:
[root@localhost mysql]# mysql -uroot -p db< master-dump.db
注:这里先上传master-dump.db到/usr/local/mysql目录下。
启动主从复制:
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
查看主从同步状态:
mysql> show slave status \G
回显部分如下,则正常
*************************** 1. row***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.0.3
Master_User: ruser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 208
Relay_Log_File:localhost-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //取日志的IO进程正常
Slave_SQL_Running: Yes //执行SQL的进程正常
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: 208
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 //延迟为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:9aa88666-9294-11e6-8eaf-000c2995d5fb
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 theslave 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)