目的
使用docker部署mysql单机模式,并且配置相应的数据和配置的挂载,配置密码,字符集配置。
部署流程
1、创建目录,拉取镜像,运行容器,查看用户
-bash-4.2# mkdir -p /mysql/config /mysql/data
-bash-4.2# vim /mysql/config/my.cnf
-bash-4.2# docker pull mysql:5.7.9
# 指定字符集,指定数据挂载,指定配置文件挂载,指定端口映射
-bash-4.2# docker run -d --privileged=true --restart always --name mysql -p 3306:3306 -v /mysql/config:/etc/mysql/conf.d -v /mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=aa132698 mysql:5.7.9 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
-bash-4.2# docker exec -it mysql mysql -u root -p
# 查询当前用户
mysql> select USER();
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
# 查询权限没问题
my.cnf 具体内容
# Copyright (c) 2014, 2015, 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 Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
skip-host-cache
skip-name-resolve
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
#log-error = /var/log/mysql/error.log
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
2、因为使用了启动参数,环境变量无法使用,设置一下mysql用户密码。
mysql> set password for 'root'@'localhost' = password('aa132698');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'aa132698';
Query OK, 0 rows affected, 1 warning (0.00 sec)
在 MySQL 中执行 SET PASSWORD 或 GRANT 命令时出现警告通常是因为一些安全设置或者语法上的问题。在你的情况下,警告消息表明命令已经执行,但可能存在一些不符合最佳实践的设置。让我们逐个分析一下:
SET PASSWORD 命令:
当你执行 SET PASSWORD 命令来修改 root 用户的密码时,MySQL 返回了一个警告。这可能是因为该命令在较新的 MySQL 版本中被废弃,而推荐使用 ALTER USER 语句来修改密码。虽然 SET PASSWORD 命令仍然可用,但 MySQL 可能会发出警告以提醒用户。
GRANT 命令:
在你执行 GRANT 命令来授予 root 用户在所有数据库上的全部权限时,同样出现了警告。这可能是因为给予 root 用户在所有数据库上的全部权限不够安全,最佳实践是为不同的应用程序或用户组分配精确的权限,而不是直接给予 root 用户所有权限。
3、最后一定要记得开防火墙。
问题
1、运行容器的命令参数和环境变量不兼容
docker run 时,我设置了2,发现密码失效,没有密码。
1、 -e MYSQL_ROOT_PASSWORD=aa132698
2、--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
解决方法:部署后设置
2、如果发生权限问题(Fatal error: Please read "Security" section of the manual to find)
(1)在容器运行是指定运行用户为root即可。
例如: docker run -d xxx mysqld --user root
(2)设置mysql用户,并指定相关用户运行。(时间原因未尝试)
参考
1、最重要的是每次都要先参考一下官方文档,免得到处踩坑
https://hub.docker.com/_/mysql
2、可以参考菜鸟教程