Docker installs Mysql, and builds master-slave replication

1. Search the mysql image and pull the specified version

 

docker search mysql actually went to https://hub.docker.com/ to search,

If you directly use the command to directly pull the name of the searched image, such as docker pull mysql, the latest version is downloaded.

If you want to install the specified version, you need to specify the version when pulling, first go to https://hub.docker.com/, search mysql, and view all the released versions

 

 Switch to the tag tag, you can see the various versions of mysql listed

 

I choose the 5.7 version here, the pull command is: docker pull mysql: 5.7

After the mysql image download is complete, you can check

 

2. Install the mysql image 

In https://hub.docker.com/_/mysql there are instructions on how to install the mysql image

 

I choose to introduce the above two types here:

1. Mount custom configuration method, this kind of benefit, we can configure mysql directly on the host machine

Use custom MySQL configuration file to start mysql 
By default, MySQL's startup configuration file is /etc/mysql/my.cnf, and any .cnf format file in the /etc/mysql/conf.d directory will be used The configuration items in this file replace the default configuration. 
Therefore, if you want to use a custom configuration, you can create a configuration file on the host machine, and then use the -v parameter when creating the container to mount the custom configuration to the / etc / mysql / conf of the mysql container by data volume . d directory. 
For example, if the /my/mysql/conf/config-file.cnf configuration file exists in the host machine , you can start the MySQL container in the following ways: 
Examples:
docker run --name mysql5.7 -p 3306:3306 -v /my/mysql/data:/var/lib/mysql -v /my/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

 

2. Direct installation command followed by parameters, this method is relatively simple and direct

 docker run --name mysql5.7_1 -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306  -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

I take the second way here to quickly install two mysql


#Install the first docker run --name mysql5.7_1 -e MYSQL_ROOT_PASSWORD = 123456 -p 3307: 3306 -d mysql: 5.7 --character-set-server = utf8mb4 --collation-server = utf8mb4_unicode_ci

#Install the second docker run --name mysql5.7_2 -e MYSQL_ROOT_PASSWORD = 123456 -p 3308 : 3306 -d mysql: 5.7 --character- set -server = utf8mb4 --collation-server = utf8mb4_unicode_ci

Check the installed container, you can see that the mysql status is up, running

 

You can use a command or client to connect to any one remotely to check whether it can be connected normally.

Use the Windows command line to connect to the test, login command: mysql -uroot -p123456 -h 192.168.220.102 -P 3307

Connect test with Navicat client

At this point, the installation is complete.

3. Build master-slave replication function

Configuration master and slave need to modify the mysql configuration file, you need to enter the mysql container to modify the configuration file

  • Configure Host

Here is the mysql_5.7_1 as the M host, first enter to configure it

By docker exec -it a1c7de8adce4 /bin/bashcommand Master into the interior of the container, you can also docker exec -it mysql_5.7_1 /bin/bashcommand to enter. a1c7de8adce4 is the id of the container, and mysql_5.7_1 is the name of the container.

[root@localhost ~]# docker exec -it a1c7de8adce4 /bin/bash

Continue to enter the mysql configuration directory

root@a1c7de8adce4:/# cd /etc/mysql

View configuration file ls -l 

 You will see that there are two configuration files my.cnf and mysql.cnf. The relationship between these two is that my.cnf is a soft link to mysql.cnf. So edit any one.

 But when we use vim, we found that it is not installed in the container, so if you want to use vim to edit, you need to install it first

 Use the apt-get install vimcommand to install vim, and the following problems will occur

 Execute apt-get update, and then execute again apt-get install vimto successfully install vim.

Then vim my.cnf, add the following configuration, save and exit.

[mysqld] #This line is very important, if my.cnf has this line, you don't need to write it again, if not, add it. 
server-id = 1 
#Server unique ID  
log-bin = mysql-bin #Enable binary log, master-slave copied data file binlog-do-db = mycat-ms 
#Database name to be synchronized binlog_format = STATEMEN #Set logbin format

To add: the above is to modify the mysql configuration file in the mysql container, you need to install vim.

