Mysql-主从搭建
1. 准备两台虚拟机 centos1, centos2 (centos2 直接拷贝centos1),mysql版本要一样(5.5, 5.6)
2. 查询ip, cent1 ip:10.211.55.4 cent2 ip:10.211.55.6
--------------------------------------
3. 配置主库
A. 授权给从数据库服务器
grant all privileges on DB1.* to 'User'@'128.82.123.%' Identified by '123456';
grant all privileges on DB1.* to 'User'@'127.0.0.1' Identified by '123456';
grant all privileges on DB2.* to 'Usr'@'128.82.123.%' Identified by '123456';
grant all privileges on DB2.* to 'Usr'@'127.0.0.1' Identified by '123456';
GRANT REPLICATION SLAVE ON *.* TO 'centosslave'@'10.211.55.6' IDENTIFIED BY '123456';
检查两台机器的iptables是否把端口打开了,否则链接失败。
2. 查询ip, cent1 ip:10.211.55.4 cent2 ip:10.211.55.6
--------------------------------------
3. 配置主库
A. 授权给从数据库服务器
grant all privileges on DB1.* to 'User'@'128.82.123.%' Identified by '123456';
grant all privileges on DB1.* to 'User'@'127.0.0.1' Identified by '123456';
grant all privileges on DB2.* to 'Usr'@'128.82.123.%' Identified by '123456';
grant all privileges on DB2.* to 'Usr'@'127.0.0.1' Identified by '123456';
GRANT REPLICATION SLAVE ON *.* TO 'centosslave'@'10.211.55.6' IDENTIFIED BY '123456';
flush privileges;
DB1, DB2的语句可以不跑
DB1, DB2的语句可以不跑
B. 修改主库配置文件
vim /etc/my.cnf
server-id=1 1随意指定,不重复即可
C. 重启mysql
service mysql restart
D. 查看主服务器当前二进制日志名和偏移量 (配置从服务器需要用到)
show master status;
----------------------------------------
----------------------------------------
4. 从库配置
vim /etc/my.cnf
server-id=2 2随意指定,不重复即可然后重启mysql
service mysql restart
A. 编辑配置文件
A. 编辑配置文件
vim /etc/my.cnf (under 5.5)
server-id=2
master-host=10.211.55.4
master-user= centosslave
master-password=123456
master-port=3306
------------------------ (under 5.5)
(大于5.5+)
CHANGE MASTER TO MASTER_HOST='10.211.55.4', MASTER_PORT=3306, MASTER_USER='centosslave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000009', MASTER_LOG_POS=107;
B. slave start; / start slave;
C. show slave status\G;
查看slave的状态,如果下面两项值为YES,则表示配置正确:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
检查两台机器的iptables是否把端口打开了,否则链接失败。
评论
发表评论