随笔 - 1  文章 - 50  评论 - 22  阅读 - 37万

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';

 

posted on   BadTree  阅读(14212)  评论(4编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示