docker部署mysql 实现远程连接

 

http://fwhyy.com/2018/04/Install-MySql-with-Docker-in-CentOS7/

https://blog.csdn.net/weixin_42459563/article/details/80924634

 

1.docker search mysql   查看mysql版本

2.docker pull mysql  选择STARS最高的那个name 进行下载

3.docker images  查看下载好的镜像

4.启动mysql实例

   docker run --name dockermysql  -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

    --name 为mysql的实例设置别名。 -p 3306为对外暴露的端口。3306是内部端口 

    -e MYSQL_ROOT_PASSWORD 设置mysql登录密码  -d 以守护进程运行(后台运行) 最后的mysql是镜像名称

 

5. docker ps -a 查看在运行的

 

6. docker exec -it dockermysql bash     进入容器内部  dockermysql 是上边运行时为容器取的别名 也可以用id替代

 

7.docker mysql -u root -p      然后直接输入密码即可 密码是在运行时设置的

 

use mysql

8.grant all privileges on *.*  to 'root'@'%' ;   给用于授予权限

    GRANT ALL PRIVILEGES ON *.*  ‘root’@’%’ identified by ‘123123’ WITH GRANT OPTION;  这是网上流传较多的写法。实际上会报错的。

9.flush privileges;  刷新权限

 

10.登录

    

11.Mysql远程连接报错:authentication plugin caching_sha2

mysql 8.0 默认使用 caching_sha2_password 身份验证机制 —— 从原来的 mysql_native_password 更改为 caching_sha2_password。 
从 5.7 升级 8.0 版本的不会改变现有用户的身份验证方法,但新用户会默认使用新的 caching_sha2_password 。

客户端不支持新的加密方式。

方法之一,修改用户的密码和加密方式

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';

要同赋予用户权限时相同。 若是localhost就同上。%也是如此

mysql8.*的新特性 caching_sha2_password 密码加密方式

以前版本的mysql密码加密使用的是 mysql_native_password
新添加的用户密码默认使用的 caching_sha2_password
如果在以前mysql基础上升级的 就得用户使用的密码加密使用的是 mysql_native_password
如果使用以前的密码加密方式,就修改文件 /etc/my.cnf

 

数据库时区问题:

链接数据库时serverTimezone=UTC这个参数出的问题

只要改成serverTimezone=Asia/Shanghai就OK了!

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
[root@VM_0_5_centos ~]# docker search mysql
INDEX       NAME                                                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                                                  MySQL is a widely used, open-source relati...   8037      [OK]
docker.io   docker.io/mariadb                                                MariaDB is a community-developed fork of M...   2709      [OK]
docker.io   docker.io/mysql/mysql-server                                     Optimized MySQL Server Docker images. Crea...   602                  [OK]
docker.io   docker.io/zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       183                  [OK]
docker.io   docker.io/hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          112
docker.io   docker.io/zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server ...   95                   [OK]
docker.io   docker.io/centurylink/mysql                                      Image containing mysql. Optimized to be li...   60                   [OK]
docker.io   docker.io/centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   51
docker.io   docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          50                   [OK]
docker.io   docker.io/mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. ...   44
docker.io   docker.io/tutum/mysql                                            Base docker image to run a MySQL database ...   31
docker.io   docker.io/bitnami/mysql                                          Bitnami MySQL Docker Image                      26                   [OK]
docker.io   docker.io/schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic back...   26                   [OK]
docker.io   docker.io/zabbix/zabbix-web-apache-mysql                         Zabbix frontend based on Apache web-server...   26                   [OK]
docker.io   docker.io/linuxserver/mysql                                      A Mysql container, brought to you by Linux...   20
docker.io   docker.io/zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        20                   [OK]
docker.io   docker.io/centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   13
docker.io   docker.io/circleci/mysql                                         MySQL is a widely used, open-source relati...   12
docker.io   docker.io/mysql/mysql-router                                     MySQL Router provides transparent routing ...   10
docker.io   docker.io/dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                6                    [OK]
docker.io   docker.io/openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 ima...   6
docker.io   docker.io/jelastic/mysql                                         An image of the MySQL database server main...   1
docker.io   docker.io/ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                    [OK]
docker.io   docker.io/cloudposse/mysql                                       Improved `mysql` service with support for ...   0                    [OK]
docker.io   docker.io/widdpim/mysql-client                                   Dockerized MySQL Client (5.7) including Cu...   0                    [OK]
[root@VM_0_5_centos ~]# docker pull docker.io/mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
27833a3ba0a5: Already exists
864c283b3c4b: Pull complete
cea281b2278b: Pull complete
8f856c14f5af: Pull complete
9c4f38c23b6f: Pull complete
1b810e1751b3: Pull complete
5479aaef3d30: Pull complete
ded8fa2e1614: Pull complete
636033ba4d2e: Pull complete
902e6010661d: Pull complete
dbe44d2bf055: Pull complete
e906385f419d: Pull complete
Digest: sha256:a7cf659a764732a27963429a87eccc8457e6d4af0ee9d5140a3b56e74986eed7
Status: Downloaded newer image for docker.io/mysql:latest
[root@VM_0_5_centos ~]# systemctl stop mysqld
[root@VM_0_5_centos ~]# docker run --name dockermysql  -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
9cbea1ca2c92b5612a8ccfdcb72a5a40dc9ae2563623170819d623907ebcba0e
[root@VM_0_5_centos ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
9cbea1ca2c92        mysql               "docker-entrypoint..."   9 seconds ago       Up 8 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   dockermysql
[root@VM_0_5_centos ~]# docker exec -it dockermysql bash
root@9cbea1ca2c92:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.15 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> grant all privileges on *.*  to 'root'@'%' ;
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
 
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.02 sec)
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| todo               |
+--------------------+
5 rows in set (0.00 sec)

  

 

posted on   youhui  阅读(842)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示