https://hub.docker.com/_/mysql
1.拉取鏡像
docker pull mysql
2.運行容器(這步可能會有錯誤,看文章最后的解決辦法)
第一條命令:普通執(zhí)行
第二條命令:宿主機掛載 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 基本的內(nèi)容:
# 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;
可能發(fā)生的過程錯誤
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 地址內(nèi)部有文件,可能之前失敗過一次,文件有殘留
解決辦法:情況 data 內(nèi)的文件,重新啟動容器