Super detailed linux (ubuntu) teaches you how to build MySQL 8.0 master-slave library

Table of contents

foreword

(1) Environment configuration

(2) Attach the command to install mysql on ubuntu

 (3) Main library database configuration

(a) Modify the main library environment configuration

(b) Modify the remote operation configuration of the main library

 (c) Create a slave database account password

(d) Check account authorization status

(e) View the status of the main library 

(f) List of possible errors in account creation

(4) Slave database configuration

(a) Modify the slave library environment configuration

(b) Create a mysql account password

(c) Check account authorization status

(d) Start the copy command from the repository

1 Create a copy command configuration

2 copy command

 3 Check the replication status

(e) Copy error list from repository

1 The account created by the main library cannot log in

2 The operation of the main library cannot be copied from the library


foreword

        When developing a project, you will always encounter the design database, and how to design a high-concurrency database is not only to improve the configuration of a single database, but also to design many databases, such as the master-slave database mechanism, distributed database, and so on. This article mainly explains the construction of the master-slave library.

        Master-slave library, master library (reading and writing), and slave library (reading), separate read and write operations, reduce the pressure on the main library, and further protect the purity of the data in the main library, so as to improve the efficiency of database query.

(1) Environment configuration

(a)linux:ubuntu 22.04 LTS 64位;

(b)mysql:mysql 8.0.31

(2) Attach the command to install mysql on ubuntu

apt-get install mysql-server // Install the database server

apt-get install mysql-client // install the database client

 (3) Main library database configuration

(a) Modify the main library environment configuration

Enter the mysql environment to configure mysql.cnf

vim /etc/mysql/mysql.conf.d/mysql.cnf

# 添加以下环境配置
[mysqld]

#[必须]主服务器唯一ID
server-id=1

#[必须]启用二进制日志,无后缀的文件名。也可以是本地的路径/log/bin-log
log-bin=bin-log

#[可选]设置需要复制的数据库名称,默认全部记录。
binlog-do-db=master_db

#[可选]设置不要复制的数据库
binlog-ignore-db=test

#[可选] 0(默认)表示读写(主库),1表示只读(从库)
read-only=0

#设置日志文件保留的时长,单位是秒
binlog_expire_logs_seconds=6000

#控制单个二进制日志大小。此参数的最大和默认值是1GB
max_binlog_size=200M

#[可选]设置binlog格式(STATEMENT是基于sql语句的复制,ROW是基于行的复制,MIXED是混合模式)
binlog_format=STATEMENT

(b) Modify the remote operation configuration of the main library

Enter the database environment to configure mysqld.cnf

vim /etc/mysql/mysql.conf.d/mysqld.cnf

bind-address = 127.0.0.1 // This means that it can only be operated locally

bind-address = 0.0.0.0 // This means that all IP addresses can be operated

 After the environment configuration is modified, use the following command to restart the database server

systemctl restart mysql.service

 (c) Create a slave database account password

(1) Enter the mysql command line interface

mysql -u account -p password

(2) Create slave database operation account password slave2 

create user 'slave2'@'%' identified by '111111';

flush privileges;

PS: When the host is 'localhost' and '127.0.0.1', only local login is allowed, while the host is '%' or 'IP address', remote account login is allowed

(3) Modify the host command

use mysql

update user set host='localhost' where user = 'slave2';

flush privileges;

(4) Grant permissions to the slave1 account

grant replication slave on master_db.* to 'slave2'@'%';

flush privileges;

(d) Check account authorization status

 show grants for 'slave2'@'%';

slave2 authorization

(e) View the status of the main library 

show master status;

Main library status

Remember to record the file name and binlog log offset, which are necessary parameters in the subsequent creation and copy command from the library!

(f) List of possible errors in account creation

grant replication slave on *.* to 'slave2'@'%' identified by '111111';

After mysql 8, authorization may fail here, and an error will be prompted:

  1. Misspelled '... near identified by ...', so cancel identified by '111111'
  2. No right to create '... no allow to create ...', this is because the flush privileges are not executed after the account is created.

After you get here, you can log out of your account and try it out.

(4) Slave database configuration

(a) Modify the slave library environment configuration

Enter the mysql environment configuration file

vim /etc/mysql/mysql.conf.d/mysql.cnf

# 添加以下配置
[mysqld]
#[必须]从服务器唯一ID
server-id=2

#[可选]启用中继日志
relay-log=mysql-relay

#[可选] 0(默认)表示读写(主机),1表示只读(从机)
read-only=1

#[可选] 选择复制的数据库,不填则默认复制主库授权可以复制的数据库
replicate-do-db=master_db

#[可选] 表示不随着数据库启动而启动复制
skip-slave-start

After the environment configuration is modified, use the following command to restart the database server

systemctl restart mysql.service

(b) Create a mysql account password

 (1) Enter the mysql command line interface

mysql -u account -p password

(2) Create an operation account password rdonly

create user 'rdonly'@'localhost' identified by '111111';

flush privileges;

(3) Grant permissions to the rdonly account, and only grant select permissions

grant select on master_db.* to 'rdonly'@'%';

flush privileges;

PS: Only the select permission is granted, which can avoid data inconsistency between the master and slave databases due to modification of the slave database data, which will cause the next replication to fail

(c) Check account authorization status

(d) Start the copy command from the repository

1 Create a copy command configuration

change master to 
master_host = '主库IP地址',
master_port = 主库端口,
master_user = 'slave2',
master_password = '111111',
master_log_file = 'bin-log.000005',
master_log_pos = 997;

2 copy command

start slave     // ​​start copy command from library

stop slave     // ​​stop copy command from library

reset slave    // ​​Reset the copy command from the library

 3 Check the replication status

show slave status\G;

slave status

 In the figure above, if the replication command has been started successfully, then both slave_io_running and slave_sql_running are yes .

(e) Copy error list from repository

Show wrong field

 If the copy does not take effect, you can refer to the parameters in the above picture for details. I also encountered several problems:

1 The account created by the main library cannot log in

  1. Check whether the server security group port number of the main library is open;
  2. Check whether the server IP address of the main library is restricted;
  3. Check whether the environment configuration of the main library restricts local operations only, see (3)-(b) for details;
  4. Check whether the main library firewall restricts the port number;
  5. Check whether there is a problem with the account permissions of the master library, and the replication permissions can be copied to the slave library;
  6. Check whether the above steps are incorrect.

2 The operation of the main library cannot be copied from the library

  1. The library, tables, and data records of the master library already exist, but the slave library does not have the corresponding library, tables, and data records, so the replication fails.

The master-slave library mechanism, as well as semi-synchronous and enhanced semi-synchronous modes can improve replication efficiency and further improve the operating efficiency of the main library. If you are interested, you can search by yourself, and I will update it when I build it further.

Guess you like

Origin blog.csdn.net/qq_15855921/article/details/130926760