【docker容器入门(六)】: Docker 安装mysql
本文主要说一下,如何在docker环境中安装和使用mysql。
1. 创建mysql容器
使用如下命令创建mysql容器
docker run -d -e MYSQL_ROOT_PASSWORD=root123 -p 3306:3306 --name mysql --restart always -v /home/mysql/data:/var/lib/mysql mysql
参数说明:
-e 这里用来设置mysql数据库root用户密码
-p 映射容器端口3306到宿主机端口3306
-v 映射容器数据存储路径到宿主机路径/home/mysql/data
# 创建mysql容器 [root@guoxiaobo ~]# docker run -d -e MYSQL_ROOT_PASSWORD=root123 -p 3306:3306 --name mysql --restart always -v /home/mysql/data:/var/lib/mysql mysql Unable to find image 'mysql:latest' locally latest: Pulling from library/mysql Digest: sha256:78800e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873 Status: Downloaded newer image for mysql:latest e05eb579c3cd4e4a6fcda2f32700e1d0c14aa93643eb3bced6a08814039c3294 # 确认mysql容器 [root@guoxiaobo ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e05eb579c3cd mysql "docker-entrypoint.s…" 19 seconds ago Up 17 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
2. 连接mysql
# 进入mysql容器 [root@guoxiaobo ~]# docker exec -it mysql bash # 连接mysql root@e05eb579c3cd:/# mysql -hlocalhost -uroot -p"root123" 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 8 Server version: 8.0.22 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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. # 查看database mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
添加数据
# 创建database mysql> create database gxbdb; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | gxbdb | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.01 sec) mysql> use gxbdb; Database changed # 添加表 mysql> create table user(id int primary key, name text, age text); Query OK, 0 rows affected (0.00 sec) mysql> show tables; +-----------------+ | Tables_in_gxbdb | +-----------------+ | user | +-----------------+ 1 row in set (0.00 sec) # 添加数据 mysql> insert into user values(1, 'dds', 18); Query OK, 1 row affected (0.02 sec) mysql> select * from user; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dds | 18 | +----+------+------+ 1 row in set (0.00 sec) # 断开mysql mysql> exit Bye root@e05eb579c3cd:/# exit exit
3. 数据库备份
备份数据口
[root@guoxiaobo ~]# docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /home/mysql/all-databases.sql mysqldump: [Warning] Using a password on the command line interface can be insecure. [root@guoxiaobo ~]# ll /home/mysql/all-databases.sql -rw-r--r-- 1 root root 3807310 Dec 24 10:17 /home/mysql/all-databases.sql
删除数据
# 删除gxbdb ... mysql> drop database gxbdb; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
4. 数据库恢复
[root@guoxiaobo ~]# docker exec -i mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /home/mysql/all-databases.sql mysql: [Warning] Using a password on the command line interface can be insecure.
确认数据恢复结果
... mysql> show databases; +--------------------+ | Database | +--------------------+ | gxbdb | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use gxbdb; 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_gxbdb | +-----------------+ | user | +-----------------+ 1 row in set (0.01 sec) mysql> select * from user; +----+------+------+ | id | name | age | +----+------+------+ | 1 | dds | 18 | +----+------+------+ 1 row in set (0.00 sec)
欢迎各位光临郭小波的博客,请在此留下您的脚印。

浙公网安备 33010602011771号