Install Mysql8.0.20 on Linux and configure master-slave replication (one master and one slave, dual master and dual slave)

Reprinted from: https://www.cnblogs.com/cao-lei/p/13603043.html

 

1. Master-slave replication explanation #

  Operations such as additions, deletions, modifications, and queries of the master database are recorded in the binary log file. The slave database receives the master database log file and synchronously copies it to the slave database based on the starting position of the last update, so that the master and slave databases remain consistent.

2. The role of master-slave replication #

  • High availability: If the master database is abnormal, it can be switched to the slave database.
  • Load balancing: achieving read and write separation
  • Backup: Make daily backups

3. Mysql master-slave replication process #


  Binary log: the binary log of the master database; Relay log: the relay log of the slave server.

Replication process:
  (1) Before each transaction is completed, the master database records the operation into the binlog log file;
  (2) There is an I/O thread in the slave database, which is responsible for connecting to the master database service and reading the binlog log changes. , if new changes are found, the changes will be written to the relay-log, otherwise it will enter the dormant state;
  (3) Read the relay log from the SQL Thread in the database, and execute the SQL events serially, so that the slave database can communicate with the master The database is always consistent.

Notes:
  (1) When time functions are involved, data inconsistency will occur. The reason is that the two IO operations during the replication process and issues such as network and disk efficiency will inevitably lead to inconsistent timestamps;
  (2) Inconsistencies will occur when system functions are involved. For example: @@hostname, obtain the host name, the master-slave database server name is inconsistent, resulting in data inconsistency;
  (3)...

4. One master and one slave configuration #

  • Server division
Server IP Role
192.168.133.129 Master1
192.168.133.130 Slave1
  • Master database installation
 

Copy

# 进入目录 cd /opt # 下载安装包 wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz # 解压 tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz # 拷贝到/usr/local mv /opt/mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local # 进入/usr/local cd /usr/local # 修改名称为mysql-8.0.20 mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql-8.0.20 # 创建存放数据文件夹 mkdir /usr/local/mysql-8.0.20/data # 创建用户及用户组 groupadd mysql useradd -g mysql mysql # 授权 chown -R mysql.mysql /usr/local/mysql-8.0.20 # 初始化数据库(记录临时密码) cd /usr/local/mysql-8.0.20/ ./bin/mysqld --user=mysql --lower-case-table-names=1 --basedir=/usr/local/mysql-8.0.20/ --datadir=/usr/local/mysql-8.0.20/data/ --initialize ; # 配置my.cnf vi /etc/my.cnf # 清空,使用下面内容 // 文件内容开始 [mysqld] basedir=/usr/local/mysql-8.0.20 datadir=/usr/local/mysql-8.0.20/data character-set-server=utf8 lower-case-table-names=1 default_authentication_plugin=mysql_native_password # 主从复制-主机配置 # 主服务器唯一ID server-id=1 # 启用二进制日志 log-bin=mysql-bin # 设置不要复制的数据库(可设置多个) binlog-ignore-db=sys binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-ignore-db=performance_schema # 设置需要复制的数据库(可设置多个) binlog-do-db=test # 设置logbin格式 binlog_format=STATEMENT // 文件内容结束 # 建立Mysql服务 cp -a ./support-files/mysql.server /etc/init.d/mysql chmod +x /etc/init.d/mysql chkconfig --add mysql # 检查服务是否生效 chkconfig --list mysql # 启动、停止、重启 service mysql start service mysql stop service mysql restart # 创建软连接 ln -s /usr/local/mysql-8.0.20/bin/mysql /usr/bin # 登录(使用临时密码) mysql -uroot -p # 修改密码 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new password'; # 退出,使用新密码登录 quit mysql -uroot -p # 修改root权限,增加远程连接 use mysql update user set host ='%' where user='root'; alter user 'root'@'%' identified with mysql_native_password by 'new password'; flush privileges; # 退出 quit

  • The slave database installation
      is the same as the master database installation, but the content of the configuration file is different.
 

Copy

# 配置my.cnf vi /etc/my.cnf # 清空,使用下面内容 // 文件内容开始 [mysqld] basedir=/usr/local/mysql-8.0.20 datadir=/usr/local/mysql-8.0.20/data character-set-server=utf8 lower-case-table-names=1 default_authentication_plugin=mysql_native_password # 主从复制-从机配置 # 从服务器唯一ID server-id=2 # 启用中继日志 relay-log=mysql-relay // 文件内容结束

  • Close the firewall of the master-slave database server or open port 3306
 

Copy

# 查看防火墙状态 systemctl status firewalld # 关闭防火墙 systemctl stop firewalld

  • Test whether the master-slave database can be accessed remotely
 

Copy

# 主数据库服务器测试从数据库 mysql -uroot -p -h192.168.133.130 -P3306 # 从数据库服务器测试主数据库 mysql -uroot -p -h192.168.133.129 -P3306

  • The master database creates user slave and authorizes it
 

Copy

