docker官网搜索镜像地址:https://hub.docker.com/_/mysql
-
下载mysql镜像
docker pull mysql:5.7
-
使用该镜像启动容器
docker run --name mysql -p 3306:3306 -v /mysql/conf:/etc/mysql/conf.d -v /mysql/logs:/logs -v /mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
命令说明:
-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。
-v /mysql/conf:/etc/mysql/conf.d:将主机/mysql/conf挂载到容器的 /etc/mysql/conf.d。
-v /mysql/logs:/logs:将主机/mysql/logs挂载到容器的 /logs。
-v /mysql/data:/var/lib/mysql :将主机/mysql/data挂载到容器的 /var/lib/mysql 。
-e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
-
查看容器运行情况
docker ps
-
配置授权远程访问
docker exec -it mysql /bin/bash
mysql -uroot -p123456
grant all on *.* to 'root'@'%';
flush privileges;
-
新建my.cnf文件,内容如下,放入/mysql/conf 目录。
[mysql]
default-character-set = utf8mb4
[mysqld]
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
#common
default-time_zone = '+8:00'
max_connections = 2000
tmp_table_size = 200M
character-set-server = utf8mb4
max_allowed_packet = 32M
lower_case_table_names = 1
log-error = mysql-error.log
slow_query_log = on
slow-query-log-file = mysql-slow.log
long_query_time = 2
log-queries-not-using-indexes = mysql-nouseindex.log
#innodb
innodb_buffer_pool_size = 1G
innodb_thread_concurrency = 16
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_lock_wait_timeout = 120