Docker Mysql 安装配置
2019-10-18 本文已影响0人
五城十九洲
Mysql安装
拉取镜像,默认都是从 DockerHub 拉取
-
docker pull mysql:5.7
拉取mysql数据库
mysql 使用
-
启动mysql
MySQL 镜像地址根据镜像说明可知:
默认的配置文件是:/etc/mysql/my.cnf
默认的数据目录是:/var/lib/mysql
- 简单启动方式
docker run -p 3307:3306 --name mysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
- 绑定配置文件启动
宿主机中创建文件夹 mysql,并分别创建 data 目录和 conf 目录。新建配置文件 mysql_profile.cnf:
[mysqld]
server-id = 1 #服务Id唯一
port = 3307
log-error = /var/log/mysql/error.log
#只能用IP地址
skip_name_resolve
#数据库默认字符集
character-set-server = utf8mb4
#数据库字符集对应一些排序等规则
collation-server = utf8mb4_general_ci
#设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'
#最大连接数
max_connections = 300
接下来分别映射数据库目录和配置文件目录,启动容器:
docker run --name mysql -d --rm \
-v /home/txl/mysql/conf:/etc/mysql/conf.d \
-v /home/txl/mysql/data:/var/lib/mysql \
-p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
配置文件解释说明:
1、通过bash进入启动的mysql container
docker exec -it mysql(容器名) bash
2、查看my.cnf
> cat /etc/mysql/my.cnf
# Copyright (c) 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
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
主配置文件 my.cnf 加载了 /etc/mysql/conf.d 文件夹下所有的配置(后缀必须是 .cnf),因此只需映射 conf.d 文件夹即可。
使用容器客户端连接:
> docker exec -it mysql mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
MySQL默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid