当前位置:网站首页>Mysql database | build master-slave instances of mysql-8.0 or above based on docker

Mysql database | build master-slave instances of mysql-8.0 or above based on docker

2022-07-05 01:32:00 PivotalCloud

Mysql Overview of master-slave environment

Mysql Build a cluster master-slave environment

This time, we use Docker Deployment cluster , And first, configure the master-slave instance .

1.Mysql-Cluster Cluster service planning

2.Mysql-Cluster Cluster prerequisites

establish Mysql-Cluster The main configuration directories of the cluster are as follows :
Mysql-Cluster colony

2.1 Mysql-Master node [3308]

establish Mysql-Slave The main configuration file directory and configuration files of the node are as follows :

Mysql-Master node

[1].mysql.cnf The configuration file :

# Copyright (c) 2015, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

#
# The MySQL  Client configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysql]
skip-grant-tables
skip-host-cache
skip-name-resolve
default_authentication_plugin=mysql_native_password
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
open_files_limit = 3072
back_log=200
max_connections = 20
max_connect_errors = 10
table_open_cache = 128
external-locking = FALSE
max_allowed_packet = 4M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 128
tmp_table_size = 16M
max_heap_table_size = 8M
slow_query_log = 1
long_query_time = 0.05
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 512M
key_buffer_size = 8M
read_buffer_size = 1M
read_rnd_buffer_size = 8M
bulk_insert_buffer_size = 32M
default-storage-engine=InnoDB
log-bin=mysql-bin
binlog_format=row
server-id = 1
transaction_isolation = REPEATABLE-READ
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
default-storage-engine=INNODB
innodb_thread_concurrency=12
innodb_flush_log_at_trx_commit=1
innodb_max_dirty_pages_pct=30
innodb_io_capacity=10000
innodb_log_buffer_size=1024M
innodb_flush_method=O_DIRECT
innodb_flush_neighbors=0
innodb_log_files_in_group=4
innodb_compression_level=0
innodb_file_per_table=1
innodb_compression_pad_pct_max=50
innodb_buffer_pool_size=256M
interactive_timeout=500
wait_timeout=500
innodb_log_file_size=256M
lower_case_table_names=1
lower_case_file_system=ON
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[mysqld]
default_authentication_plugin=mysql_native_password
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
skip-grant-tables
skip-host-cache
skip-name-resolve
skip-grant-tables
init_connect='SET NAMES utf8mb4'
open_files_limit = 3072
back_log=200
max_connections = 20
max_connect_errors = 10
table_open_cache = 128
external-locking = FALSE
max_allowed_packet = 4M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 128
tmp_table_size = 16M
max_heap_table_size = 8M
slow_query_log = 1
long_query_time = 0.05
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 512M
key_buffer_size = 8M
read_buffer_size = 1M
read_rnd_buffer_size = 8M
bulk_insert_buffer_size = 32M
default-storage-engine=InnoDB
log-bin=mysql-bin
binlog_format=row
server-id = 1
transaction_isolation = REPEATABLE-READ
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
default-storage-engine=INNODB
innodb_thread_concurrency=12
innodb_flush_log_at_trx_commit=1
innodb_max_dirty_pages_pct=30
innodb_io_capacity=10000
innodb_log_buffer_size=1024M
innodb_flush_method=O_DIRECT
innodb_flush_neighbors=0
innodb_log_files_in_group=4
innodb_compression_level=0
innodb_file_per_table=1
innodb_compression_pad_pct_max=50
innodb_buffer_pool_size=256M
interactive_timeout=500
wait_timeout=500
innodb_log_file_size=256M
lower_case_table_names=1
lower_case_file_system=ON
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[mysqldump]
quick
quote-names
max_allowed_packet=500M
[client]
default-character-set=utf8mb4
2.2 Mysql-Slave node [3309]

establish Mysql-Slave The main configuration file directory and configuration files of the node are as follows :
Mysql-Slave

