赞
踩
我习惯用dockerfile做一些自定义的配置。
FROM mysql:8.0.23
ENV MYSQL_ROOT_PASSWORD my_root_password
ENV MYSQL_DATABASE test
ENV MYSQL_USER app
ENV MYSQL_PASSWORD mypassword
ENV ALLOW_EMPTY_PASSWORD yes
EXPOSE 13306:3306
# 该参数命令是用来更改所有表的默认编码和排序规则以使用UTF-8(utf8mb4)命令如下--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
#docker run -itd -p 13306:3306 --name=mysql8 mymysql:1.0.0 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["mysqld"]
# 执行dockerfile 打包镜像 $ docker build -f Dockerfile -t mymysql:1.0.0 . Sending build context to Docker daemon 2.048kB Step 1/9 : FROM mysql:8.0.23 8.0.23: Pulling from library/mysql 6f28985ad184: Pull complete e7cd18945cf6: Pull complete ee91068b9313: Pull complete b4efa1a4f93b: Pull complete f220edfa5893: Pull complete 74a27d3460f8: Pull complete 2e11e23b7542: Pull complete fbce32c99761: Pull complete 08545fb3966f: Pull complete 5b9c076841dc: Pull complete 460de37ce6f9: Pull complete 9c38e51eabd2: Pull complete Digest: sha256:bfb6bdc172e101a3e7ab321f541bd4e3f9ac11bac8da7ea0708defcaf2c7554e Status: Downloaded newer image for mysql:8.0.23 ---> 808391de2156 Step 2/9 : ENV MYSQL_ROOT_PASSWORD my_root_password ---> Running in caeee2e878ee Removing intermediate container caeee2e878ee ---> ea1c18b462a8 Step 3/9 : ENV MYSQL_DATABASE test ---> Running in 67f505191760 Removing intermediate container 67f505191760 ---> 1e63d397e2a8 Step 4/9 : ENV MYSQL_USER app ---> Running in fb82983bc8af Removing intermediate container fb82983bc8af ---> 9a08dcf54da9 Step 5/9 : ENV MYSQL_PASSWORD mypassword ---> Running in ebecaab4a7ff Removing intermediate container ebecaab4a7ff ---> f35c1c672c98 Step 6/9 : ENV ALLOW_EMPTY_PASSWORD yes ---> Running in 77a6b3082637 Removing intermediate container 77a6b3082637 ---> 91f968434744 Step 7/9 : EXPOSE 13306:3306 ---> Running in 0bd83fdac468 Removing intermediate container 0bd83fdac468 ---> 45b0c458d258 Step 8/9 : ENTRYPOINT ["docker-entrypoint.sh"] ---> Running in fb6999124981 Removing intermediate container fb6999124981 ---> 35e94a5b3da8 Step 9/9 : CMD ["mysqld"] ---> Running in 97b46dfccf05 Removing intermediate container 97b46dfccf05 ---> b82b30fd7eb6 Successfully built b82b30fd7eb6 Successfully tagged mymysql:1.0.0
如果要对数据库表结构做utf8mb4编码更改,请在执行docker run
命令时,在尾部追加--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
。
# 示例为更改了表的默认编码为 utf8mb4编码
$ docker run -itd -p 13306:3306 --name=mysql8 mymysql:1.0.0 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
ce2ee419dc247cf7934cd9efcf70c0b0f9d86858d600819d8d4c9c0bb6362d79
[root@stresstest-test-wx-004 docker]#
[root@stresstest-test-wx-004 docker]# 查看 容器启动情况
[root@stresstest-test-wx-004 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce2ee419dc24 mymysql:1.0.0 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 33060/tcp, 0.0.0.0:13306->3306/tcp mysql8
[root@stresstest-test-wx-004 docker]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::22 :::* LISTEN 1/systemd
tcp6 0 0 :::8089 :::* LISTEN 51968/python
tcp6 0 0 :::13306 :::* LISTEN 104942/docker-proxy
注意 进入容器后,连接mysql数据库一定要加 -h 参数 不然连接数据库会报错ERROR 1045 (28000): Access denied for user下面记录完整的访问流程。
[root@stresstest-test-wx-004 docker]# docker exec -it mysql8 bash root@ce2ee419dc24:/# 注意 一定要加 -h 参数 不然会报错 ERROR 1045 (28000): Access denied for user root@ce2ee419dc24:/# mysql -h localhost -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.23 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> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.01 sec) mysql>
https://hub.docker.com/_/mysql
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。