您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么安裝和使用mysql的docker”,在日常操作中,相信很多人在怎么安裝和使用mysql的docker問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么安裝和使用mysql的docker”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
(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來找下,發(fā)現(xiàn)在這里
放著:ls /var/lib/docker/overlay2/
搜索會發(fā)現(xiàn)這個文件夾:
/var/lib/docker/overlay2/91f7d56179ab4bfb0a795401b590d17d30dae97d70b02f24454d1b5b4339798b/diff/run/mysqld。
(2)看下系統(tǒng)目前有哪些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 :開發(fā)端口
--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的參數(shù)
(4)查詢下現(xiàn)在都啟動了哪些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,發(fā)現(xiàn)什么了?
# 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)在看下主機的監(jiān)聽端口有哪些?
[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數(shù)據(jù)庫,然后創(chuàng)建database,創(chuàng)建表
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)
觀察下,發(fā)現(xiàn):mysql版本是5.7.27,用的是Community。
我創(chuàng)建了一個表stu.我去主機看下目錄,果然創(chuàng)建了一個文件test和表stu.frm.
[root@k8s-master nginx]# ls /data/datadir/test
db.opt stu.frm stu.ibd
(8)這個時候我們是不是很有成就感了,哈哈。現(xiàn)在我想進(jìn)去容器里面看看,到底有多少連接進(jìn)來,怎么做呢?
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)
在這里,就像在一個操作系統(tǒng)里面一樣,像做什么就做什么。自己體驗一下吧。
(9)啟動和關(guān)閉,再啟動容器,再關(guān)閉容器,刪除容器,再創(chuàng)建一個容器,然后連接到mysql,你發(fā)現(xiàn)了什么?
[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,發(fā)現(xiàn)我剛剛創(chuàng)建的database test和表stu還在,還有一條數(shù)據(jù)。感覺container,類似一個普通的mysql 進(jìn)程。像普通的mysql進(jìn)程一樣,
可以啟動,關(guān)閉,刪除,載入數(shù)據(jù)文件。
到此,關(guān)于“怎么安裝和使用mysql的docker”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。