环境:
主从:mysql 5.6.10
centos 6.5
步骤:
主从库创建复制账号:
建议主从库复制账号密码一模一样。
grant all privileges on *.* to '$username'@'%' identified by '$password' with grant option;
flush privileges;
将主从(3307主----3309从)切换
主库上:
前提:3307正常
一、将3307设为只读。命令行操作
# 修改配置文件
vim /home/bbq/mysql/mysql-3307/cnf/my.cnf
# 在[mysqld]中增加
read_only
主库上
# 重启3307
service mysqld3307 restart
二、等待从库执行完主库的所有sql。mysql客户端操作
主库上:
# 3307执行:
show master status # 记录File、Position
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 151 |
从库上:
# 3309执行:
select master_pos_wait(File, Position);
mysql> select master_pos_wait('master-bin.000004',446);
+------------------------------------------+
| master_pos_wait('master-bin.000004',446) |
+------------------------------------------+
| 0 |
+------------------------------------------+
从库上
三、将3309设为可写。命令行操作
# 修改配置文件
vim /home/bbq/mysql/mysql-3309/cnf/my.cnf
# 在[mysqld]中删除
# read_only
# 重新启动mysqld3309
service mysqld3309 restart
从库上:
四、将3307设为3309的从库。mysql客户端操作
# 3309 从库变主库
RESET MASTER;
STOP SLAVE;
RESET SLAVE;
show master status; #记录FILE Position
主库上:
# 3307 主库变从库
RESET MASTER;
STOP SLAVE;
RESET SLAVE;
CHANGE MASTER TO master_host='localhost',master_port=3309, master_user='复制账号',master_password='复制账号密码', master_log_file='新主库FILE',master_log_pos=新主库Position;
选填:
上面如报错,(5.6开启gtid)请执行如下:
CHANGE MASTER TO
MASTER_HOST='192.168.199.151',
MASTER_USER='复制账号',
MASTER_PASSWORD='复制账号密码',
MASTER_AUTO_POSITION = 1;
START SLAVE;
SELECT SLEEP(1);
SHOW SLAVE STATUS\G;
若是SQL线程(Slave_IO_Running)
I/O线程(Slave_SQL_Running)都显示为YES状态,则切换成功.
参考:
http://babaoqi.iteye.com/blog/1954471
mysql主从搭建:
主从:
http://blog.csdn.net/u010098331/article/details/50679807
一主多从:
http://blog.csdn.net/u010098331/article/details/50802882(已纠正)