docker安装mysql

  1. 创建挂载目录
mkdir -p /usr/local/src/mysql/data
mkdir -p /usr/local/src/mysql/conf
  1. /usr/local/src/mysql/conf/目录下新建my.cnf文件
#
## my.cnf for 8.0版本
## author: yejr(yejinrong@zhishutang.com, http://imysql.com, QQ: 4700963)
##
## 叶金荣(yejr)
## 国内知名MySQL专家,MySQL布道师,Oracle MySQL ACE Director,腾讯云TVP成员。
## 微信公众:老叶茶馆(imysql_wx), 博客:https://imysql.com
## QQ群: 125572178
## 注意:个别建议可能需要根据实际情况作调整,请自行判断或联系我,本人不对这些建议结果负相应责任
## 本配置文件主要适用于MySQL 8.0版本
#
[client]
port    = 3306

[mysql]
prompt = "\u@VM-0-8-centos \R:\m:\s [\d]> "
no_auto_rehash

[mysqld]
user    = mysql
port    = 3306
#主从复制或MGR集群中,server_id记得要不同
#另外,实例启动时会生成 auto.cnf,里面的 server_uuid 值也要不同
#server_uuid的值还可以自己手动指定,只要符合uuid的格式标准就可以
server_id = 3306
#basedir        = /var/lib/mysql
datadir = /var/lib/mysql
socket  = /var/run/mysqld/mysqld.sock
pid_file = /var/run/mysqld/mysqld.pid
character_set_server = UTF8MB4
skip_name_resolve = 1
#若你的MySQL数据库主要运行在境外,请务必根据实际情况调整本参数
default_time_zone = "+8:00"
#启用admin_port,连接数爆满等紧急情况下给管理员留个后门
admin_address = '127.0.0.1'
admin_port = 33062

#performance setttings
lock_wait_timeout = 3600
open_files_limit    = 65535
back_log = 1024
max_connections = 1024
max_connect_errors = 1000000
table_open_cache = 400
table_definition_cache = 400
thread_stack = 512K
sort_buffer_size = 4M
join_buffer_size = 4M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
thread_cache_size = 1536
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M

#log settings
log_timestamps = SYSTEM
log_error = /var/lib/mysql/data/error.log
log_error_verbosity = 3
slow_query_log = 1
log_slow_extra = 1
slow_query_log_file = /var/lib/mysql/data/slow.log
long_query_time = 0.1
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 100
log_slow_admin_statements = 1
log_slow_slave_statements = 1

#myisam settings
key_buffer_size = 15M
myisam_sort_buffer_size = 128M

#innodb settings
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 4014M
innodb_buffer_pool_instances = 4
innodb_data_file_path = ibdata1:12M:autoextend
innodb_flush_log_at_trx_commit = 1 #MGR环境中由其他节点提供容错性,可不设置双1以提高本地节点性能
innodb_log_buffer_size = 32M
innodb_log_file_size = 1G #如果线上环境的TPS较高,建议加大至1G以上,如果压力不大可以调小
innodb_log_files_in_group = 3
innodb_max_undo_log_size = 4G
# 根据您的服务器IOPS能力适当调整
# 一般配普通SSD盘的话,可以调整到 10000 - 20000
# 配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_open_files = 65535
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_online_alter_log_max_size = 4G
innodb_print_ddl_logs = 1
innodb_status_file = 1
#注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log_error文件增长较快
innodb_status_output = 0
innodb_status_output_locks = 1
innodb_sort_buffer_size = 67108864
innodb_adaptive_hash_index = OFF

#innodb monitor settings
innodb_monitor_enable = "module_innodb"
innodb_monitor_enable = "module_server"
innodb_monitor_enable = "module_dml"
innodb_monitor_enable = "module_ddl"
innodb_monitor_enable = "module_trx"
innodb_monitor_enable = "module_os"
innodb_monitor_enable = "module_purge"
innodb_monitor_enable = "module_log"
innodb_monitor_enable = "module_lock"
innodb_monitor_enable = "module_buffer"
innodb_monitor_enable = "module_index"
innodb_monitor_enable = "module_ibuf_system"
innodb_monitor_enable = "module_buffer_page"
#innodb_monitor_enable = "module_adaptive_hash"

#pfs settings
performance_schema = 1
#performance_schema_instrument = '%memory%=on'
performance_schema_instrument = '%lock%=on'

[mysqldump]
quick

怎么让 my.cnf include 其他配置文件呢?在 my.cnf 内加如下内容:

!includedir /etc/mysql/conf.d/
则除加载 my.cnf 配置文件外,还会到/etc/mysql/conf.d/目录下加载其他配置文件。当然可以配置多个!includedir

这样做的好处是可以把不同目的或功能的配置放在不同的多个文件下,然后由 my.cnf 去统一加载这些配置。避免所有配置在一个文件中。

3.创建容器

docker run -d --name=mysql8.0 \
--privileged=true \
-v /usr/local/src/mysql/data:/var/lib/mysql \
-v /usr/local/src/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-e TZ=Asia/Shanghai \
-p 3306:3306 \
--restart=always \
-e MYSQL_ROOT_PASSWORD=abc@123 mysql:8.0 \
--lower_case_table_names=1 
  1. 修改权限密码方式
[root@promote conf]# docker exec -it mysql /bin/bash
root@d941b9688fdf:/# mysql -u root -p
Enter password: 
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'os10+HLH';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

如果登陆出现错误:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysql.sock'

尝试检查my.cnf,关于socket设置

[client]
   #socket  = /var/run/mysqld/mysqld.sock //注释掉
[mysqld]   
   socket  = /var/run/mysqld/mysqld.sock #是否为此mysqld.sock名称
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值