Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Docker installs and configures Mysql clusters on one server

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how Docker installs and configures Mysql clusters on a server". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Download an image of mysql5.6 from docker hub

Docker pull mysql:5.6

two。 Use mysql5.6 image to run 4 mysql services, distinguished by port number

Preliminary preparatory work

# create four directories on this machine to store the data, logs and configuration files mkdir / data/mysqlcd / data/mysqlmkdir mysql3307 mysql3308 mysql3309 mysql3310cd mysql3307mkdir conf logs datacp-r conf/ data/ logs/.. / mysql3308cp-r conf/ data/ logs/.. / mysql3309cp-r conf/ data/ logs/.. / mysql3310 of the four mysql services

Create and run 4 mysql containers, each server with a root password of liuhaizhuang

Cd / data/mysql/mysql3307docker run-p 3307 PWD/conf:/etc/mysql/conf.d 3306-- name mysql3307\-v $PWD/conf:/etc/mysql/conf.d\-v $PWD/logs:/logs\-v $PWD/data:/var/lib/mysql\-e MYSQL_ROOT_PASSWORD=liuhaizhuang\-d mysql:5.6

Cd / data/mysql/mysql3308docker run-p 3308 PWD/conf:/etc/mysql/conf.d 3306-- name mysql3308\-v $PWD/conf:/etc/mysql/conf.d\-v $PWD/logs:/logs\-v $PWD/data:/var/lib/mysql\-e MYSQL_ROOT_PASSWORD=liuhaizhuang\-d mysql:5.6

Cd / data/mysql/mysql3309docker run-p 3309 PWD/conf:/etc/mysql/conf.d 3306-- name mysql3309\-v $PWD/conf:/etc/mysql/conf.d\-v $PWD/logs:/logs\-v $PWD/data:/var/lib/mysql\-e MYSQL_ROOT_PASSWORD=liuhaizhuang\-d mysql:5.6

Cd / data/mysql/mysql3310docker run-p 3310 PWD/conf:/etc/mysql/conf.d 3306-- name mysql3310\-v $PWD/conf:/etc/mysql/conf.d\-v $PWD/logs:/logs\-v $PWD/data:/var/lib/mysql\-e MYSQL_ROOT_PASSWORD=liuhaizhuang\-d mysql:5.6

So far, all 4 mysql server containers have been created and started!

If you want to link these four mysql remotely, you need the firewall to turn on 3307, 3308, 3308, 3309, 3310.

# Edit iptablesvim / etc/sysconfig/iptables# and add the following 4 lines to exit and save-An INPUT-p tcp-m state-- state NEW-m tcp-- dport 3307-j ACCEPT-An INPUT-p tcp-m state-- state NEW-m tcp-- dport 3308-j ACCEPT-An INPUT-p tcp-m state-state NEW-m tcp-- dport 3309-j ACCEPT-An INPUT-p tcp-m state-state NEW-m tcp-dport 3310-j ACCEPT

Restart the firewall, and you can link to the 4 newly installed mysql remotely.

Systemctl restart iptables.service

It means it's all on the link! Next, you can configure master-slave replication.

3. Configure master-slave cluster

Master: mysql with port 3307

Slaves: mysql whose port is 3308, 3309, 3310.

Configure master

# change to the configuration file directory of mysql3307 cd / data/mysql/mysql3307/conf# add my.cnf file and add the following content touch my.cnfvim my.cnf

Server-id: unique in the same local area network

Log-bin: enable the binary log function, and you can choose the following name as you like.

Restart the container corresponding to mysql3307

Docker restart mysql3307

Create data synchronization users in the master database; grant synchronization users replication slave and replication client permissions; for master-slave data synchronization

Mysql > create user' slave_user'@'%' identified by 'liuhaizhuang';Query OK, 0 rows affectedmysql > grant replication slave,replication client on *. * to' slave_user'@'%';Query OK, 0 rows affectedmysql >

Configure the my.cnf file for slave

# switch to the mysql3308 configuration file directory cp / data/mysql/mysql3308/conftouch my.cnfvim my.cnf [mysqld] # ensure that the unique server-id=2# in the LAN is enabled in binary, in case master as other salve uses log-bin=mysql3308-bin# to configure relay log relay _ log=mysql3308-relay-bin# to make slave read-only, and only parameter values limit ordinary users. Root users can still add, delete and modify read_only=1.

Restart the container corresponding to mysql3308

Docker restart mysql3308

Next, configure mysql3309 and mysql3310 slave libraries by analogy

Link master and slave

Go to the mysql3307 command line, execute show master status; to see the values of the File and Position fields, and then use the

Mysql > show master status +-+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +- -+ | mysql3307-bin.000001 | 434 | | | +-+ 1 row in setmysql >

Go to each slave command line and execute the following command:

Change master to master_host='172.18.0.5', master_user='slave_user', master_password='liuhaizhuang', master_port=3306, master_log_file='mysql3307-bin.000001', master_log_pos= 434, master_connect_retry=30

Parameter description:

Master_host: the address of master, which refers to the independent ip of the container. You can query the container's ip via docker inspect-- format=' {{.NetworkSettings.IPAddress}} 'container name | Container id

[root@VM_0_10_centos ~] # docker inspect-- format=' {{.NetworkSettings.IPAddress}} 'mysql3307172.18.0.5 [root @ VM_0_10_centos ~] #

The port number of master_port:master, which refers to the port number of the container

Master_user: user for data synchronization

Master_password: password of the user used for synchronization

Master_log_file: specifies the log file from which slave starts copying data, that is, the value of the file field mentioned above

Master_log_pos: which position to read from, that is, the value of the position field mentioned above

Master_connect_retry: if the connection fails, the interval between retries (in seconds). Default is 60 seconds.

Mysql > change master to master_host='172.18.0.5', master_user='slave_user', master_password='liuhaizhuang', master_port=3306, master_log_file='mysql3307-bin.000001', master_log_pos= 434, master_connect_retry=30;Query OK, 0 rows affectedmysql >

Enter the slave command and use start slave; to start the master-slave replication process; the command to query the master-slave synchronization status is: show slave status

Test master-slave replication

See a new database in master, and then observe whether several other slave libraries automatically synchronize data

That's all for "how Docker installs and configures Mysql clusters on a server". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report