创建数据卷
代码语言:javascript复制mkdir -p /usr/mysql/conf /usr/mysql/data
代码语言:javascript复制chmod -R 755 /usr/mysql/
创建配置文件
vim /usr/mysql/conf/my.cnf
:
[client]
#socket = /usr/mysql/mysqld.sock
default-character-set = utf8mb4
[mysqld]
#pid-file = /var/run/mysqld/mysqld.pid
#socket = /var/run/mysqld/mysqld.sock
#datadir = /var/lib/mysql
#socket = /usr/mysql/mysqld.sock
#pid-file = /usr/mysql/mysqld.pid
datadir = /usr/mysql/data
character_set_server = utf8mb4
collation_server = utf8mb4_bin
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/
创建容器
代码语言:javascript复制docker run --name mysql8 --restart=always -v /usr/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /usr/mysql/data:/var/lib/mysql -v /etc/localtime:/etc/localtime:ro -p 3310:3306 -e MYSQL_ROOT_PASSWORD=bntang -d mysql:8
添加用户
代码语言:javascript复制docker exec -it mysql8 /bin/bash
代码语言:javascript复制# 创建 bntang 用户,密码为 bntang,允许在任意机器上登录
CREATE USER 'bntang'@'%' IDENTIFIED BY 'bntang';
代码语言:javascript复制# 赋予 bntang 用户在 所有机器上 对 所有数据库和数据表 拥有 所有操作权限
GRANT ALL ON *.* TO 'bntang'@'%';
MySQL 5.7
代码语言:javascript复制docker run -itd --name="mysql" -p3307:3306 -v /root/mydata/mysql/conf/:/etc/mysql/conf.d -v /root/mydata/mysql/data:/var/lib/mysql -v /root/mydata/mysql/logs:/var/log -e MYSQL_ROOT_PASSWORD="1234" mysql:5.7
代码语言:javascript复制grant all privileges on *.* to 'root'@'%' identified by '1234'