Docker安装mysql

27 篇文章 1 订阅
18 篇文章 0 订阅

环境

CentOS7

安装好Docker,可参考CentOS7安装Docker

 

步骤

官方文档:

https://registry.hub.docker.com/_/mysql

 

以安装mysql5.7为例,Linux使用root账户

 

1.拉取镜像

docker pull mysql:5.7

 

2.运行mysql容器

docker run --name mysql01 -p 3306:3306 -v /root/customconf:/etc/mysql/conf.d -v /root/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 -d mysql:5.7

-name 启动容器的名称,可随意取名,例如:mysql01

-p 端口映射  本地端口:容器端口

-v 挂载配置目录和数据目录  本地目录:容器目录   本地目录请根据实际情况修改(运行命令后,挂载的本地目录会被自动创建出来)

           -v /root/customconf:/etc/mysql/conf.d   挂载配置目录,方便修改配置文件

          -v /root/datadir:/var/lib/mysql  挂载数据目录,保证数据安全,即使删除容器,保存在容器外的数据不会被删除。

-e 环境变量,这里设置root的密码为123

-d 后台运行

 

 3.编写配置文件

进入容器

docker exec -it mysql01 /bin/bash

容器内执行命令,查看模板 

cat /etc/mysql/mysql.conf.d/mysqld.cnf

复制mysqld.cnf的内容

# Copyright (c) 2014, 2016, 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, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

退出容器

exit

在配置目录新建mysqld.cnf

进入配置目录

cd /root/customconf

新建mysqld.cnf文件

nano mysqld.cnf

粘贴刚才复制容器里mysqld.cnf的内容,然后保存。

如果以后要修改mysql相关配置,就修改容器外/root/customconf目录下的mysqld.cnf就可以了

 

4.连接mysql

使用navicat连接, 填入IP地址(CentOS的IP)、端口号、用户名、密码,点击连接测试,看到连接成功,说明能正常使用了。

 

 

完成!enjoy it!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值