前言docker安裝mysql環(huán)境非常方便,簡單的幾步操作就可以了 拉取mysql鏡像先拉取mysql的鏡像,,可以在docker的鏡像倉庫找到不同TAG標(biāo)簽的版本https://hub./_/mysql?tab=tags 我這里選擇mysql:5.7標(biāo)簽 [root@yoyo ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql fc7181108d40: Already exists 787a24c80112: Already exists a08cb039d3cd: Already exists 4f7d35eb5394: Already exists 5aa21f895d95: Already exists a742e211b7a2: Pull complete 0163805ad937: Pull complete 62d0ebcbfc71: Pull complete 559856d01c93: Pull complete c849d5f46e83: Pull complete f114c210789a: Pull complete Digest: sha256:c3594c6528b31c6222ba426d836600abd45f554d078ef661d3c882604c70ad0a Status: Downloaded newer image for mysql:5.7 docker images查看已經(jīng)下載成功 REPOSITORY TAG IMAGE ID CREATED SIZE centos v7.5 254d4dfe9df7 3 days ago 200MB mysql 5.7 a1aa4f76fab9 2 weeks ago 373MB centos/python-36-centos7 latest b8d15efaa8ec 2 months ago 651MB centos centos7.5.1804 cf49811e3cdb 3 months ago 200MB ubuntu 15.10 9b9cb95443b5 2 years ago 137MB training/webapp latest 6fae60ef3446 4 years ago 349MB [root@yoyo ~]# 配置mysql創(chuàng)建mysql目錄,,用于存放mysql相關(guān)配置及數(shù)據(jù) mkdir : 如果存在上級目錄不存在的情況,會無法創(chuàng)建下級目錄, 會提出ERROR mkdir -p : 遞歸創(chuàng)建目錄,,即使上級目錄不存在,,會按目錄層級自動創(chuàng)建目錄 ~ : ”/“是根目錄,”~“是家目錄。 Linux存儲是以掛載的方式,,相當(dāng)于是樹狀的,,源頭就是”/“,也就是根目錄,。 而每個用戶都有”家“目錄,,也就是用戶的個人目錄,比如root用戶的”家“目錄就是/root,普通用戶a的家目錄就是/home/a
mkdir -p ~/yoyo/mysql/conf ~/yoyo/mysql/data ~/yoyo/mysql/logs
創(chuàng)建完成后,,在當(dāng)前用戶的home目錄(root用戶在root目錄),,會有三個文件夾 [root@yoyo ~]# mkdir -p ~/yoyo/mysql/conf ~/yoyo/mysql/data ~/yoyo/mysql/logs [root@yoyo mysql]# ll ~/yoyo/mysql total 12 drwxr-xr-x 2 root root 4096 Jun 30 10:31 conf drwxr-xr-x 2 root root 4096 Jun 30 10:31 data drwxr-xr-x 2 root root 4096 Jun 30 10:31 logs 運行mysql容器docker run -p 3309:3306 —name yoyomysql -v ~/yoyo/mysql/conf:/etc/mysql/conf.d -v ~/yoyo/mysql/logs:/logs -v ~/yoyo/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
命令說明: —name 重命名yoyomysql -p 3309:3306: 將容器的 3306 端口映射到主機的 3309 端口。 -v -v ~/yoyo/mysql/conf:/etc/mysql/conf.d: 將主機~/yoyo/mysql/conf 掛載到容器的 /etc/mysql/my.cnf,。 -v ~/yoyo/mysql/logs:/logs: 將主機~/yoyo/mysql 目錄掛載到容器的 /logs,。 -v ~/yoyo/mysql/data:/var/lib/mysql : 將主機~/yoyo/mysql/data目錄掛載到容器的 /var/lib/mysql 。 -e MYSQL_ROOT_PASSWORD=123456: 初始化 root 用戶的密碼,。 -d 掛后臺運行
啟動完成后,,查看運行狀態(tài) [root@yoyo mysql]# docker run -p 3308:3306 --name yoyomysql -v ~/yoyo/mysql/conf:/etc/mysql/conf.d -v ~/yoyo/mysql/logs:/logs -v ~/yoyo/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 581137afbd3ba22dc02bf89d3ec1afb6e51d55eed0d99a3a4a979bdee8d5de4a [root@yoyo ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 581137afbd3b mysql:5.7 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 33060/tcp, 0.0.0.0:3309->3306/tcp yoyomysql 查看~/yoyo/mysql/data目錄會看到有數(shù)據(jù) [root@yoyo ~]# ll ~/yoyo/mysql/data/ total 188476 -rw-r----- 1 polkitd ssh_keys 56 Jun 30 10:38 auto.cnf -rw------- 1 polkitd ssh_keys 1679 Jun 30 10:38 ca-key.pem -rw-r--r-- 1 polkitd ssh_keys 1107 Jun 30 10:38 ca.pem -rw-r--r-- 1 polkitd ssh_keys 1107 Jun 30 10:38 client-cert.pem -rw------- 1 polkitd ssh_keys 1675 Jun 30 10:38 client-key.pem -rw-r----- 1 polkitd ssh_keys 1340 Jun 30 10:38 ib_buffer_pool -rw-r----- 1 polkitd ssh_keys 79691776 Jun 30 10:38 ibdata1 -rw-r----- 1 polkitd ssh_keys 50331648 Jun 30 10:38 ib_logfile0 -rw-r----- 1 polkitd ssh_keys 50331648 Jun 30 10:38 ib_logfile1 -rw-r----- 1 polkitd ssh_keys 12582912 Jun 30 10:38 ibtmp1 drwxr-x--- 2 polkitd ssh_keys 4096 Jun 30 10:38 mysql drwxr-x--- 2 polkitd ssh_keys 4096 Jun 30 10:38 performance_schema -rw------- 1 polkitd ssh_keys 1679 Jun 30 10:38 private_key.pem -rw-r--r-- 1 polkitd ssh_keys 451 Jun 30 10:38 public_key.pem -rw-r--r-- 1 polkitd ssh_keys 1107 Jun 30 10:38 server-cert.pem -rw------- 1 polkitd ssh_keys 1675 Jun 30 10:38 server-key.pem drwxr-x--- 2 polkitd ssh_keys 12288 Jun 30 10:38 sys 進入容器bash輸入docker exec -it mysql bash進入容器bash,接著輸入mysql -uroot -p輸入root用戶密碼123456進入mysql [root@yoyo data]# docker exec -i -t yoyomysql bash root@581137afbd3b:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.26 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> 進入mysql之后所有指令結(jié)尾都需要 ; 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 -A
Database changed mysql> select host, user from user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | root | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+---------------+ 4 rows in set (0.00 sec)
mysql> exit Bye root@581137afbd3b:/# exit exit 退出mysql和容器用exit 連接mysql測試
|