[1].mysql.cnf The configuration file :

 # Copyright (c) 2015, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

#
# The MySQL  Client configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysql]
skip-grant-tables
skip-host-cache
skip-name-resolve
default_authentication_plugin=mysql_native_password
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
open_files_limit = 3072
back_log=200
max_connections = 20
max_connect_errors = 10
table_open_cache = 128
external-locking = FALSE
max_allowed_packet = 4M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 128
tmp_table_size = 16M
max_heap_table_size = 8M
slow_query_log = 1
long_query_time = 0.05
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 512M
key_buffer_size = 8M
read_buffer_size = 1M
read_rnd_buffer_size = 8M
bulk_insert_buffer_size = 32M
default-storage-engine=InnoDB
log-bin=mysql-bin
binlog_format=row
server-id = 2
relay-log = mysql-relay
transaction_isolation = REPEATABLE-READ
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
default-storage-engine=INNODB
innodb_thread_concurrency=12
innodb_flush_log_at_trx_commit=1
innodb_max_dirty_pages_pct=30
innodb_io_capacity=10000
innodb_log_buffer_size=1024M
innodb_flush_method=O_DIRECT
innodb_flush_neighbors=0
innodb_log_files_in_group=4
innodb_compression_level=0
innodb_file_per_table=1
innodb_compression_pad_pct_max=50
innodb_buffer_pool_size=256M
interactive_timeout=500
wait_timeout=500
innodb_log_file_size=256M
lower_case_table_names=1
lower_case_file_system=ON
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[mysqld]
default_authentication_plugin=mysql_native_password
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
skip-grant-tables
skip-host-cache
skip-name-resolve
skip-grant-tables
init_connect='SET NAMES utf8mb4'
open_files_limit = 3072
back_log=200
max_connections = 20
max_connect_errors = 10
table_open_cache = 128
external-locking = FALSE
max_allowed_packet = 4M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 128
tmp_table_size = 16M
max_heap_table_size = 8M
slow_query_log = 1
long_query_time = 0.05
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 512M
key_buffer_size = 8M
read_buffer_size = 1M
read_rnd_buffer_size = 8M
bulk_insert_buffer_size = 32M
default-storage-engine=InnoDB
log-bin=mysql-bin
binlog_format=row
server-id = 2
relay-log = mysql-relay
transaction_isolation = REPEATABLE-READ
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
default-storage-engine=INNODB
innodb_thread_concurrency=12
innodb_flush_log_at_trx_commit=1
innodb_max_dirty_pages_pct=30
innodb_io_capacity=10000
innodb_log_buffer_size=1024M
innodb_flush_method=O_DIRECT
innodb_flush_neighbors=0
innodb_log_files_in_group=4
innodb_compression_level=0
innodb_file_per_table=1
innodb_compression_pad_pct_max=50
innodb_buffer_pool_size=256M
interactive_timeout=500
wait_timeout=500
innodb_log_file_size=256M
lower_case_table_names=1
lower_case_file_system=ON
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
[mysqldump]
quick
quote-names
max_allowed_packet=500M
[client]
default-character-set=utf8mb4
3.Mysql-Cluster colony Docker Deployment scripts
3.1 Mysql-Master node [3308]
docker run -itd -p 3308:3306 --restart always  --privileged=true --name mysql-master --network-alias mysql-master  --hostname mysql-master -v /docker/mysql-cluster/master/conf/my.cnf:/etc/mysql/my.cnf -v /docker/mysql-cluster/master/conf/conf.d/:/etc/mysql/conf/conf.d/ -v /docker/mysql-cluster/master/data:/var/lib/mysql  -e "JAVA_OPTS=-server -Xms512m -Xmx512m -Xmn256m -Duser.home=/opt -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:+AlwaysPreTouch -XX:-UseBiasedLocking" -e TZ="Asia/Shanghai" -e [email protected]  mysql:latest
3.2 Mysql-Slave node [3309]
docker run -itd -p 3309:3306 --restart always  --privileged=true --name mysql-slave --network-alias mysql-slave  --hostname mysql-slave -v /docker/mysql-cluster/slave/conf/my.cnf:/etc/mysql/my.cnf -v /docker/mysql-cluster/slave/conf/conf.d/:/etc/mysql/conf/conf.d/ -v /docker/mysql-cluster/slave/data:/var/lib/mysql  -e "JAVA_OPTS=-server -Xms512m -Xmx512m -Xmn256m -Duser.home=/opt -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:+AlwaysPreTouch -XX:-UseBiasedLocking" -e TZ="Asia/Shanghai" -e [email protected]  mysql:latest
4.Mysql-Cluster colony Docker Execute deployment
4.1 Mysql-Master node [3308]
Marklin:~ marklin$ docker run -itd -p 3308:3306 --restart always  --privileged=true --name mysql-master --network-alias mysql-master  --hostname mysql-master -v /docker/mysql-cluster/master/conf/my.cnf:/etc/mysql/my.cnf -v /docker/mysql-cluster/master/conf/conf.d/:/etc/mysql/conf/conf.d/ -v /docker/mysql-cluster/master/data:/var/lib/mysql  -e "JAVA_OPTS=-server -Xms512m -Xmx512m -Xmn256m -Duser.home=/opt -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:+AlwaysPreTouch -XX:-UseBiasedLocking" -e TZ="Asia/Shanghai" -e [email protected]  mysql:latest
d99ac84d4e11dd3835b89ff410a488c8041c43ec67aa4bccb1679edf57b08539
Marklin:~ marklin$
4.2 Mysql-Slave node [3309]
Marklin:~ marklin$ docker run -itd -p 3309:3306 --restart always  --privileged=true --name mysql-slave --network-alias mysql-slave  --hostname mysql-slave -v /docker/mysql-cluster/slave/conf/my.cnf:/etc/mysql/my.cnf -v /docker/mysql-cluster/slave/conf/conf.d/:/etc/mysql/conf/conf.d/ -v /docker/mysql-cluster/slave/data:/var/lib/mysql  -e "JAVA_OPTS=-server -Xms512m -Xmx512m -Xmn256m -Duser.home=/opt -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:+AlwaysPreTouch -XX:-UseBiasedLocking" -e TZ="Asia/Shanghai" -e [email protected]  mysql:latest
fe3eb3b02fc0cf9e187a8158b58bb5832d70a4462b583d4ba4cf4297ff7dd8d3
Marklin:~ marklin$
4.3 Mysql-Cluster Cluster deployment and test results :

