MySQL8.0 mha gtid主从同步搭建步骤
1.二进制安装
0)、修改主机名,配置hosts
hostnamectl set-hostname node1
hostnamectl set-hostname node2
hostnamectl set-hostname node3
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.17.0.35 node1
172.17.0.29 node2
172.17.0.56 node3
1)、创建操作系统用户:
groupadd mysql
useradd -g mysql mysql
2)、解压二进制包,建立软链接
cd /usr/local
tar xvf mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz
ln -s mysql-8.0.25-linux-glibc2.12-x86_64 mysql
3)、修改配置文件my.cnf
主:172.17.0.35
[client]
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
log-error=/usr/local/mysql/data/mysqld.err
port=3306
user=mysql
log_timestamps=system
default_authentication_plugin = mysql_native_password
log-bin=mysql-bin
server-id=663306
gtid-mode=on
enforce-gtid-consistency=1
从1:172.17.0.29
[client]
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
log-error=/usr/local/mysql/data/mysqld.err
port=3308
user=mysql
log_timestamps=system
default_authentication_plugin = mysql_native_password
log-bin=mysql-bin
server-id=663306
gtid-mode=on
enforce-gtid-consistency=1
从2:172.17.0.56
[client]
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
log-error=/usr/local/mysql/data/mysqld.err
port=3307
user=mysql
log_timestamps=system
default_authentication_plugin = mysql_native_password
log-bin=mysql-bin
server-id=883306
gtid-mode=on
enforce-gtid-consistency=1
4)、创建数据目录,并修改其属主、属组。
mkdir -p /usr/local/mysql/data
chown mysql.mysql /usr/local/mysql/data
4.1)、配置互信,每台都要做
ssh-keygen -t rsa
ssh-copy-id 172.17.0.35
ssh-copy-id 172.17.0.29
ssh-copy-id 172.17.0.56
4.3、关闭防火墙以及selinux
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
vim /etc/selinux/config
SELINUX=disabled
setenforce 0
5)、初始化实例:
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize
6)、启动实例
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
7)、登录实例(使用初始化过程中生成的随机密码)
grep ‘temporary password’ mysqld.err
8)、配置mysql环境变量
echo “PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH” >> /etc/profile
echo “export PATH” >> /etc/profile
source /etc/profile
9)、修改root用户密码
mysql -uroot -p’>H0VQ;ZEm:FK’;
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
create user root@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
10)、创建同步用户repl以及赋权
create user ‘repl’@’%’ identified with mysql_native_password by ‘123456’;
GRANT REPLICATION SLAVE ON . TO ‘repl’@’%’;
11)、创建用户mha以及赋权
create user ‘mha’@’%’ identified with mysql_native_password by ‘123456’;
grant all privileges on . to ‘mha’@’%’;
flush privileges;
12)、验证用户远程登录是否成功
mysql -urepl -p123456 -h172.17.0.35 -P 3306
13)、检查同步用户权限
show grants for repl@’%’\G
14)、搭建MySQL8.0gtid主从同步
主:172.17.0.35
mysql> show master status \G;
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 156
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
从1:172.17.0.29
change master to master_host=“172.17.0.35”,master_user=“repl”,master_password=“123456”,master_port=3306,master_auto_position=1;
start slave;
show slave status \G;
mysql> change master to master_host=“172.17.0.35”,master_user=“repl”,master_password=“123456”,master_port=3306,master_auto_position=1;
Query OK, 0 rows affected, 8 warnings (0.06 sec)
mysql>
mysql>
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.35
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 156
Relay_Log_File: node2-relay-bin.000002
Relay_Log_Pos: 371
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 156
Relay_Log_Space: 580
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 663306
Master_UUID: fbf4026c-5572-11ee-a740-000c29db9875
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: fd29ee69-b82f-11ee-9515-000c296d8999:1-7
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
从2:172.17.0.56
change master to master_host=“172.17.0.35”,master_user=“repl”,master_password=“123456”,master_port=3306,master_auto_position=1;
start slave;
show slave status \G;
mysql> change master to master_host=“172.17.0.35”,master_user=“repl”,master_password=“123456”,master_port=3306,master_auto_position=1;
Query OK, 0 rows affected, 8 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.35
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 156
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 371
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 156
Relay_Log_Space: 584
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 663306
Master_UUID: fbf4026c-5572-11ee-a740-000c29db9875
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: c7229515-b876-11ee-afea-0050562ee3cd:1-7
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified
15)、如主从环境搭建失败清理步骤
主:
reset master;
show master status \G;
从:
stop slave;
reset slave all;
show slave status \G;
以上为测试环境实验,生产环境谨慎操作。