CentOS 7 使用docker安裝mysql

1.檢查內核版本,必須是3.10及以上

uname -r 

2.安裝docker

yum install docker -y

3.啓動docker

systemctl start docker

4.設置爲開機自啓

systemctl enable docker

5.搜索docker倉庫

docker search mysql

6下載mysql,這裏我安裝5.7

docker pull mysql:5.7

這裏可能會出現報錯

Trying to pull repository docker.io/library/mysql ... 
5.7: Pulling from docker.io/library/mysql
Get https://registry-1.docker.io/v2/library/mysql/manifests/sha256:b16d058ac835a0a087d4e42a5c200abc2a4936ec73ff7a427b28257f66bb5c04: net/http: TLS handshake timeout

更換爲國內的倉庫就好了,點擊這個鏈接https://www.daocloud.io/mirror 這裏面有每個平臺設置國內倉庫的方法

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

該腳本可以將 --registry-mirror 加入到你的 Docker 配置文件 /etc/docker/daemon.json 中。適用於 Ubuntu14.04、Debian、CentOS6 、CentOS7、Fedora、Arch Linux、openSUSE Leap 42.1,其他版本可能有細微不同。更多詳情請訪問文檔。
更換好以後重新下載mysql

docker pull mysql:5.7

等待下載完成後,我們就可以在本地鏡像列表裏查到剛下的mysql

docker images |grep mysql
最新官方MySQL(5.7.19)的docker鏡像在創建時映射的配置文件目錄有所不同,在此記錄並分享給大家:

官方原文:

The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.

大概意思是說:

MySQL(5.7.19)的默認配置文件是 /etc/mysql/my.cnf 文件。如果想要自定義配置,建議向 /etc/mysql/conf.d 目錄中創建 .cnf 文件。新建的文件可以任意起名,只要保證後綴名是 cnf 即可。新建的文件中的配置項可以覆蓋 /etc/mysql/my.cnf 中的配置項。

具體操作:

首先需要創建將要映射到容器中的目錄以及.cnf文件,然後再創建容器
# pwd
/opt
# mkdir -p docker_v/mysql/conf
# cd docker_v/mysql/conf
# touch my.cnf
# docker run -p 3306:3306 --name mysql -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
命令說明:

-p 3306:3306:將容器的3306端口映射到主機的3306端口
-v /opt/docker_v/mysql/conf:/etc/mysql/conf.d:將主機/opt/docker_v/mysql/conf目錄掛載到容器的/etc/mysql/conf.d
-e MYSQL_ROOT_PASSWORD=123456:初始化root用戶的密碼
-d: 後臺運行容器,並返回容器ID

查看容器運行情況

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