docker安装Mysql8
0. 系统优化
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
1. 准备conf文件
mkdir -p /home/mysql/conf
cd /home/mysql/conf
vim my.cnf
写以下内容:
[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8mb4
[mysqld]
# 数据库忽略大小写 8.0加了启动报错
# lower_case_table_names = 1
#设置3306端口
port=3306
#允许最大连接数
max_connections=1000
#允许连接失败的次数。
max_connect_errors=10
#最大允许包
max_allowed_packet=10M
#服务端使用的字符集默认为utf8mb4
character-set-server=utf8mb4
collation_server = utf8mb4_general_ci
#创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# mysql8中“mysql_native_password”插件认证不让用了,改成默认的caching_sha2_password
default_authentication_plugin=caching_sha2_password
2. 命令行启动容器
docker run -d -p 3316:3306 \
--restart=always \
--privileged=true \
-v /home/mysql/log:/var/log/mysql \
-v /home/mysql/data:/var/lib/mysql \
-v /home/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /home/mysql/mysql-files:/var/lib/mysql-files \
-v /etc/timezone:/etc/timezone:ro \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD=mysql123 \
-e TZ=Asia/Shanghai \
--name mysql mysql:latest
启动成功后,进入容器:
docker exec -it mysql bash
root@a18abfe70e23:/# mysql -u root -p
mysql> use mysql;
mysql> select Host, User, plugin, authentication_string from user;
OK
3. 番外:改root密码
-- 改本地root密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
-- 改本地root密码
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'new_password';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
喜欢请赞赏一下啦^_^