靳闯博客 记录是一种习惯,分享是一种态度
  • 波浪
  • 波浪
  • 波浪
  • 波浪
centos7 基于docker搭建mysql
发表于: | 分类: 技术积累 | 标签: Centos7 Docker | 评论:0 | 阅读: 50

使用docker搭建服务方便又快捷,这里使用docker来搭建mysql


查询mysql镜像/下载镜像

[root@localhost-docker ~]#docker search mysql
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   7474                [OK]                
mariadb                                                MariaDB is a community-developed fork of MyS…   2410                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   557                                     [OK]
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       150                                     [OK]
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          102                                     
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   80                                      [OK]
centurylink/mysql                                      Image containing mysql. Optimized to be link…   59                                      [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          47                                      [OK]
centos/mysql-57-centos7                                MySQL 5.7 SQL database server                   43                                      
mysql/mysql-cluster                                    Experimental MySQL Cluster Docker images. Cr…   35                                      
tutum/mysql                                            Base docker image to run a MySQL database se…   31                                      
bitnami/mysql                                          Bitnami MySQL Docker Image                      23                                      [OK]
schickling/mysql-backup-s3                             Backup MySQL to S3 (supports periodic backup…   23                                      [OK]
linuxserver/mysql                                      A Mysql container, brought to you by LinuxSe…   17                                      
zabbix/zabbix-proxy-mysql                              Zabbix proxy with MySQL database support        17                                      [OK]
centos/mysql-56-centos7                                MySQL 5.6 SQL database server                   12                                      
circleci/mysql                                         MySQL is a widely used, open-source relation…   7                                       
mysql/mysql-router                                     MySQL Router provides transparent routing be…   6                                       
openshift/mysql-55-centos7                             DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
dsteinkopf/backup-all-mysql                            backup all DBs in a mysql server                5                                       [OK]
openzipkin/zipkin-mysql                                Mirror of https://quay.io/repository/openzip…   1                                       
jelastic/mysql                                         An image of the MySQL database server mainta…   1                                       
cloudfoundry/cf-mysql-ci                               Image used in CI of cf-mysql-release            0                                       
ansibleplaybookbundle/mysql-apb                        An APB which deploys RHSCL MySQL                0                                       [OK]
cloudposse/mysql                                       Improved `mysql` service with support for `m…   0                                       [OK]

#这里可以选择不同的mysql版本,我们下载最新版的mysql(mysql8.0)
[root@localhost-docker ~]#docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
a5a6f2f73cd8: Pull complete 
936836019e67: Pull complete 
283fa4c95fb4: Pull complete 
1f212fb371f9: Pull complete 
e2ae0d063e89: Pull complete 
5ed0ae805b65: Pull complete 
0283dc49ef4e: Pull complete 
a7e1170b4fdb: Pull complete 
88918a9e4742: Pull complete 
241282fa67c2: Pull complete 
b0fecf619210: Pull complete 
bebf9f901dcc: Pull complete 
Digest: sha256:b7f7479f0a2e7a3f4ce008329572f3497075dc000d8b89bac3134b0fb0288de8
Status: Downloaded newer image for mysql:latest

#查看下载的镜像
[root@localhost-docker ~]#docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              f991c20cb508        2 weeks ago         486MB
hello-world         latest              4ab4c602aa5e        2 months ago        1.84kB

使用mysql镜像创建一个mysql服务容器

[root@localhost-docker ~]#docker run --name mysql8 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
77b8c375d7f50c21756badd1e9ec1e0aae5c62d42c01f096c08fd0ea42017547
#返回一串数字就是容器的id,也代表启动成功

#创建容器的参数含义:
run            运行一个容器
--name         给容器起个名称
-e             设置环境变量,此处是设置root密码
-p 3306:3306   容器中的3306端口映绑定到本机的3306端口
-d             表示使用守护进程运行,即服务挂在后台
最后参数一个为镜像的名称

#可选参数:
-v             主机目录映射容器目录(比如配置文件和数据库目录)
--dns 8.8.8.8  指定容器使用的DNS服务器,默认和宿主一致;
--dns-search example.com 指定容器DNS搜索域名,默认和宿主一致;
-h "mars"      指定容器的hostname;
-e username="ritchie" 设置环境变量;
--env-file=[]  从指定文件读入环境变量;
--cpuset="0-2" or --cpuset="0,1,2"  绑定容器到指定CPU运行;
-m             设置容器使用内存最大值;
--net="bridge" 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
--link=[]      添加链接到另一个容器;
--expose=[]    开放一个端口或一组端口;

#查看容器运行情况
[root@localhost-docker ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                    PORTS                               NAMES
77b8c375d7f5        mysql               "docker-entrypoint.s…"   About a minute ago   Up About a minute         0.0.0.0:3306->3306/tcp, 33060/tcp   mysql8
2b0d38b55f65        hello-world         "/hello"                 17 hours ago         Exited (0) 17 hours ago                                       condescending_bohr

进入mysql容器

[root@localhost-docker ~]#docker exec -it mysql8 /bin/bash
#参数含义:
-d :分离模式: 在后台运行
-i :即使没有附加也保持STDIN 打开
-t :分配一个伪终端

root@77b8c375d7f5:/# 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 9
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> 

授权用户远程连接

#mysql8.0 之前授权语句是不能用的,8.0后要先创建用户,然后再授权。
#mysql8.0 之前的版本默认加密认证规则是mysql_native_password, mysql8.0之后默认加密认证规则是caching_sha2_password 
#注意:默认使用远程工具是连接不上的,在设置用户密码时候需要加上 with mysql_native_password 参数才可以。

#查看user表
mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host      | user             | authentication_string                                                  | plugin                |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| %         | root             | $A$005$whW=Qp{Q jf:c0Of8AWSmczxg3VgzdjLisc2AUeUxQLH0WG66jIuRHFAS. | caching_sha2_password |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session    | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys        | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root             | $A$005$LS z7#CE~`
                                                   6]pHq/0tshTFB1aWQuSuiiQ2w6LFt4Kpi0UTKdRpYBmvLZL/ | caching_sha2_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
5 rows in set (0.00 sec)

#看到有一个root@%的用户了,但是密码加密认证方式是caching_sha2_password,我们需要修改下
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '666666';
Query OK, 0 rows affected (0.04 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

远程连接测试

[root@localhost-docker ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0c:29:b9:c6:90 brd ff:ff:ff:ff:ff:ff
    inet 192.168.16.187/22 brd 192.168.19.255 scope global ens192
       valid_lft forever preferred_lft forever
    inet6 fe80::e6e5:a42b:e32e:85c0/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ab:23:5b:49 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:abff:fe23:5b49/64 scope link 
       valid_lft forever preferred_lft forever
7: veth8f5b2bd@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether aa:9f:6a:b7:71:4c brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::a89f:6aff:feb7:714c/64 scope link 
       valid_lft forever preferred_lft forever

[root@localhost-docker ~]#netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1229/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1789/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      9194/docker-proxy   
tcp6       0      0 :::22                   :::*                    LISTEN      1229/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1789/master 

#使用另一台机器连接测试(如果没有更改密码的加密认证方式会报这样的错误)
[root@localhost ~]# mysql -uroot -p -h192.168.16.187
Enter password:
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/local/mysql/lib/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory


#改完加密认证方式后再次连接,连接成功
[root@localhost emucoo-service]# mysql -uroot -p666666 -h192.168.16.187
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 14
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2017, 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> 

商业转载请联系作者获得授权,非商业转载请注明出处,谢谢合作。


如果这篇文章帮助到了你,我感到十分荣幸!

或许你不想写点什么·但我依旧在这里

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png2018new_aini_org.png2018new_baobao_thumb.png2018new_erha_org.png2018new_kuxiao_thumb.png2018new_yun_thumb.png2018new_hufen_thumb.png2018new_gui_org.png2018new_xiaoerbuyu_org.png2018new_heixian_thumb.png2018new_wabi_thumb.png2018new_tianping_thumb.png

站点地图 网站地图
豫ICP备17003270号 | Copyright © 2018 💖 靳闯博客
Typecho🍹Sgreen
TOP