Mysql-Cluster Cluster deployment :

Mysql-Cluster colony

test Navicat The client connection data is as follows :

Navicat client

5.Mysql-Cluster Subsequent configuration of cluster master-slave nodes
5.1 Mysql-Cluster Cluster nodes IP Address

1.mysql-master node IPAddress: docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-master

Marklin:~ marklin$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-master
172.17.0.13
Marklin:~ marklin$

2. see mysql-slave Node IPAddress: docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-slave

Marklin:~ marklin$ docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-slave
172.17.0.14
Marklin:~ marklin$
5.2 Mysql-Cluster Cluster master-slave user authorization configuration

Get into Docker Two ways of backstage :
1. Input... Through the terminal docker ps View the container process , Finally, execute docker exec -it mysql Container name /id bash
perform docker ps:

Marklin:~ marklin$ docker ps
CONTAINER ID   IMAGE                                      COMMAND                  CREATED        STATUS         PORTS                                                                                              NAMES
fe3eb3b02fc0   mysql:latest                               "docker-entrypoint.s…"   18 hours ago   Up 2 hours     33060/tcp, 0.0.0.0:3309->3306/tcp                                                                  mysql-slave
d99ac84d4e11   mysql:latest                               "docker-entrypoint.s…"   18 hours ago   Up 2 hours     33060/tcp, 0.0.0.0:3308->3306/tcp                                                                  mysql-master
59030d0bff07   docker/dev-environments-default:stable-1   "sleep infinity"         19 hours ago   Up 4 minutes                                                                                                      mysql-cluster
d0f4142d8781   dubbo-admin:latest                         "tini -- /usr/local/…"   13 days ago    Up 2 hours     0.0.0.0:8082->8080/tcp                                                                             dubbo-monitor
c91f1bd81cf9   nacos-server:latest                        "bin/docker-startup.…"   13 days ago    Up 2 hours     0.0.0.0:8848->8848/tcp                                                                             nacos-server
fefa57c84ee1   dubbo-admin:latest                         "tini -- /usr/local/…"   13 days ago    Up 2 hours     0.0.0.0:8080->8080/tcp                                                                             dubbo-server
f3b3f54acfc3   zookeeper:latest                           "/docker-entrypoint.…"   13 days ago    Up 2 hours     0.0.0.0:2181->2181/tcp, 0.0.0.0:2888->2888/tcp, 0.0.0.0:3888->3888/tcp, 8080/tcp                   zookeeper-server
1f323ef0299f   mysql:5.7.36                               "docker-entrypoint.s…"   5 weeks ago    Up 2 hours     33060/tcp, 0.0.0.0:3307->3306/tcp                                                                  mysql5.7-server
a53b9713927f   rabbitmq:latest                            "docker-entrypoint.s…"   5 weeks ago    Up 2 hours     0.0.0.0:5671-5672->5671-5672/tcp, 4369/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbitmq-server
bd7637c345b6   mysql:8.0.21                               "docker-entrypoint.s…"   5 weeks ago    Up 2 hours     0.0.0.0:3306->3306/tcp, 33060/tcp                                                                  mysql-server
87aa352ecead   redis:latest                               "docker-entrypoint.s…"   5 weeks ago    Up 2 hours     0.0.0.0:6379->6379/tcp                                                                             redis-server
6af30979d57c   minio:latest                               "/usr/bin/docker-ent…"   5 weeks ago    Up 2 hours     0.0.0.0:8081->8081/tcp, 0.0.0.0:9000->9000/tcp                                                     minio-server
51ffd8d090ba   portainer-ce:latest                        "/portainer"             5 weeks ago    Up 2 hours     8000/tcp, 9443/tcp, 0.0.0.0:9999->9000/tcp                                                         portainer-server
Marklin:~ marklin$ 

