mysql docker(解決:Failed to find valid data directory)

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)的文件,重新啟動容器

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容