Docker安装MySQL8

28 篇文章 0 订阅

 

进入docker 有时要用: docker exec -it mysql8 bash

 

 

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

 1. 拉取mysql镜像

docker pull mysql

 

2. 运行容器

 rw是读写,如果指定为只读可以用:ro

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

[mysql]  
default-character-set=utf8  
lower_case_table_names=1

 

 

 

或进入mysql后
ALTER user 'root'@'%' IDENTIFIED BY '123'  PASSWORD EXPIRE NEVER;
ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123';

flush privileges;

如果你没有配置好就启动容器,容器初始化完成后,第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';

 

 

 

 

可能异常:

 

注意:启动mysql报如下错误,那是因为MYSQL新特性secure_file_priv对读写文件的影响。

复制代码

ERROR: mysqld failed while attempting to check config
command was: "mysqld --verbose --help"

mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
2019-09-14T09:52:51.015937Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
2019-09-14T09:52:51.018328Z 0 [ERROR] [MY-010119] [Server] Aborting

复制代码

解决问题:
windows下:修改my.ini 在[mysqld]内加入secure_file_priv=/var/lib/mysql

linux下:修改my.cnf 在[mysqld]内加入secure_file_priv=/var/lib/mysql

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值