# 登录 mysql -uroot -p # 创建用户 create user 'slave'@'%' identified with mysql_native_password by 'password'; # 授权 grant replication slave on *.* to 'slave'@'%'; # 刷新权限 flush privileges;

  • Verify from the database whether the slave user is available
 

Copy

mysql -uslave -p -h192.168.133.129 -P3306

  • Master database query service ID and Master status
 

Copy

# 登录 mysql -uroot -p # 查询server_id是否可配置文件中一致 show variables like 'server_id'; # 若不一致,可设置临时ID(重启失效) set global server_id = 1; # 查询Master状态,并记录 File 和 Position 的值 show master status; # 注意:执行完此步骤后退出主数据库,防止再次操作导致 File 和 Position 的值发生变化

  • Set the master database from the database
 

Copy

# 登录 mysql -uroot -p # 查询server_id是否可配置文件中一致 show variables like 'server_id'; # 若不一致,可设置临时ID(重启失效) set global server_id = 2; # 设置主数据库参数 change master to master_host='192.168.133.129',master_port=3306,master_user='slave',master_password='password',master_log_file='mysql-bin.000002',master_log_pos=156; # 开始同步 start slave; # 若出现错误,则停止同步,重置后再次启动 stop slave; reset slave; start slave; # 查询Slave状态 show slave status\G # 查看是否配置成功 # 查看参数 Slave_IO_Running 和 Slave_SQL_Running 是否都为yes,则证明配置成功。若为no,则需要查看对应的 Last_IO_Error 或 Last_SQL_Error 的异常值。

  • Test
      the connection between the master and slave databases through tools or on the server.
      Note: The databases that need to be synchronized are configured in the configuration file of the main database, so only the configured databases will be synchronized. If not configured, all databases will be synchronized.
 

Copy

# 在主数据库创建数据库test create database test; # 从数据库查看 show databases; # 在主数据库创建表 use test; create table t_user(id int, name varchar(20)); # 插入数据 insert into t_user values(1, 'C3Stones'); # 在从数据库查看 use test; select * from t_user; # 其他删改查操作请自行测试

5. Dual master and dual slave configuration #

  Dual master and dual slave means that two hosts each have two slave machines. Each slave machine only replicates the corresponding host machine, and the two hosts are each other's master and backup machines.

  • Server division
Server IP Role
192.168.133.129 Master1
192.168.133.130 Slave1
192.168.133.131 Master2
192.168.133.132 Slave2
  • To install the database, please refer to the above installation of the main database.
    The four configuration files are replaced as follows:
    (1) Mater1
 

Copy

[mysqld] basedir=/usr/local/mysql-8.0.20 datadir=/usr/local/mysql-8.0.20/data character-set-server=utf8 lower-case-table-names=1 default_authentication_plugin=mysql_native_password # 主从复制-主机1配置 # 主服务器唯一ID server-id=1 # 启用二进制日志 log-bin=mysql-bin # 设置不要复制的数据库(可设置多个) binlog-ignore-db=sys binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-ignore-db=performance_schema # 设置需要复制的数据库(可设置多个) binlog-do-db=test # 设置logbin格式 binlog_format=STATEMENT # 写入操作更新二进制日志文件 log-slave-updates # 自增长字段起始值,默认值为1,取值范围:1 ~ 65535 auto-increment-increment=2 # 自增长字段递增量,取值范围:1 ~ 65535 auto-increment-offset=1

(2)Mater2

 

Copy

[mysqld] basedir=/usr/local/mysql-8.0.20 datadir=/usr/local/mysql-8.0.20/data character-set-server=utf8 lower-case-table-names=1 default_authentication_plugin=mysql_native_password # 主从复制-主机2配置 # 主服务器唯一ID server-id=3 # 启用二进制日志 log-bin=mysql-bin # 设置不要复制的数据库(可设置多个) binlog-ignore-db=sys binlog-ignore-db=mysql binlog-ignore-db=information_schema binlog-ignore-db=performance_schema # 设置需要复制的数据库(可设置多个) binlog-do-db=test # 设置logbin格式 binlog_format=STATEMENT # 写入操作更新二进制日志文件 log-slave-updates # 自增长字段起始值,默认值为1,取值范围:1 ~ 65535 auto-increment-increment=2 # 自增长字段递增量,取值范围:1 ~ 65535 auto-increment-offset=2

(3)Slave1

 

Copy

[mysqld] basedir=/usr/local/mysql-8.0.20 datadir=/usr/local/mysql-8.0.20/data character-set-server=utf8 lower-case-table-names=1 default_authentication_plugin=mysql_native_password # 主从复制-从机1配置 # 从服务器唯一ID server-id=2 # 启用中继日志 relay-log=mysql-relay

(4)Slave2

 

Copy

[mysqld] basedir=/usr/local/mysql-8.0.20 datadir=/usr/local/mysql-8.0.20/data character-set-server=utf8 lower-case-table-names=1 default_authentication_plugin=mysql_native_password # 主从复制-从机2配置 # 从服务器唯一ID server-id=4 # 启用中继日志 relay-log=mysql-relay

  • Restart both master and slave databases
 

Copy

