開發與維運

Docker 安裝 mysql8

hailuo_579095140_RF.jpg
鏡像下載、域名解析、時間同步請點擊 阿里巴巴開源鏡像站

一、下載mysql8鏡像

docker pull mysql

二、創建mysql8配置文件

vi /etc/my.cnf #編輯MySQL配置文件

my.cnf文件內容

# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Custom config should go here
!includedir /etc/mysql/conf.d/

三、創建mysql容器

docker run -p 60306:3306 -e MYSQL_ROOT_PASSWORD=123 -v /etc/my.cnf:/etc/mysql/my.cnf:rw -v /etc/localtime:/etc/localtime:ro --name mysql8 --restart=always -dit mysql
-p 60306:3306 #本機60306端口映射到容器3306端口
-e MYSQL_ROOT_PASSWORD=123 #設置MySQL的root用戶密碼
-v /etc/my.cnf:/etc/mysql/my.cnf:rw #本機的MySQL配置文件映射到容器的MySQL配置文件
-v /etc/localtime:/etc/localtime:ro #本機時間與數據庫時間同步
--name mysql8 #設置容器別名
--restart=always #當重啟Docker時會自動啟動該容器
-dit mysql #後臺運行並可控制檯接入

四、進入mysql控制檯

docker exec -it b6cfb244d0c0 bash #進入MySQL容器
mysql -uroot -p123 #進入MySQL控制檯
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; #修改root用戶密碼

阿里巴巴開源鏡像站 提供全面,高效和穩定的系統鏡像、應用軟件下載、域名解析和時間同步服務。”

Leave a Reply

Your email address will not be published. Required fields are marked *