【Docker开发环境搭建】MySQL

🏷️当前篇为【Docker开发环境搭建】MySQL 🤖

🏷️更多开发环境搭建教程:开发环境搭建,手把手带你搭建Windows+Linux开发环境 🎈

🏷️欢迎点击MySQL基础教程,带你零基础上手MySQL数据库!💪

🏷️欢迎点赞 👍 收藏 🌟 关注 ❤️ 留言 ✍️

通过Docker部署MySQL服务可同时存在多个版本,只用拉取不同的版本且区分Docker启动MySQL容器的端口号及数据卷即可。

MySQL 5.7

# 拉取镜像
docker pull mysql:5.7.25
# 提前准备好配置文件及挂载目录
mkdir /usr/local/server /usr/local/server/mysql5.7 /usr/local/server/mysql5.7/conf/ /usr/local/server/mysql5.7/data/
touch /usr/local/server/mysql5.7/conf/my.cnf
vi /usr/local/server/mysql5.7/conf/my.cnf
# 将配置文件写入my.cnf

# Docker启动命令
docker run -d \
--name mysql5.7 \
--restart=always \
-p 3306:3306 \
-m 1024M \
-v /usr/local/server/mysql5.7/conf:/etc/mysql \
-v /usr/local/server/mysql5.7/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7.25

# 方式一:进入容器检查MySQL是否启动成功
[root@localhost local]# docker exec -it mysql5.7 bash
root@a77116498bc5:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

# 方式二:使用数据库可视化工具(如Navicat)连接数据库进行测试
# MySQL 5.7 Config
[client]
#password	= your_password
port		= 3306
socket		= /tmp/mysql.sock

[mysqld]
port		= 3306
socket		= /tmp/mysql.sock
datadir = /var/lib/mysql
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
binlog_cache_size = 64K
thread_stack = 256K
join_buffer_size = 1024K
query_cache_type = 1
max_heap_table_size = 64M
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 4K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 64
query_cache_size = 64M
tmp_table_size = 64M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

explicit_defaults_for_timestamp = true
#skip-name-resolve
max_connections = 100
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
slow_query_log=1
slow-query-log-file=/log/mysql/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on
early-plugin-load = ""


innodb_data_home_dir = /var/lib/mysql
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /var/lib/mysql
innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1

[mysqldump]
quick
max_allowed_packet = 500M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

MySQL 8

# 拉取镜像
docker pull mysql:8.0.18

# 提前准备好配置文件及挂载目录
mkdir /usr/local/server /usr/local/server/mysql8 /usr/local/server/mysql8/conf/ /usr/local/server/mysql8/data/
touch /usr/local/server/mysql8/conf/my.cnf
vi /usr/local/server/mysql8/conf/my.cnf
# 将配置文件写入my.cnf

# Docker启动命令
docker run -d \
--name mysql8 \
--restart=always \
-p 3306:3306 \
-m 1024M \
-v /usr/local/server/mysql8/conf:/etc/mysql \
-v /usr/local/server/mysql8/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:8.0.18

# 方式一:进入容器检查MySQL是否启动成功
[root@localhost conf]# docker exec -it mysql8 bash
root@6d7f706f1ef4:/# mysql -uroot -p      
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

# 方式二:使用数据库可视化工具(如Navicat)连接数据库进行测试
# MySQL 8 Config
[client]
#password	= your_password
port		= 3306
socket		= /tmp/mysql.sock

[mysqld]
secure_file_priv=
port		= 3306
socket		= /tmp/mysql.sock
datadir = /var/lib/mysql
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
binlog_cache_size = 64K
thread_stack = 256K
join_buffer_size = 1024K
max_heap_table_size = 64M
key_buffer_size = 128M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 4K
read_buffer_size = 768K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 32M
thread_cache_size = 64
tmp_table_size = 64M
default_authentication_plugin = mysql_native_password
lower_case_table_names = 1
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

explicit_defaults_for_timestamp = true
#skip-name-resolve
max_connections = 100
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id = 1
binlog_expire_logs_seconds = 600000
slow_query_log=1
slow-query-log-file=/log/mysql/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on
early-plugin-load = ""


innodb_data_home_dir = /var/lib/mysql
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /var/lib/mysql
innodb_buffer_pool_size = 256M
innodb_log_file_size = 256M
innodb_log_buffer_size = 64M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 4
innodb_write_io_threads = 4

[mysqldump]
quick
max_allowed_packet = 500M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 2M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值