1 缘起
开启容器化之路。
2 容器化MySQL
2.1 查看MySQL镜像
docker search mysql
2.2 指定版本:5.7.30
通过官网查看:https://hub.docker.com/
docker pull mysql:5.7.30
2.3 路径挂载
容器路径挂载到宿主机。
- 新建宿主机路径
mkdir -p /home/xindaqi/mysql/data /home/xindaqi/mysql/logs /home/xindaqi/mysql/conf
序号 | 路径 | 描述 |
---|---|---|
1 | /home/xindaqi/mysql/data | MySQL存储的数据 |
2 | /home/xindaqi/mysql/logs | MySQL日志 |
3 | /home/xindaqi/mysql/conf | MySQL配置 |
- 添加配置文件:my.cnf
/home/xindaqi/mysql/conf/my.cnf
[mysqld]
user=mysql
bind-address = 0.0.0.0
character-set-server=utf8
default_authentication_plugin=mysql_native_password
expire_logs_days=7
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
2.4 运行
序号 | 参数 | 描述 |
---|---|---|
1 | p | 服务端口映射,宿主机端口:容器内端口 |
2 | name | 服务名称 |
3 | v | 路径挂载,宿主机路径:容器内路径 |
4 | e | MySQL root账户密码 |
- 前台运行
docker run -p 3306:3306 \
--name mysql \
-v /home/xindaqi/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /home/xindaqi/mysql/logs:/var/log/mysql \
-v /home/xindaqi/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=my$123456 \mysql:5.7.30
- 后台运行
docker run -p 3306:3306 \
--name mysql \
-v /home/xindaqi/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /home/xindaqi/mysql/logs:/var/log/mysql \
-v /home/xindaqi/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=my$123456 \
-d mysql:5.7.30
2.5 查看容器
docker container ls | grep mysql
2.6 进入容器
# 格式
docker exec -it container_id /bin/bash
# 样例
docker exec -it 916f93180fb2 /bin/bash
2.7 登录MySQL:Ubuntu内
- 查看系统IP
ifconfig
使用enth0的网络IP登录MySQL。
- 登录MySQL
mysql -h 172.22.75.234 -uroot -pmy$123456
2.8 新建用户
用于其他服务使用,
因为,使用root登录时,无法接入MySQL,所以新建用户。
# 新增用户:格式create user username@ip identified by password
create user 'mysql-username'@'%' identified by 'my$123456';
序号 | 参数 | 描述 |
---|---|---|
1 | mysql-username | 用户名 |
2 | % | IP,其中,%表示允许所有IP连接 |
3 | my$123456 | 密码 |
# 授权,格式:grant operation on database to username@ip identified by password
grant all on *.* to 'mysql-username'@'%' identified by 'my$123456';
序号 | 参数 | 描述 |
---|---|---|
1 | all | 所有操作,单独配置操作:select、insert、update和delete |
2 | . | 表示所有数据库,可以单独指定某个数据库和某些表,如database-name.*,database-name.table-name |
3 | mysql-username | 用户名 |
4 | my$123456 | 用户密码 |
# 生效
flush privileges;
查看用户
select host, user from mysql.user;
3 FAQ
3.1 为什么指定密码
不指定密码报错信息:
3.2 配置文件
待描述。
[client]
port = 3306
socket = /usr/local/services/mysql/var/data/mysql.sock[mysqld]
bind-address = 0.0.0.0
port = 3306
socket = /usr/local/services/mysql/var/data/mysql.sock
pid-file = /usr/local/services/mysql/var/logs/mysql.pid
character-set-server = utf8
basedir = /usr/local/services/mysql
datadir = /usr/local/services/mysql/var/dataskip-external-locking
skip-name-resolve
lower_case_table_names = 1
log-bin-trust-function-creators = 1max_connections = 6000
max_user_connections = 6000
max_connect_errors = 4000
wait_timeout = 86400
interactive_timeout = 86400
table_open_cache = 512
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 8
thread_concurrency = 8
query_cache_size = 32M
#default-storage-engine = InnoDB#sql_mode="STRICT_ALL_TABLES,NO_AUTO_CREATE_USER"
server-id = 1log-short-format
log-error = /usr/local/services/mysql/var/logs/mysql.log
slow_query_log
long_query_time = 2
slow_query_log_file = /usr/local/services/mysql/var/logs/mysql-slow.loglog-bin = /usr/local/services/mysql/var/binlog/mysql-bin
log_bin_trust_function_creators=1
binlog_format = MIXED
expire_logs_days = 10# INNODB Specific options
innodb_data_home_dir = /usr/local/services/mysql/var/data
innodb_log_group_home_dir = /usr/local/services/mysql/var/redolog
innodb_additional_mem_pool_size = 10M
innodb_buffer_pool_size = 4G
innodb_data_file_path = ibdata1:100M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 0
innodb_flush_method = O_DIRECT
innodb_log_buffer_size = 128M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 50
innodb_file_per_table = 1# MyISAM Specific options
key_buffer_size = 384M
read_buffer_size = 4M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1
myisam_recover[mysqldump]
quick
max_allowed_packet = 16M[mysql]
default-character-set = utf8
no-auto-rehash
socket = /usr/local/services/mysql/var/data/mysql.sock[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M[mysqlhotcopy]
interactive-timeout