docker運行mysql(免安裝mysql),配置文件數據文件都共享到宿主機中

廢話不多說,直接進入正題。

下面是我操作的步驟(root賬號操作),及遇到的問題和解決辦法:

拉取mysql最新鏡像,運行容器並制定掛在路徑,設置root初始密碼(沒有用的,但是不影響執行)

docker run --name=mysql -p 3306:3306 -v /home/mysql/var/run/mysqld:/var/run/mysqld -v /home/mysql/var/lib/mysql:/var/lib/mysql -v /home/mysql/etc/mysql:/etc/mysql -v /home/mysql/var/lib/mysql-files:/var/lib/mysql-files/ --privileged=true  -e MYSQL_ROOT_PASSWORD=123456  -d mysql:latest

坑1:

[ERROR] [MY-010273] [Server] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.

容器不能啓動報錯。用docker logs –f a40848c0ac06 查看日誌發現錯誤

Initializing database
2019-06-26T03:02:31.827828Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server in progress as process 27
2019-06-26T03:02:33.775142Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2019-06-26T03:02:34.527527Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server has completed
Database initialized
MySQL init process in progress...
MySQL init process in progress...
2019-06-26T03:02:35.988648Z 0 [Warning] [MY-010101] [Server] Insecure configuration for --secure-file-priv: Location is accessible to all OS users. Consider choosing a different directory.
2019-06-26T03:02:35.988690Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 78
2019-06-26T03:02:36.991298Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-06-26T03:02:36.991588Z 0 [ERROR] [MY-010273] [Server] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.
2019-06-26T03:02:36.991597Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
MySQL init process in progress...
MySQL init process in progress...
2019-06-26T03:02:36.991778Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-06-26T03:02:39.086053Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.16)  MySQL Community Server - GPL.
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process failed.
2019-06-26T03:10:02.580247Z 0 [Warning] [MY-010101] [Server] Insecure configuration for --secure-file-priv: Location is accessible to all OS users. Consider choosing a different directory.
2019-06-26T03:10:02.580288Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 1
2019-06-26T03:10:02.983867Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-06-26T03:10:02.993561Z 0 [ERROR] [MY-010273] [Server] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.
2019-06-26T03:10:02.993582Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
2019-06-26T03:10:02.994647Z 0 [ERROR] [MY-010119] [Server] Aborting

這個是因爲容器啓動的時候,掛載到宿主機的目錄權限有問題。需要修改

解決辦法:

chown -R polkitd:input /home/mysql

坑2:

然後就是掛載出來,mysql配置文件丟失。使用起來很不方便。

解決辦法:將容器中的mysql配置文件copy出來,然後再重啓。具體操作如下:

1、創建容器的時候,可以掛在一個容器中空目錄比如 /mysql/conf

2、容器起來以後,docker exec -it 28b941608555 /bin/bash 進入容器,創建目錄/mysql/conf,然後將/etc/mysql中的配置文件拷貝到/mysql/conf中,然後宿主機中對應的目錄就有配置文件了cp /etc/mysql/* /mysql/conf –r

3、最後將現在的容器刪掉,重新對應掛載-v /opt/mysql/etc/mysql:/etc/mysql 創建新容器。

4、這樣就可以通過宿主機中的配置,來改變docker容器中的mysql服務。

 

坑3:

[root@localhost home]# mysql -uroot -p123456

發現沒有mysql命令,這是因爲沒有mysql客戶端

解決辦法: yum install -y mariadb.x86_64 mariadb-libs.x86_64

 

坑4:

[root@localhost home]# mysql -uroot -p123456

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

解決辦法:ln -s /home/mysql/var/run/mysqld/mysqld.sock /var/lib/mysql/mysql.sock

 

坑5:

 [root@localhost home]# mysql -h10.10.2.53 -uroot -p123456

ERROR 2003 (HY000): Can't connect to MySQL server on '10.10.2.53' (111)

說明:不能遠程訪問訪問

[root@localhost mysqld]# mysql -uroot -p123456

ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory

說明:遠程訪問,密碼加密方式不對,需要使用caching_sha2_password來處理(現在的客戶端應該使用的是mysql_native_password)

分析原因:綜合起來是用戶的問題、密碼加密的問題。

最初的用戶權限表是這樣的:(SELECT `Host`, `User`, `plugin`, authentication_string, password_expired FROM `user`)

從這個表可以看出,root用戶是不能遠程訪問的。可以本地訪問,不需要密碼。

解決辦法:

本地登錄mysql服務,增加root遠程登錄,並且使用mysql_native_password加密方式

要實現本地登錄容器中運行的mysql服務,有兩種方式

方式1:docker exec –it a40848c0ac06 bash 進入容器後,mysql -uroot –p 不需要輸入密碼

方式2:docker exec –it a40848c0ac06 mysql -uroot –p 不需要輸入密碼

然後增加root遠程登錄,並且使用mysql_native_password加密方式

create USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

flush privileges;

 

到此出就好了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章