一、环境
主机: master操作系统:centos 7 64位
IP:192.168.190.128
MySQL版本:5.7.24
从机: slave操作系统:centos 7 64位
IP:192.168.190.130
MySQL版本:5.7.24
mysql已配置开机自启和环境变量。
二、创建数据库
分别登录master机和slave机的mysql:mysql –u root –p
创建数据库:create database test
三、master、slave修改mysql的 my.cnf 配置文件
3.1、master数据库参数配置(主、从机配置的不同主要体现在service-id上)
[root@localhost /]# vi /etc/my.cnf
[mysqld]
bind-address = 0.0.0.0
#master
server-id=1
log-bin=mysql-bin
log-bin-index=mysql-bin.index
log-slave-updates=1
binlog-do-db=test
binlog-ignore-db=mysql
binlog_format=mixed
expire_logs_days=7
#mysql
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql/
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
!includedir /etc/my.cnf.d
然后重启mysql服务并登陆本机mysql;
[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost ~]# mysql -uroot -p
Enter password
查看master机状态,并记住file和position参数,配置slave机时需要用到
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 154 | test | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
为slave创建对应权限的账号,并查看。(在主从同步时从机需连接主机)
mysql> grant replication slave on *.* to 'test'@'192.168.190.130' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host from mysql.user;
+---------------+-----------------+
| user | host |
+---------------+-----------------+
| test | 192.168.190.130 |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------------+
4 rows in set (0.00 sec)
3.2、slave数据库参数配置
[root@localhost /]# vi /etc/my.cnf
[mysqld]
bind-address = 0.0.0.0
#slave
server-id=2
log-bin= mysql-bin
relay-log= mysql-relay-bin
read-only=1
log-slave-updates=1
replicate-do-db=test
#mysql
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql/
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
!includedir /etc/my.cnf.d
重启数据库,并尝试在slave连接master机mysql。
[root@localhost /]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost /]# mysql -h192.168.190.128 -uroot -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.190.128' (113)
有报错,提示无法连接,我们尝试关闭master防火墙,并在slave上重新尝试连接。
master关闭防火墙
[root@localhost ~]# service firewalld stop;
Redirecting to /bin/systemctl stop firewalld.service
slave重新连接
[root@localhost /]# mysql -h192.168.190.128 -utest -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
slave账号连接成功。
四,在slave数据库设置Slave复制参数
mysql> CHANGE MASTER TO MASTER_HOST='192.168.190.128',MASTER_USER='test',MASTER_PASSWORD='123456',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000006',MASTER_LOG_POS=154,MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
在slave数据库启动slave,并查看slave状态(Slave_IO_Running和Slave_SQL_Running参数为Yes说明启动成功)(部分参数)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.190.128
Master_User: test
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000008
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000008
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
配置成功,如果同步期间出现错误,需停止slave,清除slave错误信息,然后重启slave;
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> set global sql_slave_skip_counter=1;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
双主原理和主从一样,两台mysql互为主从。
上面已经配置了主从同步,现在我们需要在master机上面配置slave参数,然后启动slave,在这之前我们需要先配置一下主、从机的/etc/my.cnf文件。
一、master、slave数据库添加主键自增参数。
[root@localhost /]# vi /etc/my.cnf
[mysqld]
bind-address = 0.0.0.0
#master
server-id=1
log-bin=mysql-bin
log-bin-index=mysql-bin.index
log-slave-updates=1
binlog-do-db=test
binlog-ignore-db=mysql
binlog_format=mixed
expire_logs_days=7
#主键自增规则,避免主从同步时id重复
auto_increment_increment=2 #自增长值,几台mysql。
auto_increment_offset=1 #自增长初始值,主库是1,从库是2
#mysql
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql/
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
!includedir /etc/my.cnf.d
二、分别重启master、slave mysql服务。
三、将上方第一步中将主机、从机身份互换,然后重复上方第三步(修改完配置文件)之后的操作,在主机中给从机配置slave账号,并关闭主机防火墙,然后在从机中设置Slave复制参数,然后启动slave。
参考资料:https://www.cnblogs.com/liaojie970/p/6198547.html
https://www.jianshu.com/p/9538753376bb