service restart mysql

  • Turn off the firewall on all four servers
 

Copy

systemctl stop firewalld

  • Create user slaves and authorize them on the two main databases respectively.
 

Copy

# 登录 mysql -uroot -p # 创建用户 create user 'slave'@'%' identified with mysql_native_password by 'password'; # 授权 grant replication slave on *.* to 'slave'@'%'; # 刷新权限 flush privileges;

  • The master-slave database verifies whether the slave user is available
 

Copy

# 主数据库1服务器测试 mysql -uslave -p -h192.168.133.130 -P3306 mysql -uslave -p -h192.168.133.131 -P3306 # 从数据库1服务器测试主数据库1 mysql -uroot -p -h192.168.133.129 -P3306 # 主数据库2服务器测试 mysql -uslave -p -h192.168.133.129 -P3306 mysql -uslave -p -h192.168.133.132 -P3306 # 从数据库1服务器测试主数据库1 mysql -uroot -p -h192.168.133.131 -P3306

  • Query the service ID and Master status of the two master databases
 

Copy

# 登录 mysql -uroot -p # 查询server_id是否可配置文件中一致 show variables like 'server_id'; # 若不一致,可设置临时ID(重启失效) # 主数据库1 set global server_id = 1; # 主数据库2 set global server_id = 3; # 查询Master状态,并记录 File 和 Position 的值 show master status; # 注意:执行完此步骤后退出主数据库,防止再次操作导致 File 和 Position 的值发生变化

  • Set master database 1 from database 1
 

Copy

# 登录 mysql -uroot -p # 查询server_id是否可配置文件中一致 show variables like 'server_id'; # 若不一致,可设置临时ID(重启失效) set global server_id = 2; # 设置主数据库参数 change master to master_host='192.168.133.129',master_port=3306,master_user='slave',master_password='password',master_log_file='mysql-bin.000003',master_log_pos=156; # 开始同步 start slave; # 若出现错误,则停止同步,重置后再次启动 stop slave; reset slave; start slave; # 查询Slave状态 show slave status\G # 查看是否配置成功 # 查看参数 Slave_IO_Running 和 Slave_SQL_Running 是否都为yes,则证明配置成功。若为no,则需要查看对应的 Last_IO_Error 或 Last_SQL_Error 的异常值。

  • Set master database 2 from database 2
 

Copy

# 登录 mysql -uroot -p # 查询server_id是否可配置文件中一致 show variables like 'server_id'; # 若不一致,可设置临时ID(重启失效) set global server_id = 4; # 设置主数据参数 change master to master_host='192.168.133.131',master_port=3306,master_user='slave',master_password='password',master_log_file='mysql-bin.000001',master_log_pos=156; # 开始同步 start slave; # 若出现错误,则停止同步,重置后再次启动 stop slave; reset slave; start slave; # 查询Slave状态 show slave status\G # 查看是否配置成功 # 查看参数 Slave_IO_Running 和 Slave_SQL_Running 是否都为yes,则证明配置成功。若为no,则需要查看对应的 Last_IO_Error 或 Last_SQL_Error 的异常值。

  • Set master database 2 in master database 1
 

Copy

# 登录 mysql -uroot -p # 设置主数据库参数 change master to master_host='192.168.133.131',master_port=3306,master_user='slave',master_password='password',master_log_file='mysql-bin.000001',master_log_pos=156; # 开始同步 start slave; # 若出现错误,则停止同步,重置后再次启动 stop slave; reset slave; start slave; # 查询Slave状态 show slave status\G # 查看是否配置成功 # 查看参数 Slave_IO_Running 和 Slave_SQL_Running 是否都为yes,则证明配置成功。若为no,则需要查看对应的 Last_IO_Error 或 Last_SQL_Error 的异常值。

  • Set main database 1 in main database 2
 

Copy

# 登录 mysql -uroot -p # 设置主数据库参数 change master to master_host='192.168.133.129',master_port=3306,master_user='slave',master_password='password',master_log_file='mysql-bin.000003',master_log_pos=156; # 开始同步 start slave; # 若出现错误,则停止同步,重置后再次启动 stop slave; reset slave; start slave; # 查询Slave状态 show slave status\G # 查看是否配置成功 # 查看参数 Slave_IO_Running 和 Slave_SQL_Running 是否都为yes,则证明配置成功。若为no,则需要查看对应的 Last_IO_Error 或 Last_SQL_Error 的异常值。

  • Test
      to connect the dual master and dual slave databases through tools or connect to the server.
      Note: The databases that need to be synchronized are configured in the configuration file of the main database, so only the configured databases will be synchronized. If not configured, all databases will be synchronized.
 

Copy

# 在主数据库1创建数据库test create database test; # 其他三个数据库查看 show databases; # 在主数据库1创建表 use test; create table t_user(id int, name varchar(20)); # 插入数据 insert into t_user values(1, 'C3Stones'); # 其他三个数据库查看 use test; select * from t_user; # 其他删改查操作请自行测试

Guess you like

Origin blog.csdn.net/liuwkk/article/details/109781031