log-bin=mysql-bin server-id=2 #从服务器ID binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=mysql replicate-do-db=hr #同步的数据库名 replicate-ignore-db=mysql #屏蔽不同步的数据库 log-slave-updates slave-skip-errors=all slave-net-timeout=60
注:MySQL 5.6之后没有master-user等参数,否则重启报错
(3)重启服务器# service mysqld restart
五、数据库备份、同步1、对数据库进行备份
[root@master mysql]# /usr/local/mysql/bin/mysqldump -h localhost -uroot -proot --all-databases --lock-all-tables >/tmp/dbdump.sql [root@master mysql]# scp /tmp/dbdump.sql root@slave:/tmp/ [root@slave ~]# mysql -u root -p < /tmp/dbdump.sql
注:从服务器不需要创建要同步的数据库,备份脚本会自动创建。
2、配置从服务器连接主服务器同步
(1)、获取主服务器二进制日志信息
[root@master mysql]# mysql -uroot -p Enter password: mysql> flush tables with read lock; --只读锁定 mysql> show master status; mysql> unlock tables; --解除锁定
(2)设置从服务器同步
[root@slave ~]# mysql -u root –p mysql> stop slaves; mysql> change master to -> master_host='192.168.152.129', -> master_user='slave_cp', -> master_password='root', -> master_log_file='mysql-bin.000003', -> master_log_pos=195; --这里的参数取值参考查询到的master日志信息 mysql> start slave; mysql> show slave status \G
3、同步验证
在master上添加数据,查看slave端的同步情况。
master节点:
[root@master ~]# mysql -uroot -p mysql> create database test; mysql> use test; mysql> create table users(id int primary key,name char(20)); mysql> insert into users(id,name)values(1,'aa'); mysql> use hr; mysql> show tables; mysql> create table bb(id int,name char(20)); mysql> insert into emp(id,name)values(2,'bb');
slave节点:
[root@slave ~]# mysql -uroot -p mysql> select * from test.users;
错误
1、启动从服务器失败
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository
解决:
mysql> reset slave; mysql> change master to master_host='192.168.152.129', master_user='slave_cp',master_password='root',master_log_file='mysql-bin.000005', master_log_pos=610; mysql> start slave;