windows version of mysql replication environment from the main building

background

A recent study by Spring Aop database to read and write to achieve separation of functions.
Before writing the code, is first deployed to mysql environment, due to the separation of read and write, it is required to deploy at least two mysql instance, a master-slave, and the master can be automatically synchronized between the instances, because my native memory is not high, so we intend to build the main mysql directly on the windows from the examples (do not want to open a virtual machine), but this process has had some trouble, though eventually resolved, but also spent a lot of time. To avoid wasting time later on the same thing, but also the convenience of readers have been able to replicate the same scene, so I wrote this blog about the process set up to record the environment.

Environment Description

Local address: 127.0.0.1 (localhost)

mysql version: mysql-5.7.28-winx64

Main Library Service Name: master, port 3307

From the library service name: slave, port 3308

Install and configure the main library master

download

First, download mysql, directly to the official website to download the zip version of the installation package, proposed here to download the newer version, such as the author's version is 5.7, which is much larger online suggestion of God,

Unzip files and create a my.ini

Extract the installation package named folder for the master, into the folder, create an empty text called my.ini, and

Contents of the text is as follows:

[client]
# 端口号,默认是3306,同一个环境下不同的mysql实例端口号不能相同
port=3307
default-character-set=utf8

[mysqld] 
#主库配置
server_id=1
log_bin=master-bin
log_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=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER

# 开启查询缓存
explicit_defaults_for_timestamp=true

ps: contents of the address in the configuration directory to use the slash, not with a backslash, otherwise inferior installation services will be error can not find the directory.

Install the master library service

1, cmd administrator to run into the bin directory of the master,

2, an initialization commands:

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

If you like the following appears on the display to enhance the initialization is successful, the success will initialize the system automatically generates data folder, and generates the initial password,

3, after the initialization is complete, the installation service command:

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

master for the name of the service, - ". Service successfully installed" defaults-file is the path to ini file, there will be the successful

Note: If the installation fails, see if you do not run as administrator cmd.

Normally, after you install the master service we can start mysql, however here is a pit, that is, directly after mysql will complain, because we have less to configure a place that information in the master service registry.

So before unmodified registry, it is recommended not to execute a start command.

4, modify the registry

Press win + R, to open the box shells registry type regedit, master service found, the path is HKEY_LOCAL_MACHINE-> SYSTEM-> CurrentControlSet-> Services-> master , modified ImagePath is

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

The corresponding path on your own installation of the master database folder.

5, start the service

Still is launching the service in the bin directory, the command is net master start, the following prompt appears after a successful start:

6, enter mysql

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

Since this initial password is too boring, so we had better change passwords, change the statement as follows:

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

Such as password LZ set for 123456,

This success set the root password, and then we can use graphical tools such as Navicat to operate the database connection,

Installation from the library slave

Library from the installation step and the main libraries, only the configuration corresponding to my.ini modified.

[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/data
port=3308
character_set_server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER
#开启查询缓存
explicit_defaults_for_timestamp=true

Master-linked from the library

Both libraries are installed, since we can operate the library association between master and slave, to achieve replication between master and slave,

Main library to log in, enter show master status;

Can be found in the main library generates a binary file, which is a log file, it can achieve synchronization and the main library from the library association.

Log 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;

Not difficult to see above all the configuration information for the main library, run after a successful start slaveopen master-slave replication

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

Then the slave database also generate the same test,

In this way, the master-slave replication on the successful implementation.

It is worth noting, because both libraries just do a one-way association, if the data from the library to write, then, the main library is not synchronized. It can only be used to read data from the library, and the main library not only write, but also to read, of course, most cases are used to write data, read data are generally available from the library, which can effectively reduce the main library pressure, that is, we often say that the separate read and write.

Guess you like

Origin www.cnblogs.com/yeya/p/11878009.html