当前位置:   article > 正文

docker安装mysql5.7(仅供测试使用)_docker mysql 5.7 2002

docker mysql 5.7 2002

前言

环境:Centos7.9 mysql5.7 Docker version 20.10.9
mysql数据库建议使用物理机安装,这里使用docker安装mysql仅供测试使用或其他不重要场景使用。

docker 安装mysql5.7

#创建mysql的文件存放目录
mkdir -p  /usr/local/mysql/conf
mkdir -p /usr/local/mysql/logs
mkdir -p /usr/local/mysql/mysql
cd  /usr/local/mysql/conf
#创建mysql配置文件
cat > my.cnf <<'EOF'
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
default-time_zone = '+8:00'
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBS
TITUTION
lower_case_table_names=1
EOF

#启动mysql5.7容器,并使用-v参数持久化存储mysql的数据
docker run --restart always \
-p 3306:3306 \
--name mysql5.7 \
-v /usr/local/mysql/conf/my.cnf:/etc/my.cnf \
-v /usr/local/mysql/logs:/var/log/mysql \
-v /usr/local/mysql/mysql:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

#查看mysql5.7容器,已经启动了
[root@soft mysql]# docker ps | grep  mysql5.7
1019a1f67f2d   mysql:5.7  "docker-entrypoint.s…"   30 minutes ago   Up 30 minutes   0.0.0.0:3306->3306/tcp, :::3306-
>3306/tcp, 33060/tcp   mysql5.7
[root@soft mysql]#

#进入容器查看mysql容器是否可用
[root@soft ~]# docker exec -it mysql5.7 bash				#进入容器
root@1019a1f67f2d:/# mysql -uroot -p123456					#登录mysql
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.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> show databases;							#执行命令,一切正常,说明MySQL可用
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

备注:因为我们在启动mysql5.7容器的时候做了主机端口映射,所以外部应用程序直接链接主机ip:3306即可使用容器的mysql数据库。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号