Mysql native master-slave construction

1. Master-slave database preparation

Limited by resources, all of us may install multiple mysql databases on this machine to build a master-slave as a hands-on exercise

1. Copy the green version of mysql separately, and place them in different paths (if there are multiple servers, you must ensure 2 mysql of the same version)

2. Modify the port information, database path information, and data storage path information in the my.ini file

3. Enter the bin directory respectively, and open the cmd command window with administrator privileges

4. Use mysqld to install the service: mysqld --install service name --defaults-file=path\my.ini

5. Use net start service name to start the service

 If the startup service stops immediately, go to the installation directory and execute the initialization mysqld --initialize, and restart the service. Another possible situation is that the data directory has not been copied together, and this needs to be copied.

After the master-slave database service is installed, the next step is to configure the master-slave:

Two, master library configuration

1. Edit the mysql configuration file, my.int (if it is linux, it is my.cnf)

mysql configuration file

The main thing is to add the section of configuring master-slave synchronization

# Configure master-slave synchronization
# Node ID, ensure unique
server-id=1
# Open binlog binary
log log-bin=mysql-bin  
log-bin-index=master-bin.index #Specify the index file of binlog file
max_binlog_size=500M # The maximum size of binlog will be created if it exceeds, and the same statement will not cross files
binlog_cache_size=128K #binlog cache size, a cache will be created when executing SQL
binlog-do-db=sharp #Open binlog database
binlog-ignore-db=mysql #No Opened database log-slave-updates#Update the binlog when copying the master database data from the database. This parameter is very important when the slave
database is used as the master database of other slave databases . #binlog log format, the default is statement, it is recommended to use mixed

3. Slave library configuration

slave configuration

The main configuration

server-id=2
#The database that needs to be synchronized
replicate_do_db=sharp
#The database that the master-slave replication ignores
replicate_ignore_db=mysql

Note: The server-id of each library must be different.

After the configuration files of the master-slave database are configured, we restart the next two database services.

Four, master library settings

1. On the main library, set up an account for synchronization, and grant REPLICATION SLAVE permissions, and finally refresh the permissions:

CREATE USER 'master'@'%' IDENTIFIED WITH mysql_native_password BY 'tinalucky';

GRANT REPLICATION SLAVE ON TO 'master'@'%';

FLUSH PRIVILEGES;

2. View the status of the master:

SHOW master status;

From here, you can get the current binary log name and offset value of the main library. The year check is used for setting the information of accessing the master from the library in the following steps, and the purpose is to restore data from this log file and from this location.

What I got on my computer is file:mysql-bin.000010, position:829.

Next, start the setting of the slave library

Five, slave library settings

The setting of the slave library is connected to the information of the master library,


    -> MASTER_HOST='localhost', #Main library machine IP
    -> MASTER_PORT=3308, #Main library port-
    > MASTER_USER='master', #Account to access the main library->
    MASTER_PASSWORD='tinalucky', #Access The password of the main library
    -> MASTER_LOG_FILE='mysql-bin.000010', #The log file of the main library
    -> MASTER_LOG_POS=829; #The offset of the log start synchronization

After executing the above configuration, the master-slave is basically set up completely. Check the status of the slave library:

show slave status\G;

From the above information, we can see that our configuration is effective.

Next, we can test and create a new table under the sharp database synchronized by the main library:

Let's look at the slave library, which has been synchronized:

Inserting records in the master library will also be synchronized to the slave library. It can be verified that the construction of the master-slave library is successful.

If the library of the copied database may have the exception of master and slave have equal MySQL server UUIDs, it is because the UUIDS of the master library and the slave library must be different. The master and slave mysql UUIDS are the same, and Slave_IO cannot be started. The error message is as follows:

The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Solution
: Modify the value of uuid in the auto.cnf file under the mysql data directory so that the two mysqls are different. Yes, restart the mysql service after modification.

Guess you like

Origin blog.csdn.net/tinalucky/article/details/116709046