下载文档请看:https://hub.docker.com/r/mysql/mysql-server
一.下载镜像
执行命令: docker pull mysql/mysql-server:5.7
查看命令:docker images
运行MySQL后台运行并映射到本地端口命令:docker run -d -p 3306:3306 --name mysql mysql/mysql-server:5.7
查看运行容器:docker ps
监视容器的输出:docker logs mysql
连接到mysql server命令:docker exec -it mysql mysql -uroot -p
密码为:docker logs mysql 里面输出password后面的内容
重置mysql服务的密码:ALTER USER 'root'@'localhost' IDENTIFIED BY 'password'
创建用户并且赋权限: create user '用户名'@'%' identified by '密码';
grant all privileges on *.* to '用户名'@'%' with grant option;
三.设置字符集编码格式
进入 容器: docker exec -it mysql bash
cd /etc
安装vim编辑器 :yum install vim
进入mysql配置文件:vim my.cnf
编写后的内容
[client] #password = your_password port = 3306 socket = /data0/data/mysql.sock default-character-set = utf8mb4 [mysql] default-character-set = utf8mb4 [mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_general_ci init_connect = 'SET NAMES utf8mb4' port = 3306 socket = /data0/data/mysql.sock skip-external-locking key_buffer_size = 16M max_allowed_packet = 1M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M datadir = /data0/data [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
也可以通过运行容器时来配置参数:
命令如下
docker run -d -p 3306:3306 -e MYSQL_USER="用户名" -e MYSQL_PASSWORD="密码" -e MYSQL_ROOT_PASSWORD="密码" --name mysql mysql/mysql-server:5.7 --character-set-server=utf8 --collation-server=utf8_general_ci
docker安装mysql文档: https://dev.mysql.com/doc/refman/5.7/en/docker-mysql-getting-started.html
个人建议 直接通过运行容器来配置参数