双主模式:两台服务器互为主从,任何一台服务器数据变更,都会通过复制应用到另外一方的数据库中。
-
安装wget依赖
yum install wget -y
-
下载MysqL5.7.26到data目录
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
-
解压MysqL
tar -zxvf MysqL-5.7.26-linux-glibc2.12-x86_64.tar.gz
-
cd /MysqL-5.7.26
mkdir data
chmod -R 777 /data/MysqL-5.7.26/data/
-
groupadd MysqL
-
vi /etc/my.cnf
[MysqLd]
bind-address=0.0.0.0
port=3306
log-bin=MysqL-bin
sync-binlog=1
#双主配置
relay_log=MysqL-relay-bin
log_slave_updates=1
auto_increment_offset=1
auto_increment_increment=2
#skip-grant-tables
user=MysqL
basedir=/data/MysqL-5.7.26
datadir=/data/MysqL-5.7.26/data
socket=/tmp/MysqL.sock
log-error=/data/MysqL-5.7.26/data/MysqL.err
pid-file=/data/MysqL-5.7.26/data/MysqL.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true -
[MysqLd]
server-id=2
log-bin=MysqL-bin
sync-binlog=1
relay_log=MysqL-relay-bin
log_slave_updates=1
auto_increment_offset=2
auto_increment_increment=2
bind-address=0.0.0.0
port=3306
#skip-grant-tables
user=MysqL
basedir=/data/MysqL-5.7.26
datadir=/data/MysqL-5.7.26/data
socket=/tmp/MysqL.sock
log-error=/data/MysqL-5.7.26/data/MysqL.err
pid-file=/data/MysqL-5.7.26/data/MysqL.pid
log-slave-updates = 1
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true -
进入bin目录,初始化MysqL
cd /data/MysqL-5.7.26/bin
./MysqLd --initialize --user=MysqL --datadir=/data/MysqL-5.7.26/data/ --basedir=/data/MysqL-5.7.26/
-
查看MysqL初始化密码
-
ln -s /data/MysqL-5.7.26/support-files/MysqL.server /etc/init.d/MysqL
ln -s /data/MysqL-5.7.26/bin/MysqL /usr/bin/MysqL
service MysqL restart
-
vi /etc/my.cnf
去掉#skip-grant-tables的注释
MysqL -u root -p
use MysqL
update user set authentication_string=password('Pw!123456') where user='root';
|alter user USER() identified by 'Pw!123456';
开启远程访问权限
update user set host ='%' where user ='root';
刷新权限
flush privileges;
重启MysqL
service MysqL restart
-
将MysqL端口加入防火墙的白名单
firewall-cmd --zone=public --add-port=3306/tcp --permanent
systemctl restart firewalld.service
-
重启MysqL验证
service MysqL restart
MysqL -u root -p
查看二进制日志是否开启
show global variables like '%log_bin%';
log_bin value 等于ON 说明开启
查看主节点二进制日志列表 (以后会有多个)
show master logs;
查看主节点的serverid
show global variables like '%server%';
-
GRANT REPLICATION SLAVE ON . TO 'root'@'%' IDENTIFIED BY 'abc123';
flush privileges;
-
查看状态
show master status;
-
配置双主关系
Maser01
MysqL> stop slave;
MysqL> change master to master_host='192.168.0.103',master_port=3306,master_user='root',master_password='Pw!123456',master_log_file='MysqL-bin.000003',master_log_pos=403;
master02
MysqL> stop slave;
MysqL> change master to master_host='192.168.0.102',master_port=3306,master_user='root',master_password='123456',master_log_file='MysqL-bin.000004',master_log_pos=403;
#查看状态
MysqL> start slave;
MysqL> show slave status \G;
注意没报错并且Slave_IO_Running和Slave_sql_Running都为YES,则配置成功。 -
设置开机自启动
查看自启动的服务
chkconfig --list
-
设置自启动
cd /etc/rc.d/init.d
cp /data/MysqL-5.7.26/support-files/MysqL.server /etc/rc.d/init.d/
赋予可执行权限:chmod +x /etc/init.d/MysqL.server