安装MySQL主备服务

Install MySQL on master and slave

yum install wget -y

wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

rpm -ivh mysql57-community-release-el7-9.noarch.rpm

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

rpm -qa gpg-pubkey*

yum install mysql-server -y

systemctl start mysqld

systemctl status mysqld

systemctl enable mysqld

grep 'temporary password' /var/log/mysqld.log

mysql_secure_installation

Configure MySQL Master Node

First, we will configure the master node with the required configuration.

The key configurations are

  1. Binding static IP address to MySQL server.
  2. Unique ID config for master. Each server in the cluster should be identified uniquely.
  3. Enable Binary log -- It contains information about the data changes made to MySQL.
  4. Create a replication user that will be used by the slaves to replicate the data from the master.

Note: In this installation, MySQL data dir will default to /var/lib/mysql in the root file system. For production use cases, this path should be an external disk attached to the VM.

Step 1: Open /etc/my.cnf file.

复制代码
sudo vi /etc/my.cnf

Step 2: Add the following three parameters under the [mysqld] section and save it. Replace 10.128.0.11 your server IP.

复制代码
bind-address           = 10.128.0.11
server-id              = 1
log_bin                = mysql-bin

Step 3: Restart the MySQL server for the configurations changes to take place.

复制代码
sudo systemctl restart mysqld

Step 4: Check the MySQL status to make sure all the configurations are applied as expected without any errors.

复制代码
sudo systemctl status mysqld

Step 5: Login to MySQL server as the root user.

复制代码
mysql -uroot -p

Step 6: Create a user named replicauser with a strong password. This user will be used by the slaves to replicate the data from the master. Replace 10.128.0.11 with your master IP

复制代码
CREATE USER 'replicauser'@'10.128.0.11' IDENTIFIED BY 'my-secret-password';

Step 7: Grant privileges to the slave user for slave replication.

复制代码
GRANT REPLICATION SLAVE ON *.* TO 'replicauser'@'%' IDENTIFIED BY 'my-secret-password';

Step 8: From the MySQL prompt, Check the master status. Note down the file [mysql-bin.000001 ] and Position[706] parameters from the output. It is required for the slave replication configuration.

复制代码
SHOW MASTER STATUS\G

The output would look like the following.

复制代码
mysql> SHOW MASTER STATUS\G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 706
     Binlog_Do_DB:
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

Configure MySQL Replication Slave Node

Execute the following steps in all the slaves.

Step 1: Add the same configurations as the master to the /etc/my.cnf file with the Slave Ip address and unique server ID.

复制代码
bind-address           = 10.128.0.12
server-id              = 2
log_bin                = mysql-bin

Note: If you more than one slave, make sure you replace the respective slave IP and add a unique server-id per slave.

Step 2: Restart the MySQL service.

复制代码
sudo systemctl restart mysqld

Step 3: Login to MySQL with root credentials.

复制代码
mysql -uroot -p

Step 4: Stop the slave threads using the following command.

复制代码
STOP SLAVE;

Step 5: Execute the following statement from MySQL prompt replacing the master IP [10.128.0.15], replicauser password [replicauser-secret-password].

Replace MASTER_LOG_FILE & MASTER_LOG_POS with the values, you got from step 8 in master configuration.

复制代码
CHANGE MASTER TO MASTER_HOST='10.128.0.15',MASTER_USER='replicauser', MASTER_PASSWORD='replicauser-secret-password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  706;

Step 6: Now, start the slave threads.

复制代码
START SLAVE;

Step 7: Check the MySQL replication slave status.

复制代码
SHOW SLAVE STATUS\G

Slave_SQL_Running_State parameter will show the current slave status.

Test MySQL Master-Slave Replication

In this section, we will test master-slave replication.

On Master Server

Login to master mySQL CLI.

复制代码
mysql -uroot -p

Create a database named testdb

复制代码
CREATE DATABASE testdb;

On Slave Server

Login to slave mySQL CLI.

复制代码
mysql -uroot -p

List the Databases. You should see the testdb database created from the master server.

复制代码
show databases;

Example, output.

复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
+--------------------+
相关推荐
Hello.Reader4 小时前
Redis 延迟监控深度指南
数据库·redis·缓存
ybq195133454314 小时前
Redis-主从复制-分布式系统
java·数据库·redis
一心0926 小时前
ubuntu 20.04.6 sudo 源码包在线升级到1.9.17p1
运维·ubuntu·sudo·漏洞升级
好好学习啊天天向上6 小时前
世上最全:ubuntu 上及天河超算上源码编译llvm遇到的坑,cmake,ninja完整过程
linux·运维·ubuntu·自动性能优化
你想考研啊6 小时前
三、jenkins使用tomcat部署项目
运维·tomcat·jenkins
好奇的菜鸟7 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
tan180°7 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
代码老y7 小时前
Docker:容器化技术的基石与实践指南
运维·docker·容器
满昕欢喜7 小时前
SQL Server从入门到项目实践(超值版)读书笔记 20
数据库·sql·sqlserver
典学长编程7 小时前
Linux操作系统从入门到精通!第二天(命令行)
linux·运维·chrome