當(dāng)前位置:首頁 > IT技術(shù) > 數(shù)據(jù)庫 > 正文

Mysql數(shù)據(jù)庫 | 基于Docker搭建Mysql-8.0以上版本主從實(shí)例實(shí)戰(zhàn)
2022-02-14 10:46:20

Mysql主從環(huán)境概述

Mysql集群主從環(huán)境搭建

本次采用Docker部署集群,并且首先主從實(shí)例配置。

1.Mysql-Cluster集群服務(wù)規(guī)劃

2.Mysql-Cluster集群先決條件

創(chuàng)建 Mysql-Cluster集群主要配置目錄如下:
Mysql-Cluster集群

2.1 Mysql-Master節(jié)點(diǎn)[3308]

創(chuàng)建 Mysql-Slave節(jié)點(diǎn)主要配置文件目錄以及配置文件如下:

Mysql-Master節(jié)點(diǎn)

[1].mysql.cnf配置文件:

# 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節(jié)點(diǎn)[3309]

創(chuàng)建 Mysql-Slave節(jié)點(diǎn)主要配置文件目錄以及配置文件如下:
Mysql-Slave

[1].mysql.cnf配置文件:

 # 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集群Docker部署腳本
3.1 Mysql-Master節(jié)點(diǎn)[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 MYSQL_ROOT_PASSWORD=PivotalCloud999@Love  mysql:latest
3.2 Mysql-Slave節(jié)點(diǎn)[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 MYSQL_ROOT_PASSWORD=PivotalCloud999@Love  mysql:latest
4.Mysql-Cluster集群Docker執(zhí)行部署
4.1 Mysql-Master節(jié)點(diǎn)[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 MYSQL_ROOT_PASSWORD=PivotalCloud999@Love  mysql:latest
d99ac84d4e11dd3835b89ff410a488c8041c43ec67aa4bccb1679edf57b08539
Marklin:~ marklin$
4.2 Mysql-Slave節(jié)點(diǎn)[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 MYSQL_ROOT_PASSWORD=PivotalCloud999@Love  mysql:latest
fe3eb3b02fc0cf9e187a8158b58bb5832d70a4462b583d4ba4cf4297ff7dd8d3
Marklin:~ marklin$
4.3 Mysql-Cluster集群部署以及測試結(jié)果:

Mysql-Cluster集群部署:

Mysql-Cluster集群

測試Navicat客戶端連接數(shù)據(jù)如下:

Navicat客戶端

5.Mysql-Cluster集群主從節(jié)點(diǎn)后續(xù)配置
5.1 Mysql-Cluster集群節(jié)點(diǎn)IP地址

1.mysql-master節(jié)點(diǎn)IPAddress: docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-master

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

2.查看mysql-slave節(jié)點(diǎn)的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集群主從用戶授權(quán)配置

進(jìn)入Docker后臺的兩種方式:
1.通過終端輸入docker ps查看容器進(jìn)程,最后執(zhí)行docker exec -it mysql容器名稱/id bash
執(zhí)行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$ 

進(jìn)入mysql容器: docker exec -it d99ac84d4e11 bash

Marklin:~ marklin$ docker exec -it d99ac84d4e11 bash
root@mysql-master:/#

2.通過Docker可視化后臺進(jìn)入:
點(diǎn)擊Consle:

點(diǎn)擊Conect:

進(jìn)入到mysql容器:

登錄mysql客戶端:mysql -u root -p

root@mysql-master:/# 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>

如圖所示:

創(chuàng)建用戶并授權(quán):

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'PivotalCloud999@Love';
Query OK, 0 rows affected (0.10 sec)

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'PivotalCloud999@Love';
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'PivotalCloud999@Love';
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>

如圖所示:

查看master狀態(tài):show master status; ,記錄二進(jìn)制文件名和位置

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>

如圖所示:

版權(quán)聲明:本文為博主原創(chuàng)文章,遵循相關(guān)版權(quán)協(xié)議,如若轉(zhuǎn)載或者分享請附上原文出處鏈接和鏈接來源。

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >