mysql 安装部署(Docker - Centos7)

mysql 安装部署(Docker - Centos7)

 

1. 拉取镜像

docker pull mysql

 

docker pull mysql:5.7

 

查看镜像

docker images

 

 

2. 启动容器

docker run -d -p 3306:3306  \
--restart=always \
-v /usr/mysql/log:/var/log/mysql \
-v /usr/mysql/data:/var/lib/mysql \-v /usr/mysql/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=root \
--name mysql8 mysql:latest

 

 配置文件在容器内,路径:/etc/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

# Custom config should go here

 

 说明:

-p 3306:3306 --name mysql
将容器的3306端口映射到主机的3306端口
配置mysql数据卷挂载
1.-v /usr/mysql/log:/var/log/mysql(日志文件挂载) 将容器中的日志文件夹/var/log/mysql挂载到主机对应的/usr/mysql文件夹中
2.-v /usr/mysql/data:/var/lib/mysql(数据文件挂载) 将容器中的数据文件夹/var/lib/mysql挂载到主机对应的/usr/mysql/data文件夹中
3.-v /usr/mysql/conf:/etc/mysql(配置文件挂载)(映射出来为空文件夹,不知道什么情况) 将容器的配置文件夹/etc/mysql挂载到主机对应的/usr/mysql/conf文件夹中

3.-v /usr/mysql/mysql-files:/var/lib/mysql-files(配置文件挂载) 
将容器的配置文件夹/var/lib/mysql-files挂载到主机对应的/usr/mysql/mysql-files文件夹中
注(这里所提的主机指的是当前的linux主机)
配置用户
-e MYSQL_ROOT_PASSWORD=123456 设置初始化root用户的密码为123456 (没用效,密码为空,进入容器,进入mysql 修改)
指定镜像资源
-d mysql:5.7 -d:以后台方式运行实例 mysql:5.7:指定用这个镜像来创建运行实例
--restart=always 开机启动

 

 

 如启动失败,查看docker记录

docker logs 71fc5ea15565

 

 

 使用 sqlyog 连接时,出现如下错误

 

  进入mysql 容器

docker exec -it mysql /bin/bash

 进入mysql

mysql -uroot -p

 修改密码 

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
flush privileges;

 

 

修改root 的访问域

update mysql.user set host = '%' where user = 'root';

flush privileges;

 

 

sqlyon连接成功!

 

 

 

 

 

 

 

 

 

 

 

参考:https://blog.csdn.net/weixin_43830765/article/details/123849821

 

posted @ 2023-01-10 10:13  无心々菜  阅读(188)  评论(0编辑  收藏  举报