MySQL8.0.33 master-slave replication configuration record

Official website: https://dev.mysql.com/downloads/mysql/
8.0.33
insert image description here

1. Download

Download online or download locally and upload

cd /usr/local

Prepare the wget download tool:

yum install wget -y
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.33-el7-x86_64.tar.gz

insert image description here
download


2. Unzip

tar -zxvf mysql-8.0.33-el7-x86_64.tar.gz -C /usr/local/

3. Double naming

mv mysql-8.0.33-el7-x86_64 mysql

4. Create a storage data file

mkdir mysql/data

5. Set up user groups and empower them

groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql /usr/local/mysql/
chmod -R 755 /usr/local/mysql

6. Initialize MySQL

Enter the MySQL bin directory

cd mysql/bin

Initialize and get password

./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql

./mysqld: error while loading shared libraries: libnuma.so.1: cannot
open shared object file: No such file or directory
报错,缺依赖::::
yum install -y libaio
yum -y install numactl
再初始化
./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql

initial password

7. Configuration file

vi /etc/my.cnf
[client]
#password       = your_password
port               = 3306
socket          = /usr/local/mysql/data/mysql.sock
default-character-set = utf8mb4

[mysql]  
default-character-set = utf8mb4

[mysqld]  
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect = 'SET NAMES utf8mb4'

# 服务器唯一id,默认为1,值范围为1~2^32−1. ;主数据库和从数据库的server-id不能重复
server-id=1
# 设置同步的binary log二进制日志文件名前缀,默认是binlog
log-bin=mysql-bin
# 可选配置
# 是否只读 0读写 1只读
read-only=0
# 禁用管理员写操作
# super-read-only=1
# 需要主从复制的数据库 ,如多个则重复配置(不设置则都同步)
# replicate-do-db=user_db.%
#屏蔽系统库同步,如果需要对某个数据库不进行同步则追加以下配置,配置对应的数据库名称,多个的话配置多行
replicate_wild_ignore_table=mysql.%
replicate_wild_ignore_table=information_schema.%
replicate_wild_ignore_table=performance_schema.%

port            = 3306
socket          = /usr/local/mysql/data/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
datadir = /usr/local/mysql/data

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout


8. Start MySQL

/usr/local/mysql/support-files/mysql.server start

9. Set a soft connection and restart MySQL

ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/mysql.sock /var/mysql.sock 
service mysql restart

10. Login and change password

mysql -u root -p

Enter the initial password obtained by the initialization above

Change encryption:

alter user 'root'@'localhost' identified by '123456';

11. Open remote connection

use mysql;
update user set user.Host='%' where user.User='root';
flush privileges;

Since the localhost is changed to % here, so follow-up
alter user 'root'@'%' identified by '123456';
flush privileges;

12. Connection tool to test connection to MySQL

Remember to release the port on the server and
open the specified port
firewall-cmd --zone=public --add-port=3306/tcp --permanent
–zone #Scope
–add-port=3306/tcp #Add port, the format is: port /communication protocol
–permanent #Permanently effective, without this parameter, it will fail after restarting,
test connection
then exit MySQL and use exit, wait until the slave server is done well, and then create a table test

exit

13. Boot configuration

Copy the service file to init.d and rename it to mysqld

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

grant executable permissions

chmod +x /etc/init.d/mysqld

add service

chkconfig --add mysqld

show service list

chkconfig --list

Note: If you see the mysql service, and 3, 4, 5 are all open, then it is successful, if it is closed, then

chkconfig --level 345 mysqld on

14. Slave server configuration

The installation process is the same as above, only the configuration is slightly different

vi /etc/my.cnf
# 服务器唯一id,默认为1,值范围为1~2^32?1. ;主数据库和从数据库的server-id不能重复
server-id=2
# 设置同步的binary log二进制日志文件名前缀,默认是binlog
log-bin=mysql-bin
# 可选配置
# 是否只读 0读写 1只读
read-only=1
# 禁用管理员写操作
# super-read-only=1
# 需要主从复制的数据库 ,如多个则重复配置(不设置则都同步)
# replicate-do-db=user_db.%
#屏蔽系统库同步,如果需要对某个数据库不进行同步则追加以下配置,配置对应的数据库名称,多个的话配置多行
replicate_wild_ignore_table=mysql.%
replicate_wild_ignore_table=information_schema.%
replicate_wild_ignore_table=performance_schema.%

permissions

15. Main library configuration

Login to 主服务器MySQL

mysql -uroot -p

Grant master-slave replication permissions

GRANT REPLICATION SLAVE ON *.* TO 'root'@'%';

master database
Through the command, view the binary log target

show master status;

Target

16. Slave library configuration

Login to 从库服务器MySQL

mysql -uroot -p

Associate the main server (note that the log file and location name of the main server must be consistent)
syntax after 8.0.23 (compatible with previous syntax)
CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.20.20',SOURCE_USER='root',SOURCE_PASSWORD='123456',SOURCE_LOG_FILE='mysql-bin.000003',SOURCE_LOG_POS=369;

