1. Mysql5.7.28 realizes master-slave synchronization

Mysql5.7.28 master-slave synchronization implementation

table of Contents

1. Mysql download and install

  1. There are two types of mysql downloads, one is the zip file, the other is the
    official version 5.7 zip file download page of the
    msi installer . The official version 5.7 msi installer download page .

    I am using the 5.7.28 version of the msi installer here. The following Baidu cloud disk can be downloaded directly (it is recommended to use Baidu cloud directly)
    5.7.28 Baidu cloud disk version msi installer .
    Extract code: qj25
    will not install Little friends, see here
    attached to the installation tutorial link of the great god, and I will do it .

2. Master-slave synchronization realization

1. After installation, find the location of the installation file of your choice
Installation file location

2. Copy a copy of the installation file to the location you specify for unified management
Insert picture description here
. 3. To configure the main library first, you need to modify the mysql installation file master/ my.ini configuration file we copied.
Insert picture description here
Note: the my.ini configuration file of the MySQL version before 5.7.28 The configuration method is slightly different. If you need to see the configuration of other articles,

#
[mysqld]
# 端口配置
port=3309  
# bin-log日志开启  
log-bin=mysql-bin

# Path to the database root  配置的路径为: 拷贝的安装文件下/Data 
datadir="F:/javafile/mysqlTest/master/Data"
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

# General and Slow logging. 日志的输出路径同理修改为当前路径
log-output=FILE

general-log=1

general_log_file="F:/javafile/mysqlTest/master/master.log"

slow-query-log=1

slow_query_log_file="F:/javafile/mysqlTest/master/masterquery.log"

long_query_time=10

# Error Logging.
log-error="F:/javafile/mysqlTest/master/mastererror.log"
#server 编号一定要加上
server-id=1

The master configuration file is modified, remember to save

4. The next step is the installation and startup of the master:
copy the installation command that comes with my.ini and slightly modify it

Insert picture description here

#把路径修改为自己master安装文件my.ini的路径 方便等一下粘贴
mysqld --install master --defaults-file="F:\javafile\mysqlTest\master\my.ini"

Administrator cmd to open the command-line
Insert picture description here
fight to open command line cd to the next master files in the bin directory of the installation command does not see below

C:\Users\dev>f:

F:\cd /javafile/mysqlTest/master/bin
# master 为自定义的服务名称 可以自行替换  --defaults-file=这里为你自己my.ini的路径
F:\javafile\mysqlTest\master\bin>mysqld --install master --defaults-file="F:\javafile\mysqlTest\master\my.ini"


After the execution, this error is reported to prove that you did not run cmd as an administrator. Listen to me, run cmd
Insert picture description here install/remove of the service denied as an administrator. The permissions are not enough to run as an administrator.

After the installation is successful, the system prompts
Insert picture description here

#如果弄错了想重新安装 先执行下列命令将服务删除 再重新执行安装命令
mysqld --remove master --defaults-file="F:\javafile\mysqlTest\master\my.ini"

#安装完毕后输入:net start master 启动服务 master--为你自己起的服务名称
net start  master 
#停掉服务
net stop master

You can also start and close the operation service in the task manager
Insert picture description hereand check whether the bin-log file is generated after the installation is complete.
Insert picture description here

Start successfully,

open your database visualization tool and connect and the link is successful.
Insert picture description here

6. Install
and copy the master file directly from the database slave
Insert picture description here. Just like the master, you only need to modify the my.ini configuration file in the slave file.

#
[mysqld]
# 端口配置
port=3310  
# bin-log日志开启  
log-bin=mysql-bin
# Path to the database root  配置的路径为: 拷贝的安装文件下/Data 
datadir="F:/javafile/mysqlTest/slave/Data"
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

# General and Slow logging. 日志的输出路径同理修改为当前路径
log-output=FILE

general-log=1

general_log_file="F:/javafile/mysqlTest/slave/slave.log"

slow-query-log=1

slow_query_log_file="F:/javafile/mysqlTest/slave/slavequery.log"

long_query_time=10

# Error Logging.
log-error="F:/javafile/mysqlTest/slave/mastererror.log"
#server 编号一定要加上
server-id=10

You can configure multiple slave servers in the same way.
Modify the configuration file my.ini of the slave server to modify the server-id to server-id = 10, and make sure that this ID is not used by other MySQL services.

7. After the modification, install the slave slave library in the same way as the master above and start the connection. 7. After the master-slave service is ready, we will start to configure the master-slave synchronization

(1) Create an account and permission to authorize the use of the savle server on the master server master

#最后一段具体填充: '用户名'@'自己的IP地址' IDENTIFIED by '密码';
grant REPLICATION SLAVE ON *.* TO 'lvtest'@'192.113.151.112' IDENTIFIED by '123456';
#常看master状态
show master status

Check the ip method Insert picture description here(2) I directly open the database visualization tool to operate and open the master server master
Insert picture description here
(3) After the execution is completed, open the slave server connection in the visualization tool

change master to
master_host='192.**',   # master主服务器的 IP地址
master_user='slaveuser1',   #master数据库通过GRANT授权的账号也就是刚刚在master授权的那个的用户
master_password='123456',         #master数据库通过GRANT授权的密码 也就是刚刚在master授权的那个的用户密码
master_port=3309,              # 这里为master服务器的端口
master_log_file='mysql-bin.000004', #master数据库中通过show master status显示的File名称 上张图片有提到
master_log_pos=546       #master数据库的通过show master status显示的Position的值 上张图片有提到
#重新启动slave服务
start slave 
stop master
#查看slave状态 查看是否发生关系
show slave status 
show variables like 'server_id'

#查看当前链接server_id 
show variables like 'server_id'
#修改服务 server_id
set global server_id=2

Remember to stop slave before executing the statement, otherwise the following error will occur
Insert picture description here

(4) Successful examples
Insert picture description hereInsert picture description here
check the Slave_IO_Running status and find that it is NO, and the solution is as follows
Insert picture description here

(5) Create a new table in the master database to check whether the
Insert picture description here
synchronization is successful
Insert picture description here

You can also check the synchronization through the log file generated in the installation file.
Insert picture description here
Insert a piece of data to
Insert picture description hereview the slave.log in the slave installation directory. The master-slave synchronization is successful.
Insert picture description here

The configuration of multiple slave libraries is the same. They are all configured using accounts created in the main library. Note that the uniqueness of server_id

This article is only a rough set of Mysql master-slave synchronization, with limited knowledge, please predecessors for advice. At the same time, I take this blog post to share my learning experience, and to start a discussion.

Guess you like

Origin blog.csdn.net/weixin_45584768/article/details/113250131