说明:
此文章是借鉴好朋友的文档 http://sunys.blog.51cto.com/8368410/1639820
环境:
mysql的版本:mysql-5.6.22
系统:centos6.4
master:192.168.186.129
slave:192.168.186.128
vip: 192.168.186.140 #虚拟ip,web服务器连接的ip
步骤:
配置mysql的单向主从:
注意:mysql的安装步骤省略
master: #开启二进制日志文件
vi /usr/local/mysql/my.cnf
在[mysqld]下添加:
server-id = 1
log-bin = /opt/mysql/binlog/mysql-binlog #二进制日志文件路径随便设置,最好单独放在一个目录下
slave:
vi /usr/local/mysql/my.cnf
在[mysqld]下添加:
server-id = 2
master: #对slave授权
mysql -uroot -p123456
grant replication slave on *.* to ‘slave‘@‘192.168.186.128‘ identified by ‘123456‘;
flush privileges;
master: #备份数据
mysql -uroot -p123456
flush tables with read lock; #锁表
show master status; #查看binlog位置和pos值,这个要记录下来
+---------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| mysql-binlog.000044 | 412 | | | |
+---------------------+----------+--------------+------------------+-------------------+
打开另外一个终端:mysqldump -u root -p123456 --all-databases > /tmp/mysqldump.sql
回到之前终端:unlock tables; #解表
注意:锁表--查看--备份--解表 顺序要注意
scp /tmp/mysqldump.sql 192.168.186.128:/tmp/ #把备份数据拷到slave
slave: #导入数据
mysql -uroot -p123456 < /tmp/mysqldump.sql
slave: #开始同步
mysql -uroot -p123456
change master to master_host=‘192.168.186.129‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘mysql-binlog.000044‘,master_log_pos=412,master_port=3306;
start slave;
show slave status\G; #查看是否成功,如下:出现两个Yes则成功
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.186.129
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000044
Read_Master_Log_Pos: 412
Relay_Log_File: scj-relay-bin.000002
Relay_Log_Pos: 286
Relay_Master_Log_File: mysql-binlog.000044
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
注意:主从同步,不能在从机进行任何写操作,不然主从就会断掉,所有写操作都是在主上进行
若从机出现问题(如突然关机),正常后会继续同步主机数据
配置mysql的双向主从: