Master:192.168.1.1
Slave:192.168.1.2
Master端:
vi /etc/my.cnf
server-id = 1 #master端ID号
binlog-ignore-db = mysql #不同步mysql库
binlog-ignore-db = test #同上
binlog-ignore-db = information_schema #同上
/usr/local/mysql/bin/mysql -uroot -p
mysql>grant replication slave on *.* to slave@’192.168.1.2′ identified by ‘123456’;
mysql>flush privileges;
mysql>flush tables with read lock; #只读锁表
mysql>show master status; #获取并记录下当前日志file和position的值,稍后Slave端需要用到
File | Position |
+——————+———-
| mysql-bin.000001 | 291392712
另开一个终端窗口,然后执行如下的操作:
tar zcvf /usr/local/mysql/var.tgz /usr/local/mysql/var //将var目录打包
scp /usr/local/mysql/var.tgz root@192.168.1.2:/usr/local/mysql/var.tgz //将文件包传至Slave端
完成后回到前一个终端窗口解锁只读。
mysql>unlock tables;
Slave端:
vi /etc/my.cnf
server-id = 2 #Slave的ID号,此处不能与到Master端的ID重复。
保存退出。
service mysqld stop 或 /usr/local/mysql/bin/mysqladmin -uroot -p shutdown
tar zxvf /usr/local/mysql/var.tgz /usr/local/mysql/var
chown -R mysql:mysql /usr/local/mysql/var
service mysqld start
/usr/local/mysql/bin/mysql -uroot -p
mysql>stop slave;
mysql>change master to
>master_host=’192.168.1.1′,
>master_user=’slave’,
>master_password=’123456′,
>master_log_file=’mysql-bin.000001′, 此前记录下的Master端file值
>master_log_pos=291392712; 此前记录下的Master端position值
mysql>start slave;
mysql>show slave status \G
查看上面命令返回的结果,如果Slave_IO_Running: Yes 和Slave_SQL_Running: Yes 2个均为yes,则证明主从同步正常,如果任一个显示NO,则证明同步有问题。