mysql 5.7.37 主从

2022/4/23 19:13:03

本文主要是介绍mysql 5.7.37 主从,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

准备

ip 主从
192.168.100.100 主(master)
192.168.100.101 从(slave)

一、 建立互信

[root@maseter ~]# ssh-keygen 
[root@maseter ~]# ssh-copy-id 192.168.100.101

二、安装 mysql

主从都需要安装 MySQL

# 卸载 mariadb
rpm -e --nodeps mariadb-libs
# 安装 mysql5.7
wget  http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum localinstall mysql57-community-release-el7-8.noarch.rpm -y
# 更新镜像
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install mysql-community-server -y

systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld

三、修改配置

master 节点操作

[root@master ~]# grep 'temporary password' /var/log/mysqld.log
2022-04-23T02:52:23.118001Z 1 [Note] A temporary password is generated for root@localhost: TMKww8tm)ghy
[root@master ~]# mysql -uroot -p
# 修改密码
mysql> set password for 'root'@'localhost'=password('1qaz!QAZ'); 
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 开启远程控制功能
mysql> grant all privileges on *.* to 'root'@'%' identified by '1qaz!QAZ' with grant option;  
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
# 创建同步账号及授权
mysql> grant replication slave on *.* to 'myslave'@'192.168.100.%' identified by '1qaz!QAZ';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> exit
[root@master ~]# vim /etc/my.cnf
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin = master-bin  #二进制日志文件 master-bin可以自己设置
server-id = 1         #服务器的id号,用于区别
log-slave-updates=true #开启从服务器更新日志功能(结合复制流程连接)
max_connections = 1000
max_connect_errors = 1000
gtid_mode=on  #开启GITD复制模式,gtid在mysql5.6之后才有,但是mysql5.6上gtid默认是不可用的,5.7支持动态修改gtid
enforce-gtid-consistency=true
log-slave-updates=on
binlog_format=row #使用GITD复制模式官方建议使用row复制模式,具有最高性能
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[root@master ~]# systemctl restart mysqld

slave 节点操作

[root@slave ~]# grep 'temporary password' /var/log/mysqld.log
2022-04-23T02:38:05.236939Z 1 [Note] A temporary password is generated for root@localhost: #4uidAqt-SZu
[root@slave ~]# mysql -uroot -p
mysql> set password for 'root'@'localhost'=password('1qaz!QAZ');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@slave ~]# vim /etc/my.cnf

5、slave配置
[root@slave ~]# cat /etc/my.cnf
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=2 #开启二进制日志
log_bin=master-bin #使用中继日志进行同步
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
master_info_repository=TABLE
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
relay_log_info_repository=TABLE
relay_log_recovery=ON
skip-name-resolve
gtid_mode=on #开启GITD复制模式
enforce-gtid-consistency=true
log-slave-updates=on
binlog_format=row #使用GITD复制模式官方建议使用row复制模式,具有最高性能.
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[root@master ~]# systemctl restart mysqld

[root@slave ~]# mysql -uroot -p

mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.7', MASTER_USER='myslave', MASTER_PASSWORD='1qaz!QAZ', master_auto_position=1;start slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

Query OK, 0 rows affected (0.05 sec)

查看集群状态

主库:

mysql> show master status \G;

从库:

mysql> show slave status \G;

测试

在主库创建一个表(create database dbs;)在从库看看能不能查看到(show databases;)。

 



这篇关于mysql 5.7.37 主从的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程