docker部署mysql8.0
linux环境下基于docker部署并配置mysql8.0
环境docker-18.06.0
192.168.12.2 20002 主
192.168.12.3 20004 从
1. # 下载mysql的版本
docker pull mysql:8.0.22
2. # 创建文件夹
为docker挂载使用,不明白挂载的可以去学习一下docker基础,可以根据自己的需求创建,我选择在home中
该版本的mysql需要多挂在一个mysql-files,不然启动会报错,找不到该目录
mkdir -p /home/deploy/mysql/conf
mkdir -p /home/deploy/mysql/data
mkdir -p /home/deploy/mysql/mysql-files
mkdir -p /home/deploy/mysql/pid
mkdir -p /home/deploy/mysql/socket
useradd -s /sbin/nologin -M mysql
chown -R mysql:mysql /home/deploy/mysql
chmod -R 777 /home/deploy/mysql
3. # 配置文件
master配置
vim /home/deploy/mysql/conf/my.cnf
[client]
user=mysql
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
server-id=1
user=mysql
datadir=/home/deploy/mysql/data
default_authentication_plugin=mysql_native_password
transaction_isolation=READ-COMMITTED
max_connections=5000
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
lower_case_table_names=1
---------------------------------------------------------------------------------------
slave配置
vim /home/deploy/mysql/conf/my.cnf
[client]
user=mysql
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
server-id=2
user=mysql
datadir=/home/deploy/mysql/data
default_authentication_plugin=mysql_native_password
transaction_isolation=READ-COMMITTED
max_connections=5000
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
lower_case_table_names=1
-------------------------------------------------------------------------------------------------------
然后就可以挂载运行了
4. # 运行mysql容器
docker run -p 20002:3306 --name mysql \
-v /home/deploy/mysql/conf:/etc/mysql \
-v /home/deploy/mysql/data:/var/lib/mysql \
-v /home/deploy/mysql/mysql-files/:/var/lib/mysql-files \
-v /home/deploy/mysql/socket/:/var/run/mysqld \
-e MYSQL_ROOT_PASSWORD=butel \
-d mysql:8.0.22
docker run -p 3306:3306 --name mysql-01 \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
--restart=always \
-d mysql:8.0.22
5. # 查看容器状态
[root@localhost mysql]# docker ps -a |grep mysql
bf0f36089b00 mysql:8.0.22 "docker-entrypoint.s…" 22 hours ago Up 2 hours 33060/tcp, 0.0.0.0:20002->3306/tcp mysql
6. # 连接容器,登录mysql
[root@localhost mysql]# docker exec -it bf0f36089b00 /bin/bash
root@bf0f36089b00:/# mysql -uroot -pbutel
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 258
Server version: 8.0.22 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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>
设置主主同步
7. 进入容器后,我们需要连接其MySQL服务:然后我们需要为从服务器创建一个可以用来master服务器的账户,也就是创建一个专门用来复制binlog的账号,并且赋予该账号复制权限,其命令如下:
主:
create user 'repl' @'192.168.12.3' identified by 'butel'; 谁能连接我 对方的ip 允许slave12.3使用repl用户butel密码连接我
GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.12.3'; 连接我有啥权限 对方的ip
FLUSH PRIVILEGES;
从:
create user 'repl' @'192.168.12.2' identified by 'butel'; 允许master12.2使用repl用户butel密码连接我
GRANT ALL PRIVILEGES ON *.* TO 'repl'@'192.168.12.2'; 连接我有啥权限 对方的ip
FLUSH PRIVILEGES;
8. 从节点尝试使用repl用户和butel密码登录主节点
mysql -urepl -pbutel -h192.168.12.2 -P20002 salve 使用repl连接master
mysql -urepl -pbutel -h192.168.12.3 -P20004 master使用repl连接salve
9. 接着我们需要查看master节点的binlog状态:
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000004 | 827 | | | |
+---------------+----------+--------------+------------------+-------------------+
10. 从节点上操作(指定master节点信息)
mysql> stop slave;
mysql> change master to master_host='192.168.12.2', master_user='repl', master_password='butel', master_port=20002, master_log_file='binlog.000004', master_log_pos=827;
mysql> start slave;
mysql> show slave status \G;
注:上述命令中,各个参数的含义其实比较好理解,主要就是指定master服务的ip、用户名、密码、端口,以及master节点的binlog文件和log的position。这里的binlog文件和log的position就是前面我们
show master status;
命令的执行结果,最后我们需要开启主从复制:11. 主节点上操作(指定master节点信息)
mysql> stop slave;
mysql> change master to master_host='192.168.12.3', master_user='repl', master_password='butel', master_port=20004, master_log_file='binlog.000004', master_log_pos=827;
mysql> start slave;
mysql> show slave status \G;
12.验证