Docker 安装 MySQL、Redis
1 Docker 中安装 Redis
1.1 创建目录
在硬盘上创建 redis 的数据目录:
mkdir -p /Users/yygnb/dockerMe/redis/data
为该目录添加权限:
chown 200 -R /Users/yygnb/dockerMe/redis
1.2 拉取镜像
搜索镜像:
docker search redis
搜索结果:
拉取官方镜像:
docker pull redis
1.3 启动容器
映射端口 6379 和 data 目录,启动容器:
docker run -itd -p 6379:6379 \
-v /Users/yygnb/dockerMe/redis/data:/data \
--restart always \
--name redis \
redis
1.4 查看 Redis 状态
docker ps -a | grep redis
Redis 容器运行后,可以通过 RDM 等客户端工具尝试连接。
2 Docker 中安装 MySQL
2.1 创建目录
在硬盘上分别创建 conf(配置文件目录)、data(数据文件目录)、logs(日志文件目录):
mkdir -p /Users/yygnb/dockerMe/mysql/conf /Users/yygnb/dockerMe/mysql/logs /Users/yygnb/dockerMe/mysql/data
为上面三个目录添加权限:
chown 200 -R /Users/yygnb/dockerMe/mysql
2.2 拉取镜像
docker search mysql
搜索结果:
拉取官方镜像:
docker pull mysql
2.3 创建配置文件
进入上面创建的 mysql/conf 目录下,创建配置文件 my.cnf
:
cd /Users/yygnb/dockerMe/mysql/conf
touch my.cnf
vi my.cnf
配置文件 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
# //dev.mysql.com/doc/mysql/en/server-system-variables.html
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[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
max_connections=10000
default-time_zone='+8:00'
character-set-client-handshake=FALSE
character_set_server=utf8mb4
collation-server=utf8mb4_general_ci
init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_general_ci'
# Custom config should go here
!includedir /etc/mysql/conf.d/
2.4 启动容器
启动容器,指定3306 端口映射和上面的三个目录映射,同时也指定 MySQL root 账户的密码为 Mysql.123
:
docker run --name mysql \
--restart=always \
-p 3306:3306 \
-v /Users/yygnb/dockerMe/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /Users/yygnb/dockerMe/mysql/data:/var/lib/mysql \
-v /Users/yygnb/dockerMe/mysql/logs:/logs \
--privileged=true \
-e MYSQL_ROOT_PASSWORD=Mysql.123 \
-d mysql:latest
2.5 查看 MySQL 状态
docker ps | grep mysql
2.6 修改 MySQL 权限
容器启动后,可以进入容器修改 MySQL 的一些权限,使得外部可以连接。
进入容器:
docker exec -it mysql /bin/bash
登录 MySQL:
mysql -u root -p
输入上面设置的密码 Mysql.123
后便在命令行中进入了 MySQL。
依次执行下列 SQL 语句,修改权限:
use mysql;
update user set authentication_string='' where user='root';
alter user 'root'@'%' IDENTIFIED BY 'Mysql.123';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
全部执行完毕后,可以尝试使用 MySQL Workbench 或 IDEA 或 Navicat 连接 MySQL。
MySQL 8 的 JDBC 连接串一般格式为:
jdbc:mysql://127.0.0.1:3306/db_name?useUnicode=true&characterEncoding=utf8&useSSL=true
驱动为:
com.mysql.cj.jdbc.Driver
感谢你阅读本文,如果本文给了你一点点帮助或者启发,还请三连支持一下,点赞、关注、收藏,作者会持续与大家分享更多干货