Syntax before 8.0.23

CHANGE REPLICATION MASTER TO MASTER_HOST='*.*.*.*',MASTER_USER='***',MASTER_PASSWORD='**',MASTER_LOG_FILE='**',MASTER_LOG_POS=**;

MASTER_HOST master database IP address
MASTER_USER connection master database user name
MASTER_PASSWORD connection master database password
MASTER_LOG_FILE binlog log file name
MASTER_LOG_POS binlog log file location

Configure the main library connection
Turn on sync operation:

8.0.22版本之后(兼容之前的命令)
start replica; 
8.0.22版本之前
start slave;

Enable sync operation
View master-slave replication status:

show replica status\G;

UUID
At this time, the UUID conflict is found, the UUID needs to be changed, and
MySQL exits

exit;

Generally in the /mysql/data/auto.cnf file

vi /usr/local/mysql/data/auto.cnf

Randomly change a number (the length is fixed, if one is missing, an error will be reported when starting up, it is recommended to adjust a number randomly), ESC :wq to save and exit, and then restart MySQL.
UUID

Restart MySQL:

service mysql restart

Then check the sync status

show replica status\G;

yes

17. Test:

主服务器Create new library, use library, create new table, insert data, query table, query table data

CREATE database test;
use test;
CREATE TABLE `tb_user`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
  `sex` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
INSERT INTO `tb_user` VALUES (1, 'coisini', '0');
show tables;
select * from tb_user;

new database
Check 从服务器if sync

use test;
show tables;
select * from tb_user;

data

Summary: Synchronize
the data changes of the master database to the slave database to ensure the consistency of the master-slave data, which can be used for data backup, failure migration, read-write classification, and reduce the read-write pressure of a single database.
Principle:
The main library will record data changes in the binary log file binlog.
The slave library connects to the main library, reads the binlog log, and writes its own relay log to the relaylog.
The slave redoes the relay log, which will change and reflect its own data.


18. In addition, to implement master-slave replication on Windows, deploy two MySQL first

Refer to this local deployment of multiple MySQL , my local configuration is posted below, you can refer to
the main server configuration:

[mysqld]
# 设置3307端口
port=3307
# 服务器唯一id,默认为1,值范围为1~2^32−1. ;主数据库和从数据库的server-id不能重复
server-id=1
# 主从数据库配置核心部分
# 设置同步的binary log二进制日志文件名前缀,默认为binlog;在MySQL 8.0中,无论是否指定--log bin选项,默认情况下都会启用二进制日志记录,并将log_bin系统变量设置为ON。
log-bin=mysql-bin
# 可选配置
# 是否只读 0读写 1只读
read-only=0
# 需要主从复制的数据库,如多个则重复配置
# binlog-do-db=user_db
# 屏蔽系统库同步
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
# 设置mysql的安装目录
basedir=F:/MySQL/mysql-8.0.26-winx64
# 设置mysql数据库的数据的存放目录
datadir=F:/MySQL/mysql-8.0.26-winx64/data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。
max_connect_errors=10
# 服务端使用的字符集默认为utf8mb4
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
#mysql_native_password
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3307
default-character-set=utf8mb4

From the server configuration:

[mysqld]
# 登录跳过权限检查
# skip-qrant-tables
# 从服务器设置3308端口
port=3308
# 服务器唯一id,默认为1,值范围为1~2^32−1. ;主数据库和从数据库的server-id不能重复
server-id=2
# 设置同步的binary log二进制日志文件名前缀,默认是binlog
log-bin=mysql-bin
# 可选配置
# 是否只读 0读写 1只读
read-only=1
# 禁用管理员写操作
# super-read-only=1
# 需要主从复制的数据库 ,如多个则重复配置
# replicate-do-db=user_db.%
#屏蔽系统库同步,如果需要对某个数据库不进行同步则追加以下配置,配置对应的数据库名称,多个的话配置多行
replicate_wild_ignore_table=mysql.%
replicate_wild_ignore_table=information_schema.%
replicate_wild_ignore_table=performance_schema.%
# 设置从服务器mysql的安装目录
basedir=F:/MySQL/mysql-8.0.26-winx64-s1
# 设置从服务器mysql数据库的数据的存放目录
datadir=F:/MySQL/mysql-8.0.26-winx64-s1/data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。
max_connect_errors=10
# 服务端使用的字符集默认为utf8mb4
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
#mysql_native_password
default_authentication_plugin=mysql_native_password
[mysql]
# 设置从服务器mysql客户端默认字符集
default-character-set=utf8mb4
[client]
# 设置从服务器mysql客户端连接服务端时默认使用的端口
port=3308
default-character-set=utf8mb4

The steps are similar. There are two MySQL first, and then configure the master-slave configuration, query whether the synchronization is successful (two YES), and create a new database for testing.


Done, thank you for reading, I hope it can be helpful to you~
END


Guess you like

Origin blog.csdn.net/qq_44870331/article/details/130251438