文章目录
目录结构
一、配置相关挂载目录及其文件
mkdir -p /root/mysqldocker/conf
mkdir -p /root/mysqldocker/datadir
conf 下新建my.cnf
[mysqld]
user=mysql
default-storage-engine=INNODB
#character-set-server=utf8
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[client]
#utf8mb4字符集可以存储emoji表情字符
#default-character-set=utf8
default-character-set=utf8mb4
[mysql]
#default-character-set=utf8
default-character-set=utf8mb4
datadir存放myslq数据,暂时不放东西
二、编写容器编排文件
docker-compose.yml
version: '3'
services:
mysql:
restart: always
image: mysql:5.7.16
container_name: my_mysql
volumes:
- /root/mysqldocker/mydir:/mydir
- /root/mysqldocker/datadir:/var/lib/mysql
- /root/mysqldocker/conf/my.cnf:/etc/my.cnf
- /root/mysqldocker/source:/docker-entrypoint-initdb.d
environment:
- "MYSQL_ROOT_PASSWORD=123456"
- "MYSQL_USER=root2"
- "MYSQL_PASSWORD=123456"
- "MYSQL_DATABASE=test"
- "TZ=Asia/Shanghai"
ports:
- 3306:3306
三、运行及查看
# docker-compose up -d
# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------
my_mysql docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
四、配置远程访问权限
第一次运行可能需要配置远程访问权限,详细请查看之前《远程连接docker中mysql容器》博客
1、进入容器
docker exec -it c0d75389810c bash
2、登陆
mysql -h localhost -u root -p
3、查看数据库
show databases;
4、添加远程登陆用户
CREATE USER 'newroot'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
5、给予远程用户所有表所有权限
GRANT ALL PRIVILEGES ON *.* TO 'newroot'@'%';
6、刷新权限
FLUSH PRIVILEGES;
7、远程访问
连接测试成功
进步ing