https://www.cnblogs.com/badtree/articles/10130695.html
https://www.linuxidc.com/Linux/2019-02/157029.htm
1、~ docker pull mysql:8
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8 c8ee894bd2bd 12 days ago 456MB
redis latest 598a6f110d01 3 months ago 118MB
mysql 5.7 a1aa4f76fab9 4 months ago 373MB
2、从镜像启动一个容器
~ docker run -p 3306:3306 --name mysql8 \
-v /Volumes/data/develop/mysql/8/logs:/var/log/mysql \
-v /Volumes/data/develop/mysql/8/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=password \
-e TZ='Asia/Shanghai' \
--restart=always \
-d mysql:8 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_general_ci
注意:可以直接启动容器的时候直接映射配置文件,添加如下的脚本,注意路径和文件名即可;
-v /Volumes/data/develop/mysql/8/conf/my.cnf:/etc/mysql/my.cnf:rw \
配置文件基本的内容如下:(最后六行是自己配置的,其余都是从容器中拷贝的)
# 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/
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54fd46e1e8f8 mysql:8 "docker-entrypoint.s…" 28 seconds ago Up 27 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql8
3、 进入docker的mysql容器
➜ ~ docker exec -it mysql8 /bin/bash
root@54fd46e1e8f8:/#
4、登录数据库(此处的密码为参数-e MYSQL_ROOT_PASSWORD=password对应的值,此处密码为password)
# mysql -uroot -ppassword
root@54fd46e1e8f8:/# mysql -uroot -ppassword
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 8
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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.
本机使用的话,此处已经OK了,
5、使用mysql数据库
mysql> use mysql;
6、mysql8.0的root用户的验证方式变了,通过查询:select host,user,plugin from user;
mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| % | root | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
得知:root的用户的加密方式为caching_sha2_passoword, 而navicat老版本(新版本不需要修改)连接所用的方式为native_password。mysql为远程连接和本地连接提供了不同的密码验证方式。
修改root用户插件验证方式:
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
赋权限:
mysql> alter user 'root'@'localhost' identified by 'password';
mysql> alter user 'root'@'%' identified by 'password';
刷新权限:
mysql> flush privileges;
可以查看mysql的字符编码
mysql> show variables like'character%';