建议在主从两台服务器上安装相同版本的mysql版本,如果不是同版本,会因为某些版本差异,导致数据出现异常情况.
主库:
1.授权 grant replication slave on *.* to user@'ip' identified by 'password';
2.修改主数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改之后必须重启Mysql服务
[mysqld]
log-bin = /home/mysql/log/mysql-bin.log
server-id=1
3.备份主库数据: mysqldump -uroot -proot --all-databases --single-transaction --master-data=2 >all_database.sql
--single-transaction 是表示提交所有事务后的数据
--master-data 提取备份点的主库二进制日志和偏移量,在从库启动slave前需要这个数据.=1表示自动执行,=2是注释掉的,需要手工执行
备库:
1.修改从库my.cnf,
[mysqld]
log-bin = /home/mysql/log/mysql-bin.log
server-id=2
2.重启mysql,然后还原数据库
3.更改从库读取主库的日志和偏移量(这些数据是由主库备份的时候又master=2从来的)
mysql>change master to master_host = '192.168.0.199', master_port = 3306, master_user = 'repl12', master_password = '29uin2%2#
4.启动同步
start slave;
5.验证同步情况:show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.199
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000308
Read_Master_Log_Pos: 424729182
Relay_Log_File: db2-relay-bin.000079
Relay_Log_Pos: 424721986
Relay_Master_Log_File: mysql-bin.000308
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: 424721840
Relay_Log_Space: 424729528
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: 199
1 row in set (0.00 sec)
注意上面红色标示部分:Slave_IO_Running负责与主库通讯,也就是将主库变更的二进制日志复制过来的功能;Slave_SQL_Running负责写入从库数据库
Seconds_Behind_Master 表示从库的数据状态与主库的差异时间,
Master_SSL_Verify_Server_Cert 如果从库同步有错,会在这行显示,没有错误就显示:no
阅读(778) | 评论(0) | 转发(0) |