赞
踩
docker 已安装。
选择合适的mysql镜像(mysql:5.7.31)。
shell> docker pull mysql:5.7.31 Trying to pull repository docker.io/library/mysql ... 5.7.31: Pulling from docker.io/library/mysql bb79b6b2107f: Pull complete 49e22f6fb9f7: Pull complete 842b1255668c: Pull complete 9f48d1f43000: Pull complete c693f0615bce: Pull complete 8a621b9dbed2: Pull complete 0807d32aef13: Pull complete 6d2fc69dfa35: Pull complete 56153548dd2c: Pull complete 3bb6ba940303: Pull complete 3e1888da91a7: Pull complete Digest: sha256:b3dc8d10307ab7b9ca1a7981b1601a67e176408be618fc4216d137be37dae10b Status: Downloaded newer image for docker.io/mysql:5.7.31
查看已拉取的镜像
shell> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest feb5d9fea6a5 6 weeks ago 13.3 kB
docker.io/mysql 5.7.31 42cdba9f1b08 12 months ago 448 MB
shell> docker create -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw --name mysql1 mysql:5.7.31
shell> docker create \
-v /data/mysql1/data:/var/lib/mysql \
-v /data/mysql1/conf.d:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-p 3306:3306 \
--name mysql1 mysql:5.7.31
--restart=always
: 自动运行-v /data/mysql1/data:/var/lib/mysql
: 将宿主机的/data/mysql1/data
挂载成容器的/var/lib/mysql
-v /data/mysql1/conf.d:/etc/mysql/conf.d
: 将宿主机的/data/mysql1/conf.d
挂载成容器的/etc/mysql/conf.d
shell> docker start mysql1
shell> docker exec -it mysql1 bash root@a6f9e4a700a3:/# mysql -uroot -pmy-secret-pw 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 2 Server version: 5.7.31 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> exit Bye
或者:
shell> docker exec -it mysql1 mysql -uroot -pmy-secret-pw 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 2 Server version: 5.7.31 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> exit Bye
shell> docker update --restart=always mysql1
shell> docker network connect --ip 172.19.0.2 mynetwork mysql1
mysql> set password=password('123456');
shell> vim /data/mysql1/conf.d/my.cnf
# 打开文件(没有该文件时,创建它)后,添加下面的配置。注意对应节点
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect = 'SET NAMES utf8mb4'
重启mysql并查看设置
shell> docker exec -it mysql1 mysql -uroot -p123456 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 2 Server version: 5.7.31 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> SHOW variables WHERE variable_name like 'character\_set\_%' OR variable_name like 'collation%'; +--------------------------+--------------------+ | Variable_name | Value | +--------------------------+--------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | collation_connection | utf8mb4_unicode_ci | | collation_database | utf8mb4_unicode_ci | | collation_server | utf8mb4_unicode_ci | +--------------------------+--------------------+ 10 rows in set (0.01 sec) mysql> exit Bye
shell> vim /data/mysql1/conf.d/my.cnf
-----------------------------
# 打开文件后,添加下面的配置。注意对应节点
[mysqld]
lower_case_table_names = 1
重启mysql并查看设置
shell> docker exec -it mysql1 mysql -uroot -p123456 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 2 Server version: 5.7.31 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> show variables like '%table_names'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | lower_case_table_names | 1 | +------------------------+-------+ 1 row in set (0.00 sec) mysql> exit Bye
时区设置成东八区
shell> vim /data/mysql1/conf.d/my.cnf
-----------------------------
# 打开文件后,添加下面的配置。注意对应节点
[mysqld]
default-time_zone = '+8:00'
重启mysql并查看设置
shell> docker exec -it mysql1 mysql -uroot -p123456 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 2 Server version: 5.7.31 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> show variables like '%time_zone%'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | UTC | | time_zone | +08:00 | +------------------+--------+ 2 rows in set (0.00 sec) mysql> exit Bye
参考这里。
https://hub.docker.com/_/mysql
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。