【原文链接】MySQL----基于Docker部署MySQL
(1)环境首先需要已经安装Docker,若未安装可参考 CentOS系统安装Docker Engine 安装
(2) 搜索mysql镜像
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11731 [OK]
mariadb MariaDB Server is a high performing open sou… 4472 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 875 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 381 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 89
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 53
prom/mysqld-exporter 43 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 34
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 31 [OK]
mysql/mysql-router MySQL Router provides transparent routing be… 23
centos/mysql-56-centos7 MySQL 5.6 SQL database server 21
arey/mysql-client Run a MySQL client from a docker container 19 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 16 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 8 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 3 [OK]
centos/mysql-80-centos7 MySQL 8.0 SQL database server 2
jelastic/mysql An image of the MySQL database server mainta… 2
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]#
(3)下载MySQL的镜像
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a10c77af2613: Pull complete
b76a7eb51ffd: Pull complete
258223f927e4: Pull complete
2d2c75386df9: Pull complete
63e92e4046c9: Pull complete
f5845c731544: Pull complete
bd0401123a9b: Pull complete
2724b2da64fd: Pull complete
d10a7e9e325c: Pull complete
1c5fd9c3683d: Pull complete
2e35f83a12e9: Pull complete
Digest: sha256:7a3a7b7a29e6fbff433c339fc52245435fa2c308586481f2f92ab1df239d6a29
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]#
(4)创建数据库数据文件本地存储目录
mkdir -p /docker/mysql/var/lib/mysql
(5)启动docker容器
docker run -d -p 3306:3306 --privileged=true -v /docker/mysql/var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=xxxxxxxxx --name mysql mysql:5.7 --character-set-server=utf8 --collation-server=utf8_general_ci
参数说明:
(6)进入docker验证mysql数据库
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4afca70d4674 mysql:5.7 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
373b8eaed36f jenkins/jenkins:lts "/sbin/tini -- /usr/…" 8 days ago Up 3 days 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp, 50000/tcp, 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp myjenkins
1906e95db7ab nginx "/docker-entrypoint.…" 7 weeks ago Up 7 days 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker exec -it 4afca70d4674 bash
root@4afca70d4674:/# 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.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> exit
Bye
root@4afca70d4674:/# exit
exit
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]#
(7) 设置root用户可以远程链接mysql数据库
进入docker,登录mysql数据库后,执行如下命令即可(xxxxxx为数据库密码)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxxxxxx';
flush privileges;
如
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4afca70d4674 mysql:5.7 "docker-entrypoint.s…" 17 minutes ago Up 17 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
373b8eaed36f jenkins/jenkins:lts "/sbin/tini -- /usr/…" 8 days ago Up 3 days 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp, 50000/tcp, 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp myjenkins
1906e95db7ab nginx "/docker-entrypoint.…" 7 weeks ago Up 7 days 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
[root@iZbp1flzt6x7pxmxfhmxeeZ jdk1.8.0_301]# docker exec -it 4afca70d4674 bash
root@4afca70d4674:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxxxxxx';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
此时即可使用root用户远程连接了
(7) 如不想对外暴露root用户远程连接,可以使用如下命令创建一个普通用户对外暴露
如下,暴露一个用户名为mysql的用户
GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY 'xxxxxxx' WITH GRANT OPTION;
flush privileges;
如:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY 'xxxxxxx' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
此时即可使用mysql用户远程连接了
(8) 此时数据库编码并未支持中文,如果向此数据库写入中文数据,则会显示乱码,为解决这个问题,需要在mysql视图下执行如下命令:
SET NAMES 'utf8';