标签搜索

目 录CONTENT

文章目录

Docker安装Mysql8

陈铭
2023-11-07 / 0 评论 / 0 点赞 / 64 阅读 / 472 字 / 正在检测是否收录...

安装

挂载目录

挂载目录准备,宿主机下准备好mysql的配置文件

mkdir -p /opt/mysql/mysql/conf.d
cd /opt/mysql/mysql/conf.d

在conf.d下新建配置文件my.cnf

# 新建配置文件my.cnf
# 其中mysql8默认不允许账号密码登录,配置文件中指定了mysql_native_password
vim my.cnf

[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
expire_logs_days=7
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000
 
[client]
default-character-set=utf8
 
[mysql]

启动容器

docker run \
-p 5566:3306 \
--name mysql8 \
--privileged=true \
--restart unless-stopped \
-v /opt/mysql/mysql:/etc/mysql \
-v /opt/mysql/logs:/logs \
-v /opt/mysql/data:/var/lib/mysql \
-v /opt/mysql/mysql-files:/var/lib/mysql-files \
-v /etc/localtime:/etc/localtime \
-e MYSQL_ROOT_PASSWORD=<root_password> \
-d \
mysql:8.0.20

配置mysql

进入容器,连接mysql

docker exec -it mysql8 bash
mysql -u root -p

给root用户赋权

grant all PRIVILEGES on *.* to root@'%' WITH GRANT OPTION;
use mysql
update user set host='%' where user='root';

# 让root能从任意地方登录(host=%)
select host,user from user where user ='root';
+-----------+------+
| host      | user |
+-----------+------+
| %         | root |
| localhost | root |
+-----------+------+

配置root用户的登录方式,即使用账号密码

grant all PRIVILEGES on *.* to root@'%' WITH GRANT OPTION;
ALTER user 'root'@'%' IDENTIFIED BY '<root_password>    ' PASSWORD EXPIRE NEVER;
ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '<root_password>';
FLUSH PRIVILEGES;

创建一个新用户,一样是允许任意来源的host登录

CREATE USER 'reyee_cem'@'%' IDENTIFIED BY '<reyee_cem_password>';
REVOKE ALL PRIVILEGES ON *.* FROM 'reyee_cem'@'%';
GRANT ALL PRIVILEGES ON cem.* TO 'reyee_cem'@'%';
FLUSH PRIVILEGES;

配置reyee_cem用户的登录方式,即使用账号密码

ALTER user 'reyee_cem'@'%' IDENTIFIED BY '<reyee_cem_password>' PASSWORD EXPIRE NEVER;
ALTER user 'reyee_cem'@'%' IDENTIFIED WITH mysql_native_password BY '<reyee_cem_password>';
FLUSH PRIVILEGES;
0

评论区