07.docker安装MYSQL

/ Docker / 没有评论 / 900浏览

docker search

安装Msql

命令

docker run -d -p 3307:3306 --privileged=true -v /Users/huzd/Downloads/mysql/log:/var/log/mysql -v /Users/huzd/Downloads/mysql/data:/var/lib/mysql -v /Users/huzd/Downloads/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=sitechdss --name iMySQL mysql:5.7

实操

安装MYSQL5.7

➜  ~ docker run -d -p 3307:3306 --privileged=true -v /Users/huzd/Download/mysql/log:/var/log/mysql -v /Users/huzd/Download/mysql/data:/var/lib/mysql -v /Users/huzd/Download/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=sitechdss --name iMySQL mysql:5.7
Unable to find image 'mysql:5.7' locally
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
b9bd3f348913deeb06d31bb16a9d6ae0acc7bd15fedef69d4669b09e817eaddd
➜  ~ docker ps   
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                               NAMES
b9bd3f348913   mysql:5.7   "docker-entrypoint.s…"   27 seconds ago   Up 24 seconds   33060/tcp, 0.0.0.0:3307->3306/tcp   iMySQL
➜  ~                         

配置MSQL5.7

➜  ~ cd /Users/huzd/Download             # 1 进入绑定的容器卷目录                                
➜  Download ls
mysql
➜  Download cd mysql               
➜  mysql ls
conf data log
➜  mysql cd conf 
➜  conf ls
➜  conf vim my.cnf             # 2 新建一个my.cnf文件 并制定字符集配置信息
➜  conf more my.cnf                 
[client]
default_character_set=utf8
[mysqld]
collation_server=utf8_general_ci
character_set_server=utf8
➜  conf docker stop b9bd3f348913     # 3 停止mysql 容器           
b9bd3f348913
➜  conf docker start b9bd3f348913           # 4 启动mysql容器 
b9bd3f348913
➜  conf docker exec -it b9bd3f348913 /bin/bash # 5 命令行连接容器
root@b9bd3f348913:/# mysql -uroot -p # 6 登录容器
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
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;  # 7 查询数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

mysql> show variables like 'character%' # 8 查询字符集
    -> ;
+--------------------------+----------------------------+
| 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.01 sec)

mysql> 

删除容器

➜  conf docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                     PORTS     NAMES
b9bd3f348913   mysql:5.7      "docker-entrypoint.s…"   16 minutes ago   Exited (0) 6 seconds ago             iMySQL
11b88904b686   registry       "/entrypoint.sh /etc…"   4 days ago       Exited (2) 4 days ago                naughty_roentgen
58fd941f173c   753e19c60999   "/bin/bash"              4 days ago       Exited (127) 4 days ago              frosty_wilson
8f83b1f2e780   753e19c60999   "/bin/bash"              4 days ago       Exited (0) 4 days ago                frosty_lehmann
eba4a23b97c9   ba6acccedd29   "/bin/bash"              4 days ago       Exited (0) 4 days ago                charming_leakey
3c1f7895a3ca   b292881e118d   "/bin/bash"              5 days ago       Exited (0) 5 days ago                condescending_solomon
57cc53e3eafc   nginx          "/docker-entrypoint.…"   5 days ago       Exited (0) 5 days ago                mynginx
42dff86a059c   ubuntu         "/bin/bash"              5 days ago       Exited (0) 5 days ago                agitated_wilbur
7caa76b4c6b0   ubuntu         "/bin/bash"              5 days ago       Exited (0) 5 days ago                upbeat_brahmagupta
cbb8f18af774   ubuntu         "tail -f /dev/null"      5 days ago       Exited (137) 5 days ago              beautiful_cohen
20facd76b57b   ubuntu         "/bin/bash"              5 weeks ago      Exited (0) 3 weeks ago               geoserver
➜  conf docker rm -f b9bd3f348913            
b9bd3f348913

复用容器卷启动新容器

data docker run -d -p 3307:3306 --privileged=true -v /Users/huzd/Downloads/mysql/log:/var/log/mysql -v /Users/huzd/Downloads/mysql/data:/var/lib/mysql -v /Users/huzd/Downloads/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=sitechdss --name iMySQL mysql:5.7
e0975a3d178208bf0079b4d0d6f960c5ee0da6ddca59f09913e63e8c9e1b60ce

➜  data docker exec -it e09 /bin/bash
root@e0975a3d1782:/# mysql -uroot -p
Enter password: 
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                |
| test01             |
+--------------------+
5 rows in set (0.04 sec)

mysql> use test01;
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> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| user             |
+------------------+
1 row in set (0.01 sec)

mysql> select * fr user;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fr user' at line 1
mysql> select * from user;
+------+------------+
| id   | name       |
+------+------------+
|    1 | hello      |
|    2 | 好的啊     |
|    3 | 你好啊     |
+------+------------+
3 rows in set (0.00 sec)

mysql>