Database series-MySQL master-slave replication configuration

1. Background

In the previous project, the most basic and most mainstream database was a stand-alone database, and both read and write were in one library. When the number of users gradually increases and the stand-alone database cannot meet the performance requirements, the read-write separation transformation will be carried out (applicable to read more and write less), write to one library, read to multiple libraries, usually a database cluster, open the master and slave Backup, one master and multiple slaves to improve read performance. When users cannot meet more read-write separation, a distributed database is needed. (With MyCat, sharing-jdbc , Canal, etc.), Ant Financial, Oceanbase.

Under normal circumstances, to realize the separation of read and write, a database cluster with one master and multiple slaves must be made first, and data synchronization is also required. This article records how to use mysql to build a one-master multiple configuration,

The next article records how to achieve separation of reading and writing at the code level.

2. Build a database cluster with one master and multiple slaves

Master-slave backup requires multiple virtual machines. If you use wmware to completely clone multiple instances, please note that the direct cloning of virtual machines will cause the uuid of each database to be the same, and you need to modify it to a different uuid.

The core configuration is as follows:

Main library configuration

Create a new user in the master database to read the binary log of the master database from the database (slave). The sql statement is as follows:

mysql> CREATE USER 'repl'@'%' IDENTIFIED BY '123456';#创建用户
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';#分配权限
mysql>flush privileges; #刷新权限

At the same time, modify the mysql configuration file to turn on the binary log, the new part is as follows:

[mysqld]
server-id=1
log-bin=master-bin #这边写的是对应bin的日志名称
log-bin-index=master-bin.index

Then restart the database, use the show master status;statement on the main library to view the status of the main library, as shown below:

 

Configure from library

Also add a few lines of configuration first:

[mysqld]
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin

Then restart the database and use the following statement to connect to the main library:

CHANGE MASTER TO
MASTER_HOST='192.168.226.5',
MASTER_USER='root',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=6953;

Then run the start slave;backup on the slave library , the normal situation is shown in the following figure: Slave_IO_Running and Slave_SQL_Running are both yes.

 

You can use this step to open multiple slave libraries.

  By default, all operations of the main library will be backed up to the slave library. Actually, some libraries may need to be ignored. You can add the following configuration to the main library:

# 不同步哪些数据库
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
# 只同步哪些数据库,除此之外,其他不同步
binlog-do-db = game

Actual demonstration 

In order to facilitate the demonstration, I used [ Win10 to install multiple mysql instances ], the principle is the same

1. Download the latest decompressed version of mysql, here is mysql-8.0.22 

Unzip and make a copy. Create a new my.ini file respectively.

 

 

 

 my.ini content reference: pay attention to port modification

[mysqld]
# 设置3306端口
port=3307
# 设置mysql的安装目录# 切记此处一定要用双斜杠
basedir=D:\\DevPackTool\\InstallPack\\mysql1
# 设置mysql数据库的数据的存放目录
datadir=D:\\DevPackTool\\InstallPack\\mysql1\\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=UTF8MB4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
#开启binlog
log-bin=mysql-bin
replicate-do-db=test
server-id=3307  
[mysql]
# 设置mysql客户端默认字符集
default-character-set=UTF8MB4

[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3307
default-character-set=UTF8MB4

2. Create mysql

First mysql

CMD enters the directory: D:\DevPackTool\InstallPack\mysql1\bin, ( choose your own installation path ) requires administrator privileges to execute commands:

 

mysqld --initialize --console

The console will print out the password. Please remember the initialization password. If you forget to delete the root directory data folder, execute the above statement again.

Continue to execute the service creation command in the D:\DevPackTool\InstallPack\mysql1\bin path and name the service MySQL1:

mysqld --install mysql1

Enable service:

net start mysql1

 

Log in to mysql:

mysql -uroot -p
输入密码

 

change the password:

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

Second mysql

The principle is the same as above. After logging in, you should pay attention to adding the port number, because the default is 3306. 3306 is already occupied by MySQL1, and MySQL2 is 3307. You need to specify the port when logging in:

mysql -uroot -P3307 -p
输入密码
#注意大写-P3307

The configuration refers to the above, which needs to be configured on the main library and the slave library.

 

1. Configuration file configuration

Main library:

From the library: 

 

2. Start configuration

Main library configuration

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

CREATE USER 'repl_user'@'%' IDENTIFIED BY '123456'


SHOW SLAVE STATUS

USE test

SHOW MASTER STATUS 

 

 

Configuration from the library:


CHANGE MASTER TO
  MASTER_HOST='127.0.0.1',
  MASTER_USER='repl_user',
  MASTER_PASSWORD='123456',
  MASTER_PORT=3306,
  MASTER_LOG_FILE='mysql-bin.000002', 
  MASTER_LOG_POS=5905;
  
  START SLAVE;
  
  SHOW SLAVE STATUS

 

You can also view with the command line

 

3. Test synchronization effect:

 

Guess you like

Origin blog.csdn.net/Coder_Boy_/article/details/110950347