首頁 > 軟體

詳解docker 製作mysql映象並自動安裝指令碼

2022-05-20 19:03:29

centos7環境下 一鍵製作mysql docker映象,並安裝

centos7環境下 一鍵製作mysql docker映象,並安裝

shell指令碼內容如下:

#docker自動安裝mysql
echo -e "33[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 預設安裝引數: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' 33[0m"
echo -e "33[32m '埠:3309' 33[0m"
echo -e "33[32m '對映宿主機路徑: /mysql' 33[0m"
echo -e "33[32m '密碼:xxxxxx' 33[0m"
echo -e "33[32m '容器名稱:mysql8' 33[0m"
echo -e "33[32m '開啟binlog' 33[0m"
echo -e "33[32m '設定連線數1k' 33[0m"
echo -e "33[32m '設定連線超時斷開24h' 33[0m"
echo -e "33[32m '設定使用4個cpu核心' 33[0m"
mkdir -p /mysql
### 1下載映象
docker pull mysql
if [ $? -eq 0 ]
    then 
        echo -e "33[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1下載映象 finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' 33[0m"
    else 
        echo -e "33[31m '__________________________________  1下載映象 bad ,and EXIT' 33[0m"
        exit 1
fi
###______________________________________________________________________________________________________
### 建立 dockerfile
rm -rf ./mymysql.yaml
echo '### dockerfile 自定義mysql容器映象' >> ./mymysql.yaml
echo 'FROM mysql:latest' >> ./mymysql.yaml
echo '#使用官方mysql映象最新版' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "###" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "log_bin = 1" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '#開啟binlog紀錄檔,儲存在預設位置' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "max_connections = 1050" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo 'RUN echo "mysqlx_max_connections = 1000" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '#最大連線數' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "wait_timeout=86400" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo 'RUN echo "interactive_timeout=86400" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '#空閒斷開時長,秒' >> ./mymysql.yaml
echo '' >> ./mymysql.yaml
echo 'RUN echo "innodb_read_io_threads=4" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo 'RUN echo "innodb_write_io_threads=4" >> /etc/mysql/my.cnf' >> ./mymysql.yaml
echo '#設定使用多核cpu,預設使用4核,根據cpu核心數設定' >> ./mymysql.yaml
###______________________________________________________________________________________________________
###使用mymysql.yaml構建映象mymysql:1
docker build -f ./mymysql.yaml -t "mymysql:1" .
if [ $? -eq 0 ]
    then 
        echo -e "33[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> mymysql:1 映象構建成功 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' 33[0m"
    else 
        echo -e "33[31m '__________________________________  mymysql:1 映象構建失敗,and EXIT' 33[0m"
        exit 1
fi
### 2建立容器
docker create -p 3309:3306 --restart=always --name mysql8 -v /mysql/mysql_08/log:/var/log/mysql -v /mysql/mysql_08/data:/var/lib/mysql -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=xxxxx -it  mymysql:1
if [ $? -eq 0 ]
    then 
        echo -e "33[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  2建立容器 finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' 33[0m"
    else 
        echo -e "33[31m '__________________________________  2建立容器 bad ,and EXIT' 33[0m"
        exit 1
fi
### 3啟動容器
docker start mysql8
if [ $? -eq 0 ]
    then 
        echo -e "33[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3啟動容器 finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' 33[0m"
    else 
        echo -e "33[31m '__________________________________  3啟動容器 bad ,and EXIT' 33[0m"
        exit 1
fi
### 顯示容器狀態
echo -e "33[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 顯示已執行容器:' 33[0m"
docker ps

到此這篇關於docker 製作mysql映象並自動安裝指令碼的文章就介紹到這了,更多相關docker 製作mysql映象內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com