千家信息网

怎么安装和使用mysql的docker

发表于:2024-09-28 作者:千家信息网编辑
千家信息网最后更新 2024年09月28日,这篇文章主要介绍"怎么安装和使用mysql的docker",在日常操作中,相信很多人在怎么安装和使用mysql的docker问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答
千家信息网最后更新 2024年09月28日怎么安装和使用mysql的docker

这篇文章主要介绍"怎么安装和使用mysql的docker",在日常操作中,相信很多人在怎么安装和使用mysql的docker问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"怎么安装和使用mysql的docker"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

(1)https://hub.docker.com/ 从库中搜索mysql库,选择mysql:5.7

$ docker pull mysql:5.7

意思就是:从docker拉取mysql镜像,版本是5.7。其实我们可以具体点这样写:

$ docker pull docker.io/library/mysql:5.7 ,是不是很像下载文件的写法。

[root@k8s-master nginx]# df -k

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/sda2 102877120 2973376 94654772 4% /

[root@k8s-master nginx]# df -k

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/sda2 102877120 3355004 94273144 4% /

这个时候,我就会想,下载的镜像放在哪里了呢?这是个好问题。我们用find来找下,发现在这里

放着:ls /var/lib/docker/overlay2/

搜索会发现这个文件夹:

/var/lib/docker/overlay2/91f7d56179ab4bfb0a795401b590d17d30dae97d70b02f24454d1b5b4339798b/diff/run/mysqld。

(2)看下系统目前有哪些images

[root@k8s-master nginx]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

centos 7 67fa590cfc1c 2 days ago 202MB

mysql 5.7 e1e1680ac726 9 days ago 373MB

(3)开始启动这个镜像,用起来呗。

Starting a MySQL instance is simple:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

... where some-mysql is the name you want to assign to your container, my-secret-pw is the password to be

set for the MySQL root user and tag is the tag specifying the MySQL version you want. See the list above for

relevant tags.

$ mkdir /data/datadir

$ docker run --expose=3306 -p 3306 --name docker_mysql -v /data/datadir:/var/lib/mysql \

-e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 \

--collation-server=utf8mb4_unicode_ci

-p 3306 :开发端口

--name docker_mysql:container name 是docker_mysql

-v /data/datadir:/var/lib/mysql :主机目录/data/datadir容器目录/var/lib/mysql

-e MYSQL_ROOT_PASSWORD=123456 :root密码

-d: 后台启动

--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci :传递给mysql的参数

(4)查询下现在都启动了哪些container呢?

[root@k8s-master nginx]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

6a9ad568a182 mysql:5.7 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 33060/tcp, 0.0.0.0:32768->3306/tcp docker_mysql

其实这个时候我们想,以docker启动和普通mysql启动有什么不同呢?先用ps看下

# ps aux|grep mysql

systemd+ 5438 0.2 9.7 1136232 194828 ? Ssl 15:41 0:00 mysqld

目前是用systemd用户启动的。

(5)这个时候我们查看一下/data/datadir,发现什么了?

# ls /data/datadir

auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys

ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem

(6)在看下主机的监听端口有哪些?

[root@k8s-master nginx]# netstat -tunlp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 746/sshd

tcp6 0 0 :::32768 :::* LISTEN 8054/docker-proxy

32768就是暴露外面访问端口的地址。

(7)这个时候,我们就像连接mysql数据库,然后创建database,创建表

mysql -uroot -p123456 -h292.168.2.129 -P32768

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.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> create database test;

Query OK, 1 row affected (0.00 sec)

mysql> use test

Database changed

mysql> create table stu (id number ,name varchar(30));

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 'number ,name varchar(30))' at line 1

mysql> create table stu (id integer ,name varchar(30));

Query OK, 0 rows affected (0.01 sec)

mysql>

mysql> insert into stu values(1,'xie');

Query OK, 1 row affected (0.01 sec)

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

观察下,发现:mysql版本是5.7.27,用的是Community。

我创建了一个表stu.我去主机看下目录,果然创建了一个文件test和表stu.frm.

[root@k8s-master nginx]# ls /data/datadir/test

db.opt stu.frm stu.ibd

(8)这个时候我们是不是很有成就感了,哈哈。现在我想进去容器里面看看,到底有多少连接进来,怎么做呢?

docker exec -it docker_mysql bash

-i, --interactive Keep STDIN open even if not attached

-t, --tty Allocate a pseudo-TTY

[root@k8s-master nginx]# docker exec -it docker_mysql bash

root@6a9ad568a182:/# 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 3

Server version: 5.7.27 MySQL Community Server (GPL)

mysql> show processlist;

+----+------+---------------------+------+---------+------+----------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+------+---------------------+------+---------+------+----------+------------------+

| 3 | root | localhost | NULL | Query | 0 | starting | show processlist |

| 4 | root | 192.168.2.132:50408 | NULL | Sleep | 4 | | NULL |

+----+------+---------------------+------+---------+------+----------+------------------+

2 rows in set (0.00 sec)

在这里,就像在一个操作系统里面一样,像做什么就做什么。自己体验一下吧。

(9)启动和关闭,再启动容器,再关闭容器,删除容器,再创建一个容器,然后连接到mysql,你发现了什么?

[root@k8s-master nginx]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

6a9ad568a182 mysql:5.7 "docker-entrypoint.s…" 24 minutes ago Up 24 minutes 33060/tcp, 0.0.0.0:32768->3306/tcp docker_mysql

[root@k8s-master nginx]# docker stop docker_mysql

docker_mysql

[root@k8s-master nginx]# docker start docker_mysql

docker_mysql

[root@k8s-master nginx]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

6a9ad568a182 mysql:5.7 "docker-entrypoint.s…" 24 minutes ago Up 12 seconds 33060/tcp, 0.0.0.0:32769->3306/tcp docker_mysql

# docker rm 6a9ad568a182

# docker run --expose=3306 -p 3306 --name docker_mysql -v /data/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

我再次连接到mysql,发现我刚刚创建的database test和表stu还在,还有一条数据。感觉container,类似一个普通的mysql 进程。像普通的mysql进程一样,

可以启动,关闭,删除,载入数据文件。

到此,关于"怎么安装和使用mysql的docker"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0