https://hub.docker.com/_/mysql
1.拉取镜像
docker pull mysql
2.运行容器(这步可能会有错误,看文章最后的解决办法)
第一条命令:普通执行
第二条命令:宿主机挂载 data 文件,并设置密码,以当前为例,需要先新建几个文件夹
mkdir /opt/mysql/data
mkdir /opt/mysql/conf.d
docker run --name stb_mysql -e MYSQL_ROOT_PASSWORD=12345678 -d mysql
docker run -p 3306:3306 -d --name=stb_mysql -e MYSQL_ROOT_PASSWORD=12345678 -v /opt/mysql/data/:/var/lib/mysql -v /opt/mysql/conf.d:/etc/mysql/conf.d mysql
// 如果需要自己定义 cnf 配置,加上-v /opt/mysql/my.cnf:/etc/mysql/my.cnf (需要自己建立 my.cnf)
docker run -p 3306:3306 -d --name=stb_mysql -e MYSQL_ROOT_PASSWORD=12345678 -v /opt/mysql/data/:/var/lib/mysql -v /opt/mysql/conf.d:/etc/mysql/conf.d -v /opt/mysql/my.cnf:/etc/mysql/my.cnf mysql
my.cnf 基本的内容:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
3.日志查看
docker logs stb_mysql 2>&1 | grep GENERATED
4.进入检查,并允许远程登录,或者修改密码(docker 默认可省略这步)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '12345678';
use mysql;
UPDATE mysql.user SET Host='%' WHERE Host='localhost' AND User='root';
FLUSH PRIVILEGES;
可能发生的过程错误
2023-03-16T07:05:55.887622Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2023-03-16T07:05:55.887830Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2023-03-16T07:05:55.887924Z 0 [ERROR] [MY-010119] [Server] Aborting
原因:挂载映射的 data 地址内部有文件,可能之前失败过一次,文件有残留
解决办法:情况 data 内的文件,重新启动容器
网友评论