跳至主要內容

docker安装并配置mysql环境

Zenghr大约 4 分钟

docker下安装并配置mysql

Mysql 数据库是市面上最受欢迎的数据库,也是使用最多的一种数据库,所以开发者经常需要安装 Mysql 数据库环境,安装 Mysql 的方式有很多,Window、Linux 等系统可以下载 对应的安装文件安装,但是这些安装方式通常比较麻烦,也很容易出现安装错误。

那么可以选用 docker 来快速构建环境,容器比虚拟机更加轻量化,拥有诸多好处

提示

记录一下 docker 配置 mysql 环境的步骤

拉取 Mysql 镜像

docker 环境的搭建请查看其它文章

docker search mysql
docker pull mysql
# 拉取指定版本
docker pull mysql:5.7

也可以去官网查看镜像tagopen in new window,选择自己需要的版本,否则会下载最新版本

pSxDir
pSxDir

启动 Mysql 容器

docker run --name mysql \
--privileged=true \
-p 3306:3306 \
-v /database/mysql/conf:/etc/mysql/conf.d \
-v /database/mysql/logs:/logs \
-v /database/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7
  • -privileged:加入 -privileged=true 给容器加上特定权限,因为Centos7安全Selinux禁止了一些安全权限
  • -name:指定容器名称,必须是唯一
  • -p:将容器的 3306 端口映射到主机的 3306 端口。
  • -v:将主机的目录挂载到容器的目录
  • -e:初始化 root 用户的密码(注意如果没有初始化密码启动容器会失败
  • -d:容器后台运行
-v /database/mysql/conf:/etc/mysql/conf.d 挂载mysql配置文件
-v /database/mysql/logs:/logs 挂载日志文件
-v /database/mysql/data:/var/lib/mysql 挂载mysql数据, 否则容器重启之后数据会丢失

查看容器是否启动

docker ps

如果发现容器未能正常启动成功,则需要查看日志排查异常:docker logs mysql

Mysql 相关设置

1. 进入 mysql 容器

docker exec -it mysql /bin/bash
ajeCjb
ajeCjb

2. 连接 mysql

mysql -u root -p

输入启动容器时指定的密码

Hsrhxe
Hsrhxe

3. 查询 user 表

mysql> select host,user,plugin,authentication_string from mysql.user;
+-------------+---------------+-----------------------+-------------------------------------------+
| host        | user          | plugin                | authentication_string                     |
+-------------+---------------+-----------------------+-------------------------------------------+
| localhost   | root          | mysql_native_password | *39EF1E4F4220858A4F0D30FE91AAD7E4C4AB0812 |
| localhost   | mysql.session | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost   | mysql.sys     | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-------------+---------------+-----------------------+-------------------------------------------+
4 rows in set (0.00 sec)
  • host:localhost表示本机使用,也可以指定IP访问,为 % 表示不限制ip。
  • 如果这里plugin非mysql_native_password 则需要修改密码

4. 修改 root 账号

  • 修改root账号host
update user set host = "%" where user = "root" and host = "localhost"
  • 修改root账号密码
update user set password=password('admin') where user='root';

提示

注意:如果提示无法修改,这是因为 mysql从5.7.6 开始删除了password字段open in new window 需要换另一种方式修改密码

  • 换种方式修改
alter user 'root'@'%' identified by 'admin';
  • 刷新权限
flush privileges;

查看并修改mysql编码

  • 在容器中查看mysql配置文件
cat /etc/mysql/my.cnf
# Copyright (c) 2016, 2021, Oracle and/or its affiliates.
#
# 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/

我们的配置文件可以放到容器中的 /etc/mysql/conf.d/ 文件夹下,因为我们启动容器是已经挂载了目录到该目录下,所以我们只需要在那个文件夹里新建一个 my.cnf 文件即可

  • 在配置文件中写入配置
[mysqld]
character_set_server=utf8mb4
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
  • 重启 mysql
docker restart mysql
  • 查看 mysql 编码
mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

创建mysql 新用户

提示

关于 mysql 创建用户、数据库、以及给用户赋权等操作命令,请查看 mysql 系列文章 - 创建用户以及赋权