Get into mysql Containers : docker exec -it d99ac84d4e11 bash

Marklin:~ marklin$ docker exec -it d99ac84d4e11 bash
[email protected]:/#

2. adopt Docker Visual background entry :
Click on Consle:

Click on Conect:

Enter into mysql Containers :

Sign in mysql client :mysql -u root -p

[email protected]:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

As shown in the figure :

Create users and authorize :

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '[email protected]';
Query OK, 0 rows affected (0.10 sec)

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '[email protected]';
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '[email protected]';
Query OK, 0 rows affected (0.06 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql>

As shown in the figure :

see master state :show master status; , Record the binary file name and location

mysql> show variables like '%server%';
+---------------------------------+--------------------------------------+
| Variable_name                   | Value                                |
+---------------------------------+--------------------------------------+
| character_set_server            | utf8mb4                              |
| collation_server                | utf8mb4_0900_ai_ci                   |
| immediate_server_version        | 999999                               |
| innodb_dedicated_server         | OFF                                  |
| innodb_ft_server_stopword_table |                                      |
| original_server_version         | 999999                               |
| server_id                       | 1                                    |
| server_id_bits                  | 32                                   |
| server_uuid                     | 9a675ceb-64d5-11ec-b628-0242ac11000d |
+---------------------------------+--------------------------------------+
9 rows in set (0.00 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000003 |     1400 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

As shown in the figure :

Copyright notice : This article is an original blog article , Follow the relevant copyright agreement , If you reprint or share, please attach the original source link and link source .

原网站

版权声明
本文为[PivotalCloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141028297698.html