Mac Docker下創建 MySQL 8.0 容器

 

MacOS下下載安裝Docker,並pull mysql image我在很早之前做過了。不贅述了,很簡單。

以下步驟簡單記錄我重新創建MySQL容器的命令,供日後參考。

啓動Docker後,打開terminal,先查看現有images: docker images,我發現我有兩個mysql鏡像,於是刪掉一個: docker rmi imageID

(base) Sophie-mbp:~ Sophie$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              dcf9ec9265e0        5 months ago        98.2MB
mysql               5.7                 1e4405fe1ea9        5 months ago        437MB
mysql               latest              d435eee2caa5        5 months ago        456MB
hello-world         latest              fce289e99eb9        15 months ago       1.84kB
(base) Sophie-mbp:~ Sophie$ docker rmi 1e4405fe1ea9

然後在Host機器目錄(也就是我的電腦本尊)創建三個文件夾,分別用於存放mysql容器的data/config/log,並在docker裏面配置進去:Preferences --> Resources --> FILE SHARING,然後Apply&Restart。

接下來創建並啓動MySQL容器,把剛剛這三個目錄mount到這個容器。

-p 3306:3306意思是將host機器的3306端口映射到docker container的3306端口

--name是配置容器名爲MySQL

-v就是將host目錄跟container目錄映射起來,這樣將來container沒了,數據,配置,Log都還在

-e配置容器root密碼

-d配置該容器來自哪個鏡像

docker run -p 3306:3306 --name MySQL -v /Users/Sophie/Applications/docker/conf:/etc/mysql -v /Users/Sophie/Applications/docker/log:/var/log/mysql -v /Users/Sophie/Applications/docker/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=wobugaosunihahaha -d mysql:latest

然而不幸的是,我這個容器起起來就exit了...docker ps -a能看到過去所有的沒有被刪除的容器

然後用docker logs 容器ID查看日誌(容器ID只用給出開頭一部分字符,且能在所有容器ID裏面確定唯一目標容器的開頭部分字符即可)。由於我只有一個容器,所以我給出一個c就能唯一確定我想要查看的容器日誌。

(base) Sophie-mbp:docker Sophie$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES
cd8943abd16b        mysql:latest        "docker-entrypoint.s…"   12 hours ago        Exited (1) 12 hours ago                       MySQL
(base) Sophie-mbp:docker Sophie$ docker logs c
2020-04-24 03:22:00+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
2020-04-24 03:22:00+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-04-24 03:22:00+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
2020-04-24 03:22:00+00:00 [Note] [Entrypoint]: Initializing database files
2020-04-24T03:22:00.887123Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 45
2020-04-24T03:22:11.425922Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2020-04-24 03:22:20+00:00 [Note] [Entrypoint]: Database files initialized
2020-04-24 03:22:20+00:00 [Note] [Entrypoint]: Starting temporary server
mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
2020-04-24T03:22:20.621512Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
2020-04-24 03:22:20+00:00 [ERROR] [Entrypoint]: Unable to start server.

在日誌裏可以明確看出,問題出在 mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)

就是說,我少掛載了一個目錄,容器的/var/lib/mysql-files目錄需要跟host的某個路徑映射起來。

那麼,刪掉這個起不來的容器,重新來過吧,docker rm c (如果現有容器比較多的話,容器ID給長一點,由於我的目前只有一個

容器,那麼只用給出一個開頭字母就好了)

(base) Sophie-mbp:docker Sophie$ docker rm c
c
(base) Sophie-mbp:docker Sophie$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
(base) Sophie-mbp:docker Sophie$ 

很不幸,還是沒有起來,docker ps裏沒有運行狀態的容器,docker ps -a能看到我可憐的容器起來5秒就exit了。繼續查看Log 

(base) Sophie-mbp:docker Sophie$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
234d8e52707b        mysql:latest        "docker-entrypoint.s…"   25 seconds ago      Exited (1) 20 seconds ago                       MySQL
(base) Sophie-mbp:docker Sophie$ docker logs 2
2020-04-24 15:07:22+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
2020-04-24 15:07:23+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-04-24 15:07:23+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
2020-04-24T15:07:24.154280Z 0 [Warning] [MY-010101] [Server] Insecure configuration for --secure-file-priv: Location is accessible to all OS users. Consider choosing a different directory.
2020-04-24T15:07:24.154360Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 1
2020-04-24T15:07:24.162732Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /var/lib/mysql/ is case insensitive
2020-04-24T15:07:25.113266Z 1 [ERROR] [MY-011087] [Server] Different lower_case_table_names settings for server ('2') and data dictionary ('0').
2020-04-24T15:07:25.113716Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2020-04-24T15:07:25.113940Z 0 [ERROR] [MY-010119] [Server] Aborting
2020-04-24T15:07:25.743324Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.18)  MySQL Community Server - GPL.

 

 

https://docs.docker.com/compose/compose-file

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