docker基础——mysql安装

运行本地仓库

[root@localhost ~]# docker run -d -p 5000:5000 -v /jl/myregistry/:/tmp/registry --privileged=true registry         
f5bbe411a1ed3441bf1fa02babd913e34bf948330af17ab7a6f9c45a0cf4a13a
[root@localhost ~]# docker ps               
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
f5bbe411a1ed   registry   "/entrypoint.sh /etc…"   4 seconds ago   Up 3 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   gallant_driscoll
[root@localhost ~]# 

简单版

[root@localhost ~]# docker search mysql:5.7
NAME                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
kiratalent/mysql                          Pinned copy of mysql:5.7.24                     1                    
migs/mysql-5.7                            mysql:5.7 plus some basic enhancements          0                    [OK]
xiao806852034/mysql57                     基于 mysql:5.7                                  0                    
idyci/mysql                               mysql:5.7.25                                    1                    
framgiaciteam/mysql-256                   FROM mysql:5.7  CMD ["--block_encryption_mod…   0                    
metatronx/mysql                           mysql:5.7 + druid account & database            0                    
haswind/mysql                             mysql:5.7                                       0                    
stsiwo/sts-blog-db                        base image mysql:5.7.28                         0                    
llasuka/mysql                             mysql:5.7                                       0                    
michaelzx/docker_msyql                    without ONLY_FULL_GROUP_BY base on mysql:5.7…   0                    [OK]
alexmbarbosa/mysql5.7                     Based on official mysql:5.7 docker image        0                    
alecchyi/mysql                            mysql:5.7 for demo                              0                    
denghui/mysql                             mysql:5.7-tz.8                                  0                    [OK]
crbanman/mysql-tmpfs                      A docker image based off of mysql:5.7 that m…   0                    
vigasin/mysql                             Clone of official mysql:5.7 image, that allo…   0                    
variu/hyperledger-explorer-mysql          mysql:5.7.23 for hyperledger explorer           0                    
doctorva/mysql_fede                       Based on Mysql:5.7 and add new configuration…   0                    
ahmedelshfie/spring-boot-mysql-rest-api   mysql:5.7                                       0                    
zhjs/mysql                                mysql:5.7                                       0                    
beyondray/kbengine-mysql                  kbengine auto-build env from mysql:5.7          0                    [OK]
liyupei/mysql                             Mysql:5.7                                       0                    
kamatimaru/mysql57-ja                     Added Japanese support settings to mysql:5.7…   0                    
zeq9069/mysql_service                     mysql:5.7                                       0                    
ruyli/mysql                               mysql:5.7                                       0                    
dockerzm/mysql                            docker run --name mysql -e MYSQL_ROOT_PASSWO…   0                    
[root@localhost ~]# docker pull mysql:5.7  
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost ~]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED         SIZE
192.168.253.128:5000/jlubuntu   1.2       09f66e5eb0c6   4 days ago      115MB
jlubuntu                        1.2       09f66e5eb0c6   4 days ago      115MB
jlubuntu                        1.2.1     09f66e5eb0c6   4 days ago      115MB
mysql                           5.7       c20987f18b13   15 months ago   448MB
registry                        latest    b8604a3fe854   17 months ago   26.2MB
ubuntu                          latest    ba6acccedd29   18 months ago   72.8MB
hello-world                     latest    feb5d9fea6a5   18 months ago   13.3kB
redis                           6.0.8     16ecd2772934   2 years ago     104MB
billygoo/tomcat8-jdk8           latest    30ef4019761d   4 years ago     523MB
[root@localhost ~]# docker run -p 3306:3306 -e MYSQL ROOT PASSWORD=123456 -d mysql:5.7
docker: invalid reference format: repository name must be lowercase.
See 'docker run --help'.
[root@localhost ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7  
5df5690d650fd6d61440a20d8136b07a6cbea313400e90fc98bd951fa23e41d4
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
5df5690d650f   mysql:5.7   "docker-entrypoint.s…"   8 seconds ago    Up 7 seconds    0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   amazing_napier
f5bbe411a1ed   registry    "/entrypoint.sh /etc…"   29 minutes ago   Up 29 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              gallant_driscoll
[root@localhost ~]# docker exec -it 5df5690d650f bash
root@5df5690d650f:/# 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 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;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.05 sec)

mysql> exit
Bye
root@5df5690d650f:/#

在这里插入图片描述
navicate连接数据库插入中文失败

在这里插入图片描述

问题:

docker上默认字符编码隐患
删除容器后数据无法恢复

完善版

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED             STATUS             PORTS                                                  NAMES
5df5690d650f   mysql:5.7   "docker-entrypoint.s…"   48 minutes ago      Up 48 minutes      0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   amazing_napier
f5bbe411a1ed   registry    "/entrypoint.sh /etc…"   About an hour ago   Up About an hour   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              gallant_driscoll
[root@localhost ~]# docker rm -f 5df5690d650f
5df5690d650f
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED             STATUS             PORTS                                       NAMES
f5bbe411a1ed   registry   "/entrypoint.sh /etc…"   About an hour ago   Up About an hour   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   gallant_driscoll
//通过容器卷实现数据同步
[root@localhost ~]# docker run -d -p 3306:3306 --privileged=true -v /jl/mysql/log:/var/log/mysql -v /jl/mysql/data:/var/lib/mysql -v /jl/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7                           
cd096afd198eac83a4bed333af9337d3eb0a59d3d94cab6d14176d0cfb64f411
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED             STATUS             PORTS                                                  NAMES
cd096afd198e   mysql:5.7   "docker-entrypoint.s…"   7 seconds ago       Up 5 seconds       0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
f5bbe411a1ed   registry    "/entrypoint.sh /etc…"   About an hour ago   Up About an hour   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              gallant_driscoll
[root@localhost ~]# ^C
[root@localhost ~]# cd /jl/mysql/conf/
新建my.cnf文件
[root@localhost conf]# vi  my.cnf
[root@localhost conf]# cat my.cnf 
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
[root@localhost conf]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED       STATUS       PORTS                                                  NAMES
cd096afd198e   mysql:5.7   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
f5bbe411a1ed   registry    "/entrypoint.sh /etc…"   4 hours ago   Up 4 hours   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              gallant_driscoll
[root@localhost conf]# docker restart mysql
mysql
[root@localhost conf]# docker exec -it mysql bash
root@cd096afd198e:/# mysqi -uroot -p123456
bash: mysqi: command not found
root@cd096afd198e:/# 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 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 VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.25 sec)

mysql>

navicat插入中文还是报错:
在这里插入图片描述

alter table userinfo change username username varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT ‘json’ NOT NULL;
在这里插入图片描述

[root@localhost conf]# docker exec -it mysql bash
root@cd096afd198e:/# mysqi -uroot -p123456
bash: mysqi: command not found
root@cd096afd198e:/# 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 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 VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.25 sec)

mysql> ^C
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from userinfo;
+----+----------+-------------+
| id | username | tel         |
+----+----------+-------------+
|  1 | jl2      | 17854289303 |
|  2 | ll       | 17854289303 |
|  3 | 记录     | 17854289303 |
+----+----------+-------------+
3 rows in set (0.00 sec)

mysql>

测试容器卷数据同步

[root@localhost conf]# docker rm -f mysql
mysql
[root@localhost conf]# docker run -d -p 3306:3306 --privileged=true -v /jl/mysql/log:/var/log/mysql -v /jl/mysql/data:/var/lib/mysql -v /jl/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7       
7e477af1061c3f6a4287383bb79fc771357dc4faf747b915854796836ac49571
[root@localhost conf]# docker ps  
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
7e477af1061c   mysql:5.7   "docker-entrypoint.s…"   5 seconds ago   Up 4 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
f5bbe411a1ed   registry    "/entrypoint.sh /etc…"   4 hours ago     Up 4 hours     0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              gallant_driscoll
[root@localhost conf]# docker exec -it mysql bash
root@7e477af1061c:/# 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 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from userinfo;
+----+----------+-------------+
| id | username | tel         |
+----+----------+-------------+
|  1 | jl2      | 17854289303 |
|  2 | ll       | 17854289303 |
|  3 | 记录     | 17854289303 |
+----+----------+-------------+
3 rows in set (0.00 sec)

mysql>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值