詳解docker 制作mysql鏡像並自動安裝腳本

centos7環境下 一鍵制作mysql docker鏡像,並安裝

centos7環境下 一鍵制作mysql docker鏡像,並安裝

shell腳本內容如下:

#docker自動安裝mysql
echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 默認安裝參數: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
echo -e "\033[32m '端口:3309' \033[0m"
echo -e "\033[32m '映射宿主機路徑: /mysql' \033[0m"
echo -e "\033[32m '密碼:xxxxxx' \033[0m"
echo -e "\033[32m '容器名稱:mysql8' \033[0m"
echo -e "\033[32m '開啟binlog' \033[0m"
echo -e "\033[32m '設置連接數1k' \033[0m"
echo -e "\033[32m '設置連接超時斷開24h' \033[0m"
echo -e "\033[32m '設置使用4個cpu核心' \033[0m"
mkdir -p /mysql
### 1下載鏡像
docker pull mysql
if [ $? -eq 0 ]
    then 
        echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1下載鏡像 finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
    else 
        echo -e "\033[31m '__________________________________  1下載鏡像 bad ,and EXIT' \033[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 "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> mymysql:1 鏡像構建成功 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
    else 
        echo -e "\033[31m '__________________________________  mymysql:1 鏡像構建失敗,and EXIT' \033[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 "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  2創建容器 finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
    else 
        echo -e "\033[31m '__________________________________  2創建容器 bad ,and EXIT' \033[0m"
        exit 1
fi
### 3啟動容器
docker start mysql8
if [ $? -eq 0 ]
    then 
        echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 3啟動容器 finish >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' \033[0m"
    else 
        echo -e "\033[31m '__________________________________  3啟動容器 bad ,and EXIT' \033[0m"
        exit 1
fi
### 顯示容器狀態
echo -e "\033[32m '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 顯示已運行容器:' \033[0m"
docker ps

到此這篇關於docker 制作mysql鏡像並自動安裝腳本的文章就介紹到這瞭,更多相關docker 制作mysql鏡像內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: