测试版

创建MySQL容器

1. 下载MySQL镜像

  1. [root@k8s-master ~]# docker search mysql --limit 3
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relation 12286 [OK]
  4. mariadb MariaDB Server is a high performing open sou 4726 [OK]
  5. percona Percona Server is a fork of the MySQL relati 572 [OK]
  6. [root@k8s-master ~]# docker pull mysql:5.7
  7. 5.7: Pulling from library/mysql
  8. 72a69066d2fe: Pull complete
  9. 93619dbc5b36: Pull complete
  10. Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
  11. Status: Downloaded newer image for mysql:5.7
  12. docker.io/library/mysql:5.7

2. 运行MySQL容器

  1. [root@k8s-master ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
  2. 9d00ca3b344832a1c648da874fbcb531cd0ef1a4cc75aa8940d532d1d3933fc7
  3. [root@k8s-master ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 9d00ca3b3448 mysql:5.7 "docker-entrypoint.s…" 4 seconds ago Up 2 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp boring_haslett

3. 登录MySQL容器,并创建测试数据

  1. [root@k8s-master ~]# docker exec -it 9d00ca3 /bin/bash
  2. root@9d00ca3b3448:/# mysql -p
  3. Enter password:
  4. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  5. mysql> create database db01;
  6. Query OK, 1 row affected (0.00 sec)
  7. ### 创建数据库及表
  8. mysql> use db01;
  9. Database changed
  10. mysql> create table zmedu(id int,name varchar(20));
  11. Query OK, 0 rows affected (0.00 sec)
  12. ### 插入数据
  13. mysql> insert into zmedu values(1,'itlaoxin');
  14. Query OK, 1 row affected (0.05 sec)
  15. mysql> select * from zmedu;
  16. +------+----------+
  17. | id | name |
  18. +------+----------+
  19. | 1 | itlaoxin |
  20. +------+----------+
  21. 1 row in set (0.00 sec)
  22. mysql>
  1. 测试远程链接
  1. [root@k8s-master ~]# mysql -uroot -p123456 -h 192.168.3.71
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 3
  4. Server version: 5.7.36 MySQL Community Server (GPL)
  5. MySQL [(none)]> show databases;
  6. +--------------------+
  7. | Database |
  8. +--------------------+
  9. | information_schema |
  10. | db01 |
  11. | mysql |
  12. | performance_schema |
  13. | sys |
  14. +--------------------+
  15. 5 rows in set (0.00 sec)
  16. MySQL [(none)]> use db01
  17. Reading table information for completion of table and column names
  18. You can turn off this feature to get a quicker startup with -A
  19. Database changed
  20. MySQL [db01]> show tables;
  21. +----------------+
  22. | Tables_in_db01 |
  23. +----------------+
  24. | zmedu |
  25. +----------------+
  26. 1 row in set (0.00 sec)
  27. MySQL [db01]>

4. 注意事项:

注意事项: 默认情况下,docker中字符集都是latin1 插入中文容易报错

  1. mysql> show variables like 'character%';
  2. +--------------------------+----------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+----------------------------+
  5. | character_set_client | latin1 |
  6. | character_set_connection | latin1 |
  7. | character_set_database | latin1 |
  8. | character_set_filesystem | binary |
  9. | character_set_results | latin1 |
  10. | character_set_server | latin1 |
  11. | character_set_system | utf8 |
  12. | character_sets_dir | /usr/share/mysql/charsets/ |
  13. +--------------------------+----------------------------+
  14. 8 rows in set (0.00 sec)

插入中文一定会报错

  1. MySQL [db01]> insert into zmedu values(2,'老辛');
  2. ERROR 1366 (HY000): Incorrect string value: '\xE8\x80\x81\xE8\xBE\x9B' for column 'name' at row 1
  3. MySQL [db01]>

实用版

创建实例

  1. [root@k8s-master ~]# docker pull mysql:5.7
  2. 5.7: Pulling from library/mysql
  3. 72a69066d2fe: Pull complete
  4. Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
  5. Status: Downloaded newer image for mysql:5.7
  6. docker.io/library/mysql:5.7
  7. [root@k8s-master ~]# docker run -d -p 3306:3306 --privileged=true -v /zmedu/mysql/log:/var/log/mysql -v /zmedu/mysql/data:/var/lib/mysql -v /zmedu/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
  8. ce47c9c630ffead3d398a56c40f394960c6d778be967f7aad4f508c035325352

修改配置文件

新建my.cnf

  1. [root@k8s-master ~]# cat !$
  2. cat /zmedu/mysql/conf/my.cnf
  3. [client]
  4. default_character_set=utf8
  5. [mysqld]
  6. collation_server =utf8_general_ci
  7. character_set_server = utf8
  8. [root@k8s-master ~]#

重新启动容器并进入查看字符集

  1. [root@k8s-master ~]# docker restart mysql
  2. mysql
  3. [root@k8s-master ~]# docker exec -it mysql bash
  4. root@ce47c9c630ff:/# mysql -uroot -p123456
  5. mysql: [Warning] Using a password on the command line interface can be insecure.
  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 2
  8. Server version: 5.7.36 MySQL Community Server (GPL)
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> show variables like 'character%';
  11. +--------------------------+----------------------------+
  12. | Variable_name | Value |
  13. +--------------------------+----------------------------+
  14. | character_set_client | utf8 |
  15. | character_set_connection | utf8 |
  16. | character_set_database | utf8 |
  17. | character_set_filesystem | binary |
  18. | character_set_results | utf8 |
  19. | character_set_server | utf8 |
  20. | character_set_system | utf8 |
  21. | character_sets_dir | /usr/share/mysql/charsets/ |
  22. +--------------------------+----------------------------+
  23. 8 rows in set (0.00 sec)
  24. mysql>

测试是否可以插入中文

  1. [root@k8s-master ~]# mysql -uroot -p123456 -h 192.168.3.71
  2. [root@k8s-master ~]# create databese db01;
  3. MySQL [(none)]> show databases;
  4. +--------------------+
  5. | Database |
  6. +--------------------+
  7. | information_schema |
  8. | db01 |
  9. | mysql |
  10. | performance_schema |
  11. | sys |
  12. +--------------------+
  13. 5 rows in set (0.00 sec)
  14. MySQL [(none)]> use db01;
  15. Reading table information for completion of table and column names
  16. You can turn off this feature to get a quicker startup with -A
  17. Database changed
  18. MySQL [db01]> insert into zmedu values(2,'老辛');
  19. Query OK, 1 row affected (0.00 sec)
  20. MySQL [db01]> select *from zmedu;
  21. +------+--------+
  22. | id | name |
  23. +------+--------+
  24. | 2 | 老辛 |
  25. +------+--------+
  26. 1 row in set (0.00 sec)

可以看到已经可以插入中文了

建议: 创建好数据库后,一定要先设置字符集,再去创建库和表

问题: 删除容器后,MySQL库还在吗

实现方法: 把MySQL容器删掉,查看数据是否还在?

  1. [root@k8s-master mysql]# docker rm -f mysql
  2. mysql
  3. [root@k8s-master mysql]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. [root@k8s-master mysql]# docker run -d -p 3306:3306 --privileged=true -v /zmedu/mysql/log:/var/log/mysql -v /zmedu/mysql/data:/var/lib/mysql -v /zmedu/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
  6. 193de1aa288ee23b2b5648d3be5c99726945a8115e5b84c604b83a23a7dffdff
  7. [root@k8s-master mysql]# docker ps
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. 193de1aa288e mysql:5.7 "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
  10. [root@k8s-master mysql]# docker exec -it 193de1 bash
  11. root@193de1aa288e:/# mysql -p123456
  12. mysql: [Warning] Using a password on the command line interface can be insecure.
  13. Welcome to the MySQL monitor. Commands end with ; or \g.
  14. Your MySQL connection id is 2
  15. Server version: 5.7.36 MySQL Community Server (GPL)
  16. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  17. Oracle is a registered trademark of Oracle Corporation and/or its
  18. affiliates. Other names may be trademarks of their respective
  19. owners.
  20. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  21. mysql> show databases;
  22. +--------------------+
  23. | Database |
  24. +--------------------+
  25. | information_schema |
  26. | db01 |
  27. | mysql |
  28. | performance_schema |
  29. | sys |
  30. +--------------------+
  31. 5 rows in set (0.00 sec)
  32. mysql> use db01;
  33. Reading table information for completion of table and column names
  34. You can turn off this feature to get a quicker startup with -A
  35. Database changed
  36. mysql> select * from zmedu;
  37. +------+--------+
  38. | id | name |
  39. +------+--------+
  40. | 2 | 老辛 |
  41. +------+--------+
  42. 1 row in set (0.01 sec)

可以看到,即使文件被删除了,重建后,依然存在,说明数据卷的重要性

实现MySQL主从架构-docker版

1. 下载MySQL镜像

  1. [root@k8s-master ~]# docker pull mysql:5.7
  2. 5.7: Pulling from library/mysql
  3. 72a69066d2fe: Pull complete
  4. 93619dbc5b36: Pull complete
  5. 70deed891d42: Pull complete
  6. Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
  7. Status: Downloaded newer image for mysql:5.7
  8. docker.io/library/mysql:5.7

2. 创建MySQL-master并创建配置文件

  1. [root@k8s-master ~]# docker run -p 3307:3306 --name mysql-master \
  2. > -v /mydata/mysql-master/log:/var/log/mysql \
  3. > -v /mydata/mysql-master/data:/var/lib/mysql \
  4. > -v /mydata/mysql-master/conf:/etc/mysql \
  5. > -e MYSQL_ROOT_PASSWORD=root \
  6. > -d mysql:5.7
  7. cc88521bb0baea9f75c86fefb41dfa44cb3c4398f7e11885c9b9d873be6b93f9
  8. [root@k8s-master ~]# cd /mydata/mysql-master/conf/
  9. [root@k8s-master conf]# vim my.cnf
  10. [root@k8s-master conf]# grep -v ^# my.cnf
  11. [mysqld]
  12. server_id=101
  13. binlog-ignore-db=mysql
  14. log-bin=mall-mysql-bin
  15. binlog_cache_size=1M
  16. binlog_format=mixed
  17. expire_logs_days=7
  18. slave_skip_errors=1062

关于配置文件的注释:

  1. [root@k8s-master conf]# cat my.cnf
  2. [mysqld]
  3. ## 设置server_id,同一局域网中需要唯一
  4. server_id=101
  5. ## 指定不需要同步的数据库名称
  6. binlog-ignore-db=mysql
  7. ## 开启二进制日志功能
  8. log-bin=mall-mysql-bin
  9. ## 设置二进制日志使用内存大小(事务)
  10. binlog_cache_size=1M
  11. ## 设置使用的二进制日志格式(mixed,statement,row)
  12. binlog_format=mixed
  13. ## 二进制日志过期清理时间。默认值为0,表示不自动清理。
  14. expire_logs_days=7
  15. ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
  16. ## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
  17. slave_skip_errors=1062

3. 重启MySQL并配置需要同步的数据和用户

重启MySQL

  1. [root@k8s-master conf]# docker restart mysql-master
  2. mysql-master
  3. [root@k8s-master conf]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. cc88521bb0ba mysql:5.7 "docker-entrypoint.s…" 4 minutes ago Up 3 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp mysql-master
  6. [root@k8s-master conf]#

创建需要同步的数据及数据库

  1. [root@k8s-master conf]# docker exec -it mysql-master /bin/bash
  2. root@cc88521bb0ba:/# mysql -proot
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 2
  6. Server version: 5.7.36-log MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
  13. Query OK, 0 rows affected (0.01 sec)
  14. mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
  15. Query OK, 0 rows affected (0.00 sec)
  16. mysql> exit
  17. Bye

4. 新建从服务器

  1. a/mysql-slave/log:/var/log/mysql \
  2. > -v /mydata/mysql-slave/data:/var/lib/mysql \
  3. > -v /mydata/mysql-slave/conf:/etc/mysql \
  4. > -e MYSQL_ROOT_PASSWORD=root \
  5. > -d mysql:5.7
  6. 15514af9443fce00c9c6f3cc182ad210440a0052e54123a9516503d23a6bc7bb
  7. [root@k8s-master conf]# cd /mydata/mysql-slave/conf/
  8. [root@k8s-master conf]# vim my.cnf
  9. [root@k8s-master conf]# grep -v ^# my.cnf
  10. [mysqld]
  11. server_id=102
  12. binlog-ignore-db=mysql
  13. log-bin=mall-mysql-slave1-bin
  14. binlog_cache_size=1M
  15. binlog_format=mixed
  16. expire_logs_days=7
  17. slave_skip_errors=1062
  18. relay_log=mall-mysql-relay-bin
  19. log_slave_updates=1
  20. read_only=1

配置文件解析:

  1. [mysqld]
  2. ## 设置server_id,同一局域网中需要唯一
  3. server_id=102
  4. ## 指定不需要同步的数据库名称
  5. binlog-ignore-db=mysql
  6. ## 开启二进制日志功能,以备Slave作为其它数据库实例的Master时使用
  7. log-bin=mall-mysql-slave1-bin
  8. ## 设置二进制日志使用内存大小(事务)
  9. binlog_cache_size=1M
  10. ## 设置使用的二进制日志格式(mixed,statement,row)
  11. binlog_format=mixed
  12. ## 二进制日志过期清理时间。默认值为0,表示不自动清理。
  13. expire_logs_days=7
  14. ## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
  15. ## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
  16. slave_skip_errors=1062
  17. ## relay_log配置中继日志
  18. relay_log=mall-mysql-relay-bin
  19. ## log_slave_updates表示slave将复制事件写进自己的二进制日志
  20. log_slave_updates=1
  21. ## slave设置为只读(具有super权限的用户除外)
  22. read_only=1

5. 重启从服务器

  1. [root@k8s-master conf]# docker restart mysql-slave
  2. mysql-slave

6. 查看同步状态

在主服务器查看同步状态

  1. [root@k8s-master conf]# docker exec -it mysql-master bash
  2. root@cc88521bb0ba:/# mysql -proot
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 3
  6. Server version: 5.7.36-log MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql> show master status;
  13. +-----------------------+----------+--------------+------------------+-------------------+
  14. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  15. +-----------------------+----------+--------------+------------------+-------------------+
  16. | mall-mysql-bin.000001 | 617 | | mysql | |
  17. +-----------------------+----------+--------------+------------------+-------------------+
  18. 1 row in set (0.00 sec)

进入从服务器配置主从同步

  1. [root@k8s-master conf]# docker exec -it mysql-slave /bin/bash
  2. root@15514af9443f:/# mysql -proot
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 2
  6. Server version: 5.7.36-log MySQL Community Server (GPL)
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. mysql> change master to master_host='192.168.3.71', master_user='slave', master_password='123456', master_port=3307, master_log_file='mall-mysql-bin.000001', master_log_pos=617, master_connect_retry=30;
  9. Query OK, 0 rows affected, 2 warnings (0.01 sec)
  10. mysql>

查看主从状态

  1. mysql> show slave status \G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State:
  4. Master_Host: 192.168.3.71
  5. Master_User: slave
  6. Master_Port: 3307
  7. Connect_Retry: 30
  8. Master_Log_File: mall-mysql-bin.000001
  9. Read_Master_Log_Pos: 617
  10. Relay_Log_File: mall-mysql-relay-bin.000001
  11. Relay_Log_Pos: 4
  12. Relay_Master_Log_File: mall-mysql-bin.000001
  13. Slave_IO_Running: No
  14. Slave_SQL_Running: No

7. 主从同步测试

  1. mysql> show slave status \G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State:
  4. Master_Host: 192.168.3.71
  5. Master_User: slave
  6. Master_Port: 3307
  7. Connect_Retry: 30
  8. Master_Log_File: mall-mysql-bin.000001
  9. Read_Master_Log_Pos: 617
  10. Relay_Log_File: mall-mysql-relay-bin.000001
  11. Relay_Log_Pos: 4
  12. Relay_Master_Log_File: mall-mysql-bin.000001
  13. Slave_IO_Running: No
  14. Slave_SQL_Running: No

开启主从同步

  1. mysql> start slave;
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> show slave status \G;
  4. *************************** 1. row ***************************
  5. Slave_IO_State: Waiting for master to send event
  6. Master_Host: 192.168.3.71
  7. Master_User: slave
  8. Master_Port: 3307
  9. Connect_Retry: 30
  10. Master_Log_File: mall-mysql-bin.000001
  11. Read_Master_Log_Pos: 617
  12. Relay_Log_File: mall-mysql-relay-bin.000002
  13. Relay_Log_Pos: 325
  14. Relay_Master_Log_File: mall-mysql-bin.000001
  15. Slave_IO_Running: Yes
  16. Slave_SQL_Running: Yes

插入数据测试:

在主服务上插入数据,在从服务器上查看

主服务器上创建数据

  1. mysql> create database db02;
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> use db02
  4. Database changed
  5. mysql> create table zmedu(id int,name varchar(28));
  6. Query OK, 0 rows affected (0.05 sec)
  7. mysql> insert into zmedu values(1,'zhang');
  8. Query OK, 1 row affected (0.02 sec)
  9. mysql> select * from zmedu;
  10. +------+-------+
  11. | id | name |
  12. +------+-------+
  13. | 1 | zhang |
  14. +------+-------+
  15. 1 row in set (0.00 sec)
  16. mysql>

从服务器查看

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | db02 |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)
  12. mysql> use db02
  13. Reading table information for completion of table and column names
  14. You can turn off this feature to get a quicker startup with -A
  15. Database changed
  16. mysql> select * from zmedu;
  17. +------+-------+
  18. | id | name |
  19. +------+-------+
  20. | 1 | zhang |
  21. +------+-------+
  22. 1 row in set (0.00 sec)
  23. mysql>

读写分离

MySQL架构