docker 安装mysql
docker 常用命令:
docker ps
docker ps -a
docker start imageID
docker stop imageID
docker rm imageID
安装MySQL
docker pull mysql: 5.7.30
docker images
在宿主机创建三个文件夹(配置、数据等),路径自定义;
mkdir -p /work/docker/mysql/conf
mkdir -p /work/docker/mysql/data
mkdir -p /work/docker/mysql/log
在/work/docker/mysql/conf路径下创建配置文件:
touch /work/docker/mysql/conf/my.cnf
配置文件内容:
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
#服务端口号 默认3306
port=3306
bind-address=0.0.0.0
lower_case_table_names=1
datadir = /work/docker/mysql/data
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
#最大连接数
max_connections=200
#连接失败的最大次数。防止有人从该主机试图攻击数据库系统
max_connect_errors=20
#创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
启动mysql容器:
docker run -p 3306:3306 \
--name mysql7 \
--privileged=true \
-v /work/docker/mysql/log:/var/log/mysql \
-v /work/docker/mysql/data:/var/lib/mysql \
-v /work/docker/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7.30
进入mysql容器内部修改远程访问权限:
docker exec -it mysql7 /bin/bash
连接mysql:
mysql -u root -p
use mysql;
select host,user from mysql.user;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
修改访问主机以及密码,设置为所有主机可访问:
update user set host='%' where user='root';
给 root 用户分配远程访问权限:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
强制刷新权限:
FLUSH PRIVILEGES;