1. 程式人生 > >mysql 簡單主從

mysql 簡單主從

.cn 虛擬機克隆 sql stop keyword ttr mysqld nlog mysq

主服務器master

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin   #必須開啟log-bin
server-id=129       #服務器ID

從服務器slave

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin   #開啟log-bin(可選)
server-id=129       #服務器ID
[root@localhost ~]# systemctl restart mysqld  #主從服務器

mysql> GRANT REPLICATION SLAVE ON *.* to ‘mysync‘@‘%‘ identified by ‘123456‘;  #主服務器授權賬號
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      439 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

#查看master狀態
mysql> stop slave;
mysql>change master to master_host=‘192.168.100.129‘,master_user=‘mysync‘,master_password=‘123456‘, master_log_file=‘mysql-bin.000004‘,master_log_pos=308;
mysql> stop slave;
mysql> show slave status\G  #查看slave狀態
   Slave_IO_Running: Yes  #必須為Yes
   Slave_SQL_Running: Yes #必須為Yes

如果在master 也配置 slave 架構就成了主主模式 需要註意數據一致性的問題

虛擬機克隆需要重新生成auto.cnf

[root@localhost ~]# mv /var/lib/mysql/auto.cnf /tmp
[root@localhost ~]# systemctl restart mysqld

mysql 簡單主從