Docker安裝MySQL8


在安裝前,最好上 https://hub.docker.com 看一下mysql的版本,我本人一般都最用最新版本。

 1. 拉取mysql鏡像

docker pull mysql

 

2. 運行容器

docker run \
    -p 3306:3306 \
    -e MYSQL_ROOT_PASSWORD=123456 \
    -v /home/data/mysql/data:/var/lib/mysql:rw \
    -v /home/data/mysql/log:/var/log/mysql:rw \
    -v /home/data/mysql/config/my.cnf:/etc/mysql/my.cnf:rw \
    -v /etc/localtime:/etc/localtime:ro \
    --name mysql8 \
    --restart=always \
    -d mysql

提前要在提定的位置(我的位置是:/home/data)創建以下文件夾或文件:

  • mysql/data 是數據庫文件存放的地方。必須要掛載到容器外,否則容器重啟一切數據消失
  • mysql/log 是數據庫主生的log。建議掛載到容器外。
  • mysql/config/my.cnf 是數據庫的配置文件,在下面會放出來。
  • /etc/localtime:/etc/localtime:ro 是讓容器的時鍾與宿主機時鍾同步,避免時區的問題,ro是read only的意思,就是只讀。

3. 配置文件 (這個文件是我進容器里面抄出來的,因為內需要改動一點點) 

 default_authentication_plugin= mysql_native_password (這個是因應mysql8的安全機制升級而需要修改的配置,不配置的話將無法登錄管理)

# 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/

default_authentication_plugin= mysql_native_password

 

如果你沒有配置好就啟動容器,容器初始化完成后,第3點的配置是不會生效的。你需要進入容器進行以下操作:

其間按提示會要求你輸入root用戶的password,就是上面我們設置的參數 “MYSQL_ROOT_PASSWORD”

[root@centos ~]# docker exec -it mysql8 /bin/sh

# mysql -uroot -p

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM