容器版MySQL安装和配置(Docker) 原创 数据库 2022年5月9日 16:32 夏至未至 1220 当前内容 11231 字,在路上,马上到,马上到 ### 目录 [TOC] ### 安装容器环境 参考:[Docker容器环境安装(附安装脚本)](https://www.codecomeon.com/posts/176/ "Docker容器环境安装(附安装脚本)") ### 安装步骤 #### 拉取mysql镜像 在如下界面选择你需要拉取的镜像:[MySQL容器镜像](https://hub.docker.com/_/mysql?tab=tags "MySQL容器镜像"),如下命令拉取: docker pull mysql:latest 如下便是拉取完成了: [root@localhost /]# docker pull mysql:latest latest: Pulling from library/mysql 4be315f6562f: Pull complete 96e2eb237a1b: Pull complete 8aa3ac85066b: Pull complete ac7e524f6c89: Pull complete f6a88631064f: Pull complete 15bb3ec3ff50: Pull complete ae65dc337dcb: Pull complete 573c3c7fa18d: Pull complete 9d10771b98b8: Pull complete 3d8ef442614b: Pull complete 7dc17a6cea26: Pull complete 752752efdaea: Pull complete Digest: sha256:2dafe3f044f140ec6c07716d34f0b317b98f8e251435abd347951699f7aa3904 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest [root@localhost /]# 查看镜像情况: [root@localhost /]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 96d0eae5ed60 11 days ago 524MB hello-world latest feb5d9fea6a5 7 months ago 13.3kB [root@localhost /]# #### 创建并启动容器 命令如下: [root@localhost /]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name code_mysql -v /var/lib/mysql/:/var/lib/mysql mysql:latest 1e0566ddcba91798aeb1d7d2ecf28b3b7b42b10a6930236f5012bb80e1d2f720 [root@localhost /]# 参数解释: - run:运行一个容器 - -d:看做做守护线程(Daemon),即后台运行 - -p:进行端口映射,容器内端口映射到宿主机端口 - -e:初始化root用户的密码 - --name:自定义容器名称 - -v:挂载。即卷持久化,容器删除,内部数据会丢失,把宿主机上的卷映射到容器内,即挂载,容器内产生的数据,容器外映射文件也能看到 最后一个参数是镜像名称+镜像标签(tag),`docker images` 能看到。启动成功之后会出现一个随机字符串,表示容器的id #### 容器内连接数据库 首先查看容器运行情况: [root@localhost /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1e0566ddcba9 mysql:latest "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp code_mysql [root@localhost /]# 容器状态 `Up`,即正常,下面进入容器,连接数据库,验证其工作情况: [root@localhost /]# docker exec -it code_mysql /bin/sh # 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 12 Server version: 8.0.29 MySQL Community Server - GPL Copyright (c) 2000, 2022, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> 验证正常,接下来就是使用数据库了,常规操作了。 #### 宿主机上连接数据库 如果你宿主机上安装了 mysql 客户端,就可以在宿主机用客户端,连接容器内mysql了,因为启动容器的时候,已经做了端口映射了。 [root@localhost /]# mysql -uroot -p123456 -P3306 -h127.0.0.1 ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory [root@localhost /]# 如果你装的是 mysql8,连接的时候,报错如下,那是因为 8 的密码加密规则变了。如此,进入容器,连接mysql执行如下命令,修改加密规则即可: [root@localhost /]# docker exec -it code_mysql /bin/sh # 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 12 Server version: 8.0.29 MySQL Community Server - GPL Copyright (c) 2000, 2022, 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> ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; Query OK, 0 rows affected (0.00 sec) mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; Query OK, 0 rows affected (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec) mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) 重点就是这几个语句,最后一个是重置密码: ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; FLUSH PRIVILEGES; ALTER USER 'root'@'%' IDENTIFIED BY '123456'; 最后在宿主机连接验证: [root@localhost /]# mysql -uroot -p123456 -P3306 -h192.168.78.189 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 8.0.29 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) MySQL [(none)]> 特别注意的是,语句中 `'root'@'%'`,`%`代表了 所有hostname,即所有IP,如果你使用的是 `'root'@'localhost'`,那么连接数据库的时候,你只能通过 `localhost` 连接了。 ### 容器mysql配置文件持久化 容器删除或重启,容器内数据会丢失,容器支持通过卷挂载的方式,来持久化容器中想要保留下来的数据。那对于mysql来说,我们需要持久化的只有两个项,第一个就是数据文件目录,第二个是数据库配置文件,上边的启动容器命令中 `-v` 已经实现了数据文件的持久化,现在需要持久化配置文件。以下为步骤: #### 拷贝配置文件 为啥要拷贝配置文件呢,因为,可以这样理解,就是容器启动的时候,`-v`后边的参数,有两个目录,使用分号隔开,前边的为宿主机目录,后边的为容器内目录。 然后挂载的动作,就是把前边宿主机的目录挂载到后边容器内目录,那这就会有一个覆盖的行为,这样配置文件的挂载就尤为重要,如果宿主机上配置文件是空的,在启动的时候,就会覆盖容器内数据库的配置文件,故,我们需要拷贝容器的初始配置文件到宿主机上,之后再做挂载的动作。如下拷贝: [root@localhost /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1e0566ddcba9 mysql:latest "docker-entrypoint.s…" 57 minutes ago Up 33 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp code_mysql [root@localhost /]# [root@localhost /]# docker cp code_mysql:/etc/mysql/my.cnf /etc/my.cnf [root@localhost /]# 查看复制结果: [root@localhost /]# cat /etc/my.cnf # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # 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 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 Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure-file-priv= NULL # Custom config should go here !includedir /etc/mysql/conf.d/ [root@localhost /]# #### 再次启动容器 先删原来的,再启动新的: [root@localhost /]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name code_mysql -v /etc/my.cnf:/etc/mysql/my.cnf -v /var/lib/mysql/:/var/lib/mysql mysql:latest eef37bf57ffa280705d854d2541dd7215845ea564ac59075316f632e38fcae47 [root@localhost /]# [root@localhost /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eef37bf57ffa mysql:latest "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp code_mysql [root@localhost /]# 两个 v 就是持久化了两项。如果遇到如下报错,基本都是把空配置挂载到了容器内,按本节说的处理,即可解决问题: [root@localhost /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 83598af78842 mysql:latest "docker-entrypoint.s…" 8 seconds ago Exited (1) 8 seconds ago code_mysql [root@localhost /]# [root@localhost /]# docker logs 83598af78842 2022-05-09 07:53:59+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1debian10 started. 2022-05-09 07:53:59+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config command was: mysqld --verbose --help --log-bin-index=/tmp/tmp.1ZEIO8itWq mysqld: Can't read dir of '/etc/my.cnf.d' (OS errno 2 - No such file or directory) mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/mysql/my.cnf at line 18. mysqld: [ERROR] Fatal error in defaults handling. Program aborted! [root@localhost /]# #### 其他报错提示 [root@localhost mysql_load_file]# mysql -uroot -p123456 -P3306 -h192.168.78.189 mysql: Can't read dir of '/etc/mysql/conf.d/' (Errcode: 2) Fatal error in defaults handling. Program aborted [root@localhost mysql_load_file]# 这是因为宿主机 mysql 客户端这边的配置问题,如下,注释宿主机配置文件最后一行即可解决: [root@localhost etc]# cat my.cnf # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # 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 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 Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql secure_file_priv= # Custom config should go here #!includedir /etc/mysql/conf.d/ [root@localhost etc]# 本文标题: 容器版MySQL安装和配置(Docker) 本文作者: 夏至未至 发布时间: 2022年5月9日 16:32 最近更新: 2022年5月9日 17:40 原文链接: 许可协议: 署名-非商业性-禁止演绎 4.0 国际(CC BY-NC-ND 4.0) 请按协议转载并保留原文链接及作者 Docker(8) 容器(4) 上一个 Linux单库单表利用存储过程造MySQL数据脚本 下一个 Docker容器环境安装(附安装脚本) 当前文章评论暂未开放,请移步至留言处留言。