Dry goods! Build a MySQL master-slave replication environment sharing on Windows system

background

Recently, I am learning to use Spring Aop to realize the function of database read-write separation.

Before writing the code, the first thing to do is to deploy the MySQL environment. Because of the separation of reads and writes, at least two mysql instances need to be deployed, one master and one slave, and the master and slave instances can be automatically synchronized, because my native machine The memory is not high, so I plan to build a master-slave instance of mysql directly on windows (don't want to open a virtual machine), but some troubles were encountered in this process. Although it was solved in the end, it took a lot of time. In order to avoid wasting time on the same things in the future, and to make it easier for everyone to replicate the same scenes, let’s share the process of setting up the environment today.

Environmental description

Local address: 127.0.0.1(localhost)

mysql version: mysql-5.7.28-winx64

Main library service name: master, port 3307

Slave service name: slave, port 3308

Install and configure the master library master

download

The first is to download mysql, and go directly to the official website to download the zip version of the installation package. It is recommended to download a relatively new version, for example, the author’s version is 5.7. This is also the suggestion of many great gods on the Internet.

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

Unzip and create my.ini file

Unzip the installation package, name the folder master, enter the folder, and create an empty text named my.ini,

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

The content of the text is as follows:

[client]
# 端口号,默认是3306,同一个环境下不同的mysql实例端口号不能相同
port=3307
default-character-set=utf8
[mysqld] #主库配置server_id=1
log_bin=master-binlog_bin-index=master-bin.index# 设置为自己MYSQL的安装目录basedir=D:/software/mysql/master
# 设置为MYSQL的数据目录,data文件夹由mysql自动生成datadir=D:/software/mysql/master/data
port=3307
character_set_server=utf8sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER# 开启查询缓存explicit_defaults_for_timestamp=true

ps: The directory address in the configuration content should use a slash, not a backslash, otherwise the installation service will report an error that the directory cannot be found.

Install the service of the master library

1. Run as cmd administrator, enter the bin directory of master,

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

2. Execute the initialization command:

mysqld --initialize --user=mysql --console

If an upgrade similar to the following appears, the initialization is successful, and the system will automatically generate the data folder and generate the initial password if the initialization is successful.

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

3. After the initialization is complete, execute the command to install the service:

mysqld --install master --defaults-file="D:\software\mysql\master\my.ini" 

master is the name of the service, --defaults-file is the path of the ini file, and "Service successfully installed." means success

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

Note: If the installation fails, check to see if there is no administrator to run cmd.

Normally, we can start mysql after installing the master service. However, there is a pit, that is, an error will be reported after mysql directly, because we still have one less configuration, which is the information of the master service in the registry.

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

Therefore, it is recommended not to execute the startup command before modifying the registry.

4. Modify the registry

Press win+R, enter regedit in the pop-up box to open the registry, find the master service, the path is HKEY_LOCAL_MACHINE–>SYSTEM–>CurrentControlSet–>Services–>master , modify ImagePath to

D:\software\mysql\master\bin\mysqld --defaults-file=D:\software\mysql\master\my.ini master

The path corresponds to the master database folder you installed.

5. Start the service

Still execute the startup service in the bin directory, the command is net master start, and the following prompt will appear after successful startup:

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

6. Enter mysql

The command to enter mysql is: mysql -u root -p  , but because we changed the port number, the command should be changed to mysql -u root -P3307 -p , and then enter the initialization password just generated to enter mysql.

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

Since this initial password is too painful, we'd better change the password, the change statement is:

set password=password('新密码');

For example, the password set by LZ is 123456,

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

In this way, the password of the root user is successfully set, and then we can use graphical tools such as Navicat to operate and connect to the database.

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

Install slave from library

The installation steps of the slave library are the same as the main library, except that you can modify the corresponding configuration in my.ini.

[client]
port=3308
default-character-set=utf8
[mysqld] #从库配置server_id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
# 设置为自己MYSQL的安装目录 basedir=D:/software/mysql/slave# 设置为MYSQL的数据目录 datadir=D:/software/mysql/slave/dataport=3308
character_set_server=utf8sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER#开启查询缓存explicit_defaults_for_timestamp=true

Master-slave library to achieve association

After the two libraries are installed, we can operate the association between the master and slave libraries to realize the replication between the master and slave,

Log in to the main library first, enter show master status;

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

It can be found that a binary file is generated in the main library, which is a log file, and the synchronization with the main library can be realized by associating it with the slave library.

Log in from the library, execute the following command,

change master to master_host='127.0.0.1',master_port=3307,master_user='root',master_password='123456',master_log_file='master-bin.000001',master_log_pos=0;

It is not difficult to see that the above is the configuration information of the master library. After success, execute the command start slave to enable master-slave replication

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

After completion, we simply verify, create a database in the master library, the name is test,

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

Then the same database test is also generated in the slave,

Dry goods!  Build a MySQL master-slave replication environment sharing on Windows system

In this way, master-slave replication is successfully realized.

postscript

It is worth noting that, because the two libraries are only one-way related, if you write data to the slave library, the master library cannot be synchronized. Therefore, the slave library can only be used to read data, while the main library can both write and read. Of course, in most cases, it is used to write data. Reading data is generally obtained from the library, which can effectively reduce the main library’s Stress is what we often call separation of reading and writing.

If you like the article, please like it, comment and forward it, pay attention to the editor, the follow-up editor will bring a richer learning content update, I hope everyone will like it~~~

Guess you like

Origin blog.csdn.net/Java_msb666/article/details/108646826