测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
MySQL----基于Docker部署MySQL
收藏本文
作者:redrose2100 类别: 日期:2023-02-22 01:45:55 阅读:841 次 消耗积分:0 分
![](https://redrose2100.oss-cn-hangzhou.aliyuncs.com/img/c30cfca4-b251-11ed-a0e0-0242ac110002.png) (1)环境首先需要已经安装Docker,若未安装可参考 [CentOS系统安装Docker Engine](http://devops-dev.com/article/380) 安装 (2) 搜索mysql镜像 ```bash [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的镜像 ```bash [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)创建数据库数据文件本地存储目录 ```bash mkdir -p /docker/mysql/var/lib/mysql ``` (5)启动docker容器 ```bash docker run -d -p 3306:3306 --privileged=true -v /docker/mysql/var/lib/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=xxxxxxxxx --name mysql --restart=always mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci ``` 参数说明: * -d 表示后台运行 * -p 表示容器内部端口和服务器端口映射关联 * --privileged=true 设值MySQL 的root用户权限, 否则外部不能使用root用户登陆 * -v /docker/mysql/var/lib/mysql:/var/lib/mysql 映射数据库的数据目录, 避免以后docker删除重新运行MySQL容器时数据丢失 * -e MYSQL_ROOT_PASSWORD=xxxxxxx 设置MySQL数据库root用户的密码 * --name mysql 设值容器名称为mysql * mysql:5.7 表示从docker镜像mysql:5.7中启动一个容器 * --character-set-server=utf8 设置编码格式 * --collation-server=utf8_general_ci 设值数据库默认编码 (6)进入docker验证mysql数据库 ```bash [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为数据库密码) ```bash GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxxxxxxx'; flush privileges; ``` 如 ```bash [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的用户 ```bash GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY 'xxxxxxx' WITH GRANT OPTION; flush privileges; ``` 如: ```bash 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视图下执行如下命令: ```bash SET NAMES 'utf8'; ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/536
上一篇:
SikuliX----SikuliX生成的脚本使用Python调用执行
下一篇:
Appium----基于Windows系统安装Android模拟器(Android Virtual Device)
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件