In fact, you can completely copy the mysql configuration file from the container to the host machine, and then copy it back to the mysql container after the modification, which is more convenient to write. 

The specific steps are:

Since entering the mysql container, we found that the real configuration file is /etc/mysql/mysql.cnf, so we first copy this file on the host machine:

docker cp a1c7de8adce4:/etc/mysql/mysql.cnf /usr/local/

Then vim edited the copied mysql.cnf:

! includedir /etc/mysql/conf.d/ 
! includedir /etc/mysql/mysql.conf.d/ #Add the 


following configuration 
[mysqld] 
server-id = 1 
log-bin = mysql-bin 
binlog-do-db = mycat_m_s 
binlog_format = STATEMEN

After editing and saving, copy it back to the mysql container for overwriting

docker cp mysql.cnf a1c7de8adce4:/etc/mysql/

 

  • Configure slave

Enter slave mysql5.7_2 in the same way, modify my.cnf, add the following configuration, save and exit. Exit the container.

[mysqld] 
 server-id = 2 
 #Server unique ID relay-log = mysql-relay #Enable

 

Restart two mysql containers, command: docker restart container id or name

 

  • Log in to the host mysql5.7_1 and create an authorized account slave
#Execute the authorization command GRANT REPLICATION SLAVE ON *. * TO 'slave' @ '%' IDENTIFIED BY '123456' in the host MySQL 

 

After the execution, log in to Mater to check the user information: mysql> select user, host from user; see that the slave user has been successfully created.

 

  • Master and Slave establish a link

Log in to the Master and executeshow master status;查看主从复制需要的配置信息

 

 The values ​​of the File and Position fields will be used later when configuring the slave to copy the host information, so before the subsequent operations are completed, you need to ensure that the Master library cannot do any operations, otherwise the values ​​of the File and Position fields will change.

Log in to the slave and execute the link master configuration command

change master to master_host = 'Host IP address', 
master_port = port number, 
master_user = 'slave', 
master_password = '123456', 
master_log_file = 'mysql-bin. specific number', 
master_log_pos = specific value of location, 
master_connect_retry = 60;

-------------------------------------------------- -----------------------
Detailed explanation:

  master to master_host: Master's ip
  master_port: Master's port number, because it uses mysql in docker, so here refers to the host mapped to the port of the mysql container
  master_user: user for data synchronization
  master_password: for synchronization The password of the user
  master_log_file: Specifies the log file from which Slave starts to copy data, that is, the value of the File field mentioned above
  master_log_pos: The position from which to start reading, which is the value of the Position field mentioned above
  master_connect_retry: If the connection fails, Retry interval, the unit is seconds, the default is 60 seconds

 

The specific execution commands are as follows:

change master to master_host='192.168.220.102',
master_port=3307,
master_user='slave',
master_password='123456',
master_log_file='mysql-bin.000003',
master_log_pos=1821,
master_connect_retry=60;

Copy the above command to execute from the mysql command window.

View slave status mysql> show slave status \ G;

mysql> change master to master_host='192.168.220.102',
-> master_port=3307,
-> master_user='slave',
-> master_password='123456',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=1821,
-> master_connect_retry=60;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.220.102
Master_User: slave
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1821
Relay_Log_File: mysql-relay.000005
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1281
Relay_Log_Space: 523
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1236
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'log event entry exceeded max_allowed_packet; Increase max_allowed_packet on master; the first event 'mysql-bin.000003' at 1281, the last event read from './mysql-bin.000003' at 123, the last byte read from './mysql-bin.000003' at 1300.'
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 16d94e7b-7f17-11ea-bb5e-0242ac110003
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 200417 06:26:09
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

It can be seen that the two main threads SlaveIORunning and SlaveSQLRunning that the slave performs master-slave synchronization are No, and it should be yes normally. This is because we have not enabled the replication function from the server.

Use to start slaveenable master-slave replication , and then query the master-slave synchronization status again show slave status \G;.

 

 At this point, the master-slave replication is completed.

  •  Verify master-slave replication

 Screenshot below:

 

Guess you like

Origin www.cnblogs.com/tyhj-zxp/p/12719121.html