docker拉取mysql5.6镜像,并run一个容器,使用mysql服务
在宿主机:192.168.224.130上安装docker,再下载mysql5.6镜像后运行mysql5.6容器(和宿主机端口映射和目录映射),通过宿主机访问容器mysql数据
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
[root@localhost ~]# docker search mysql:5.6
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 7939 [OK]
mariadb MariaDB is a community-developed fork of MyS… 2653 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 598
…………
[root@localhost ~]# docker pull mysql:5.6
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql 5.6 b3983abaa3fe 10 days ago 302 MB
1)先随便运行一个mysql5.6容器测试一下,供分析一下里面的配置文件、数据目录、日志目录等位置
[root@localhost ~]# docker run -p 3306:3306 --name mysql -v /tmp:/tmp -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
136c6ff4b13b mysql:5.6 "docker-entrypoint..." 5 seconds ago Up 1 second 0.0.0.0:3306->3306/tcp mysql
[root@localhost ~]# docker exec -it mysql /bin/bash
root@136c6ff4b13b:/# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
mysql> create database ku;
mysql> use ku;
mysql> create table biao (name varchar(10),age int(2));
mysql> insert into biao values ("shi",28);
mysql> select * from biao;
+------+------+
| name | age |
+------+------+
| shi | 28 |
+------+------+
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
mysql> quit
root@136c6ff4b13b:/# exit
[root@localhost ~]# docker exec -it mysql ls / #查看容器里/下目录
bin docker-entrypoint-initdb.d home media proc sbin tmp
boot entrypoint.sh lib mnt root srv usr
dev etc lib64 opt run sys var
[root@localhost ~]# docker exec -it mysql ls /var/lib/mysql #查看容器里数据目录
auto.cnf ib_logfile0 ib_logfile1 ibdata1 ku mysql performance_schema
[root@localhost ~]# docker exec -it mysql ls /etc/mysql #查看容器里配置文件目录
conf.d my.cnf my.cnf.fallback mysql.cnf mysql.conf.d
[root@localhost ~]# docker exec -it mysql cat /etc/mysql/my.cnf |grep -v "#"
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[root@localhost ~]# docker exec -it mysql ls /etc/mysql/conf.d
docker.cnf mysql.cnf mysqldump.cnf
[root@localhost ~]# docker exec -it mysql ls /etc/mysql/mysql.conf.d
mysqld.cnf
[root@localhost ~]# docker exec -it mysql cat /etc/mysql/mysql.conf.d/mysqld.cnf |grep -v "#"
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
symbolic-links=0
[root@localhost ~]# docker exec -it mysql cat /etc/mysql/conf.d/mysql.cnf
[mysql]
分析结果:
a)容器中主配置文件位置:/etc/mysql/my.cnf,里面啥都没有,只是include了一个目录路径,供存放配置文件和修改配置文件使用。
b)容器里引用的配置文件目录位置:/etc/mysql/conf.d/和/etc/mysql/mysql.conf.d/
c)容器里服务端mysqld.conf文件位置:/etc/mysql/mysql.conf.d/mysqld.cnf
d)容器里客户端mysql.conf文件位置:/etc/mysql/conf.d/mysql.cnf
e)容器里的目录位置:默认没有,可以映射一个/logs,供以后查看日志使用。
f)容器里的数据文件位置:/var/lib/mysql
2)分析出镜像文件的各个需要位置后,删除测试的容器,重新运行一个各个目录和宿主机映射的容器
[root@localhost ~]# docker rm -f mysql
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql 5.6 b3983abaa3fe 10 days ago 302 MB
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker run -p 3306:3306 --name mysql \
> -v /media/mysql/logs:/logs \ #容器里没有/logs路径,映射日志目录,可以修改容器配置文件将日志位置指向这里
> -v /media/mysql/data:/var/lib/mysql \ #容器里有/var/lib/mysql路径
> -e MYSQL_ROOT_PASSWORD=123456 \
> -d mysql:5.6 #回车即可,-d是后台运行,mysql:5.6是镜像
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9881e17ca37d mysql:5.6 "docker-entrypoint..." 6 seconds ago Up 5 seconds 0.0.0.0:3306->3306/tcp mysql
[root@localhost ~]# docker exec -it mysql ls /
bin docker-entrypoint-initdb.d home logs opt run sys var #映射目录创建容器后,容器里的日志路径/logs
boot entrypoint.sh lib media proc sbin tmp
dev etc lib64 mnt root srv usr
[root@localhost ~]# docker exec -it mysql ls /var/lib/mysql #映射配置文件创建容器后,容器里数据文件目录不丢
auto.cnf ib_logfile0 ib_logfile1 ibdata1 mysql performance_schema
#配置文件没做映射,所以还都是保持分析容器时的情况
[root@localhost ~]# docker exec -it mysql ls /etc/mysql
conf.d my.cnf my.cnf.fallback mysql.cnf mysql.conf.d
[root@localhost ~]# docker exec -it mysql ls /etc/mysql/conf.d
docker.cnf mysql.cnf mysqldump.cnf
[root@localhost ~]# docker exec -it mysql ls /etc/mysql/mysql.conf.d
mysqld.cnf
[root@localhost ~]# docker exec -it mysql /bin/bash
root@9881e17ca37d:/# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
mysql> create database ku;
mysql> use ku;
mysql> create table biao (name varchar(10),age int(2));
mysql> insert into biao values ("shi2",30);
mysql> select * from biao;
+------+------+
| name | age |
+------+------+
| shi2 | 30 |
+------+------+
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
mysql> quit
root@9881e17ca37d:/# exit
3)将容器内服务端配置文件mysqld.cnf拷贝出来,进行修改成自定义的配置文件,默认存储引擎修改成:myisam
[root@localhost ~]# docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf mysqld.cnf
[root@localhost ~]# vim mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /logs/error.log #修改成和宿主机映射目录,方便查看日志
default-storage-engine=myisam #添加,修改默认存储引擎为myisam
symbolic-links=0
4)将自定义修改的mysqld.cnf拷贝覆盖到容器中
[root@localhost ~]# docker cp mysqld.cnf mysql:/etc/mysql/mysql.conf.d/mysqld.cnf
[root@localhost ~]# docker exec -it mysql cat /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /logs/error.log
default-storage-engine=myisam
symbolic-links=0
5)重启容器:
[root@localhost ~]# docker restart mysql
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9881e17ca37d mysql:5.6 "docker-entrypoint..." 6 minutes ago Up 10 seconds 0.0.0.0:3306->3306/tcp mysql
6)登录容器查看默认存储引擎已经修改,表示自定义的配置文件生效
[root@localhost ~]# docker exec -it mysql /bin/bash
root@9881e17ca37d:/# mysql -uroot -p123456
mysql> use ku;
mysql> select * from biao;
+------+------+
| name | age |
+------+------+
| shi2 | 30 |
+------+------+
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | DEFAULT | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | YES | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
mysql> quit
root@9881e17ca37d:/# exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9881e17ca37d mysql:5.6 "docker-entrypoint..." 8 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp mysqll
客户端:用navicat访问宿主机ip和映射端口3306,从而访问容器mysql数据,如下:
注意: docker stop mysql 停止后,数据库无法访问,再docker start mysql后,mysql服务自动就起来了,可直接访问
docker拉取mysql5.6镜像,并run一个容器,使用mysql服务——最全参数(非常重要)
在宿主机:192.168.224.130上安装docker,再下载mysql5.6镜像后运行mysql5.6容器(和宿主机端口映射和目录映射),通过宿主机访问容器mysql数据
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
[root@localhost ~]# docker search mysql:5.6
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 7939 [OK]
mariadb MariaDB is a community-developed fork of MyS… 2653 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 598
…………
[root@localhost ~]# docker pull mysql:5.6
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql 5.6 b3983abaa3fe 10 days ago 302 MB
1)先随便运行一个mysql5.6容器测试一下,供分析一下里面的配置文件、数据目录、日志目录等位置
[root@localhost ~]# docker run -p 3306:3306 --name mysql -v /tmp:/tmp -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
136c6ff4b13b mysql:5.6 "docker-entrypoint..." 5 seconds ago Up 1 second 0.0.0.0:3306->3306/tcp mysql
[root@localhost ~]# docker exec -it mysql /bin/bash
root@136c6ff4b13b:/# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
mysql> create database ku;
mysql> use ku;
mysql> create table biao (name varchar(10),age int(2));
mysql> insert into biao values ("shi",28);
mysql> select * from biao;
+------+------+
| name | age |
+------+------+
| shi | 28 |
+------+------+
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
mysql> quit
root@136c6ff4b13b:/# exit
[root@localhost ~]# docker exec -it mysql ls / #查看容器里/下目录
bin docker-entrypoint-initdb.d home media proc sbin tmp
boot entrypoint.sh lib mnt root srv usr
dev etc lib64 opt run sys var
[root@localhost ~]# docker exec -it mysql ls /var/lib/mysql #查看容器里数据目录
auto.cnf ib_logfile0 ib_logfile1 ibdata1 ku mysql performance_schema
[root@localhost ~]# docker exec -it mysql ls /etc/mysql #查看容器里配置文件目录
conf.d my.cnf my.cnf.fallback mysql.cnf mysql.conf.d
[root@localhost ~]# docker exec -it mysql cat /etc/mysql/my.cnf |grep -v "#"
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[root@localhost ~]# docker exec -it mysql ls /etc/mysql/conf.d
docker.cnf mysql.cnf mysqldump.cnf
[root@localhost ~]# docker exec -it mysql ls /etc/mysql/mysql.conf.d
mysqld.cnf
[root@localhost ~]# docker exec -it mysql cat /etc/mysql/mysql.conf.d/mysqld.cnf |grep -v "#"
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
symbolic-links=0
[root@localhost ~]# docker exec -it mysql cat /etc/mysql/conf.d/mysql.cnf
[mysql]
分析结果:
a)容器中主配置文件位置:/etc/mysql/my.cnf,里面啥都没有,只是include了一个目录路径,供存放配置文件和修改配置文件使用。
b)容器里引用的配置文件目录位置:/etc/mysql/conf.d/和/etc/mysql/mysql.conf.d/
c)容器里服务端mysqld.conf文件位置:/etc/mysql/mysql.conf.d/mysqld.cnf
d)容器里客户端mysql.conf文件位置:/etc/mysql/conf.d/mysql.cnf
e)容器里的目录位置:默认没有,可以映射一个/logs,供以后查看日志使用。
f)容器里的数据文件位置:/var/lib/mysql
2)分析出镜像文件的各个需要位置后,将容器内服务端配置文件mysqld.cnf拷贝出来,进行修改成自定义的配置文件,默认存储引擎修改成:myisam
[root@localhost ~]# docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf mysqld.cnf
[root@localhost ~]# vim mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /logs/error.log #修改成和宿主机映射目录,方便查看日志
default-storage-engine=myisam #添加,修改默认存储引擎为myisam
symbolic-links=0
3)删除测试的容器,重新运行一个各个目录和宿主机映射的容器
[root@localhost ~]# docker rm -f mysql
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql 5.6 b3983abaa3fe 10 days ago 302 MB
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# mkdir /media/mysql
[root@localhost ~]# mkdir /media/mysql/conf
[root@localhost ~]# cp mysqld.cnf /media/mysql/conf/
[root@localhost ~]# vim /media/mysql/conf/mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /logs/error.log #修改成和宿主机映射目录,方便查看日志
default-storage-engine=myisam #添加,修改默认存储引擎为myisam
symbolic-links=0
[root@localhost ~]# docker run -p 3306:3306 --name mysql \
> -v /media/mysql/logs:/logs \ #容器里没有/logs路径,映射日志目录,可以修改容器配置文件将日志位置指向这里
> -v /media/mysql/data:/var/lib/mysql \ #容器里有/var/lib/mysql路径
> -v /media/mysql/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \ #容器里有/etc/mysql/mysql.conf.d/mysqld.cnf路径
> -e MYSQL_ROOT_PASSWORD=123456 \
> -d mysql:5.6 #回车即可,-d是后台运行,mysql:5.6是镜像
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f60d8316b3d mysql:5.6 "docker-entrypoint..." 5 seconds ago Up 5 seconds 0.0.0.0:3306->3306/tcp mysql
4)登录容器查看默认存储引擎已经修改,表示自定义的配置文件生效
[root@localhost ~]# docker exec -it mysql /bin/bash
root@0f60d8316b3d:/# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | DEFAULT | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | YES | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
mysql> create database ku;
mysql> use ku;
mysql> create table biao (name varchar(10),age int(2));
mysql> insert into biao values ("shi2",30);
mysql> select * from biao;
+------+------+
| name | age |
+------+------+
| shi2 | 30 |
+------+------+
mysql> quit
root@0f60d8316b3d:/# exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f60d8316b3d mysql:5.6 "docker-entrypoint..." 5 minutes ago Up 5 minutes 0.0.0.0:3306->3306/tcp mysql
客户端:用navicat访问宿主机ip和映射端口3306,从而访问容器mysql数据,如下:
注意: docker stop mysql 停止后,数据库无法访问,再docker start mysql后,mysql服务自动就起来了,可直接访问
如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多运维实战技术视频!