推荐Mysql8.0, 其拥有更多新特性,如支持NO SQL,原生分布式, 支持UTF8-mb4, 取消了myisam. 最重要是性能大幅提升.
Linux系统:Ubuntu 16.04
docker版本:18.09.5
mysql版本: 8.0.16
$ su root
$ docker pull mysql:8.0.16
docker pull mysql:8.0.16
root@instance-o70no2nw:~# su root
root@instance-o70no2nw:~# docker pull mysql:8.0.16
latest: Pulling from library/mysql
27833a3ba0a5: Pull complete
864c283b3c4b: Pull complete
cea281b2278b: Pull complete
Digest: sha256:f5f78fe2054b4686da3fddb460eab0b53d04e067c977d6a02fcb5ec25375ed15
Status: Downloaded newer image for mysql:8.0.16
思路:Mysql可用,需保以下几点:
- 端口开放:3306对外开放 -> 需映射端口
- 数据持久化:配置文件、存储信息不丢失 -> 需挂载本地卷
- 支持中文:设置编码参数 -> 设置参数
- 初始化ROOT密码:-e MYSQL_ROOT_PASSWORD=root
- 永不停机: restart=always
- 守护态运行: -d
基于以上思路, Mysql容器创建命令如下:
$ docker run \
--name mysql \
--restart=always \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v /usr/local/docker/mysql/conf:/etc/mysql/conf.d \
-v /usr/local/docker/mysql/logs:/var/log/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-d mysql:8.0.16 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci
缘起:创建Mysql容器时,我们将宿主机目录挂载到容器配置目录上,
-v /usr/local/docker/mysql/conf:/etc/mysql/conf.d
这会导致一些问题。 因为宿主目录是空的,容器的原有配置信息也被清空,引申出一个问题,如何将Mysql容器的conf.d/mysql.cnf复制出来.
目标: 将容器中的文件复制到宿主机
思路: 创建一个临时Mysql容器->将该容器的配置文件复制到宿主目的卷->销毁该临时容器-> 重新执行Mysql容器创建命令
因为 mysql.cnf是空的… 具体验证如下:
$ docker run -it --rm mysql:8.0.16 bash
root@200a86d29e7c:/etc/mysql# 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= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/
root@200a86d29e7c:/etc/mysql/conf.d# ls
docker.cnf mysql.cnf
root@200a86d29e7c:/etc/mysql/conf.d# cat mysql.cnf
# Copyright (c) 2015, 2016, 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, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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 Client configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysql]
root@200a86d29e7c:/etc/mysql/conf.d#
官方指南:Docker Hub for Mysql
mysql8 :客户端连接caching-sha2-password问题
在安装mysql8的时候如果选择了密码加密,之后用客户端连接比如navicate,会提示客户端连接caching-sha2-password,是由于客户端不支持这种插件,可以通过如下方式进行修改
#修改加密规则
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
#更新密码(mysql_native_password模式)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '{NewPassword}';