03 docker安装mysql
1. 下载镜像文件
可以在docker hub网站找到自己要的镜像文件,docker hub链接地址
#mysql后面不加版本号,表示下载最新版本
docker pull mysql:5.7
此步骤需要一点时间,由网速决定
2. 创建实例并启动
docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
参数说明
-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口
-v /mydata/mysql/conf:/etc/mysql:将配置文件夹挂载到主机
-v /mydata/mysql/log:/var/log/mysql:将日志文件夹挂载到主机
-v /mydata/mysql/data:/var/lib/mysql/:将配置文件夹挂载到主机
-e MYSQL_ROOT_PASSWORD=root:初始化 root
3. 修改mysql配置
vi /mydata/mysql/conf/my.cnf
将以下信息放入配置文件中
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
注意:解决 MySQL 连接慢的问题
在配置文件中加入如下,并重启 mysql
[mysqld]
skip-name-resolve
#解释:skip-name-resolve:跳过域名解析
4. docker中的mysql相关操作
通过容器的 mysql 命令行工具连接
docker exec -it mysql mysql -uroot -proot
设置 root 远程访问
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
flush privileges;
进入容器文件系统
docker exec -it mysql /bin/bash