前言
docker安装mysql环境非常方便,简单的几步操作就可以了
拉取mysql镜像
先拉取mysql的镜像,可以在docker的镜像仓库找到不同TAG标签的版本https://hub.docker.com/_/mysql?tab=tags
我这里选择mysql:5.7标签
鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决,我使用的是网易的镜像地址:http://hub-mirror.c.163.com。
新版的 Docker 使用 /etc/docker/daemon.json(Linux) 或者 %programdata%dockerconfigdaemon.json(Windows) 来配置 Daemon。
请在该配置文件中加入(没有该文件的话,请先建一个):
{ "registry-mirrors": ["http://hub-mirror.c.163.com"] }
[root@bogon ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
9fc222b64b0a: Pull complete
291e388076f0: Pull complete
d6634415290b: Pull complete
1f1e7d852ad4: Pull complete
125fc05f36e0: Pull complete
2aed16e5b02f: Pull complete
5fa9342b7235: Pull complete
a1e72cc32505: Pull complete
81225f74ecbe: Pull complete
b9a45d065520: Pull complete
a3e7b2fe9950: Pull complete
Digest: sha256:1a121f2e7590f949b9ede7809395f209dd9910e331e8372e6682ba4bebcc020b
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@bogon ~]#
配置mysql
创建mysql目录,用于存放mysql相关配置及数据
- mkdir : 如果存在上级目录不存在的情况,会无法创建下级目录, 会提出ERROR
- mkdir -p :递归创建目录,即使上级目录不存在,会按目录层级自动创建目录
- ~ : ”/“是根目录,”~“是家目录。Linux存储是以挂载的方式,相当于是树状的,源头就是”/“,也就是根目录。
而每个用户都有”家“目录,也就是用户的个人目录,比如root用户的”家“目录就是/root,普通用户a的家目录就是/home/a
[root@bogon home]# mkdir -p ./leslie/mysql/conf ./leslie/mysql/data ./leslie/mysql/logs
[root@bogon mysql]# ll
总用量 0
drwxr-xr-x 2 root root 6 9月 3 14:47 conf
drwxr-xr-x 2 root root 6 9月 3 14:47 data
drwxr-xr-x 2 root root 6 9月 3 14:47 logs
[root@bogon mysql]#
运行mysql容器
docker run -p 3309:3306 --name lesliemysql -v /home/leslie/mysql/conf:/etc/mysql/conf.d -v /home/leslie/mysql/logs:/logs -v /home/leslie/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
命令说明:
- --name 重命名yoyomysql
- -p 3309:3306:将容器的 3306 端口映射到主机的 3309 端口。
- -v -v ~/leslie/mysql/conf:/etc/mysql/conf.d:将主机~/leslie/mysql/conf 挂载到容器的 /etc/mysql/my.cnf。
- -v ~/leslie/mysql/logs:/logs:将主机~/yoyo/mysql 目录挂载到容器的 /logs。
- -v ~/leslie/mysql/data:/var/lib/mysql :将主机~/leslie/mysql/data目录挂载到容器的 /var/lib/mysql 。
- -e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
- -d 挂后台运行
启动完成后,查看运行状态
[root@bogon mysql]# docker run -p 3309:3306 --name lesliemysql -v /home/leslie/mysql/conf:/etc/mysql/conf.d -v /home/leslie/mysql/logs:/logs -v /home/leslie/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
4252c716b85de9771917ad5bf546d4e1194ee6cc1e407a31d6a1363340a33863
[root@bogon mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4252c716b85d mysql:5.7 "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 33060/tcp, 0.0.0.0:3309->3306/tcp lesliemysql
[root@bogon mysql]#
查看/home/lelsie/mysql/data目录会看到有数据
[root@bogon mysql]# ls
conf data logs
[root@bogon mysql]# cd data/
[root@bogon data]# ls
auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem
ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem sys
ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem
[root@bogon data]# ll
总用量 188484
-rw-r----- 1 systemd-bus-proxy input 56 9月 3 14:57 auto.cnf
-rw------- 1 systemd-bus-proxy input 1675 9月 3 14:57 ca-key.pem
-rw-r--r-- 1 systemd-bus-proxy input 1107 9月 3 14:57 ca.pem
-rw-r--r-- 1 systemd-bus-proxy input 1107 9月 3 14:57 client-cert.pem
-rw------- 1 systemd-bus-proxy input 1679 9月 3 14:57 client-key.pem
-rw-r----- 1 systemd-bus-proxy input 1346 9月 3 14:57 ib_buffer_pool
-rw-r----- 1 systemd-bus-proxy input 79691776 9月 3 14:57 ibdata1
-rw-r----- 1 systemd-bus-proxy input 50331648 9月 3 14:57 ib_logfile0
-rw-r----- 1 systemd-bus-proxy input 50331648 9月 3 14:57 ib_logfile1
-rw-r----- 1 systemd-bus-proxy input 12582912 9月 3 14:57 ibtmp1
drwxr-x--- 2 systemd-bus-proxy input 4096 9月 3 14:57 mysql
drwxr-x--- 2 systemd-bus-proxy input 8192 9月 3 14:57 performance_schema
-rw------- 1 systemd-bus-proxy input 1679 9月 3 14:57 private_key.pem
-rw-r--r-- 1 systemd-bus-proxy input 451 9月 3 14:57 public_key.pem
-rw-r--r-- 1 systemd-bus-proxy input 1107 9月 3 14:57 server-cert.pem
-rw------- 1 systemd-bus-proxy input 1679 9月 3 14:57 server-key.pem
drwxr-x--- 2 systemd-bus-proxy input 8192 9月 3 14:57 sys
[root@bogon data]#
进入容器bash
输入docker exec -it mysql bash进入容器bash,接着输入mysql -uroot -p输入root用户密码123456进入mysql
[root@bogon data]# docker exec -i -t lesliemysql bash
root@4252c716b85d:/# 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.27 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)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 -ADatabase changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
退出mysql和容器用exit