Docker_安装mysql
程序员文章站
2022-05-18 09:46:14
...
首先在远程仓库拉取mysql
[aaa@qq.com docker]# docker pull mysql:5.6
5.6: Pulling from library/mysql
d599a449871e: Pull complete
f287049d3170: Pull complete
08947732a1b0: Pull complete
96f3056887f2: Pull complete
871f7f65f017: Pull complete
111ea1dd4e23: Pull complete
2dcf2d87da45: Pull complete
648aa2667757: Pull complete
418a18378dc0: Pull complete
02a64522fded: Pull complete
577b15a8d700: Pull complete
Digest: sha256:5345afaaf1712e60bbc4d9ef32cc62acf41e4160584142f8d73115f16ad94af4
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6
运行mysql
运行命令:docker run -p 3306:3306 --name mysql -v /dockerMysql/conf:/etc/mysql/conf.d -v /dockerMysql/logs:/logs -v /dockerMysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6
命令详解
docker run
-p 3306:3306 #mysql端口
--name mysql #容器别名
-v /dockerMysql/conf:/etc/mysql/conf.d #mysql配置
-v /dockerMysql/logs:/logs #mysql日志
-v /dockerMysql/data:/var/lib/mysql #mysql数据
-e MYSQL_ROOT_PASSWORD=root #root密码
-d mysql:5.6 #后台运行mysql
在windows上使用Navicat链接docker的mysql
操作数据库
最后测试对docker里面的mysql进行数据备份
测试命令:docker exec 0dc8641688af /bin/sh -c 'mysqldump -uroot -proot test' > /dockerMysql/test.sql
[root@localhost dockerMysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0dc8641688af mysql:5.6 "docker-entrypoint.s…" 26 minutes ago Up 26 minutes 0.0.0.0:3306->3306/tcp mysql
[root@localhost dockerMysql]# docker exec 0dc8641688af /bin/sh -c 'mysqldump -uroot -proot test' > /dockerMysql/test.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost dockerMysql]# cat test.sql
-- MySQL dump 10.13 Distrib 5.6.46, for Linux (x86_64)
--
-- Host: localhost Database: test
-- ------------------------------------------------------
-- Server version 5.6.46
/*!40101 SET @aaa@qq.com@CHARACTER_SET_CLIENT */;
/*!40101 SET @aaa@qq.com@CHARACTER_SET_RESULTS */;
/*!40101 SET @aaa@qq.com@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @aaa@qq.com@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @aaa@qq.com@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @aaa@qq.com@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @aaa@qq.com@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @aaa@qq.com@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `user`
--
DROP TABLE IF EXISTS `user`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `user`
--
LOCK TABLES `user` WRITE;
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
INSERT INTO `user` VALUES (1,'zhangsan'),(2,'李四'),(3,'wangwu');
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET aaa@qq.com_TIME_ZONE */;
/*!40101 SET aaa@qq.com_SQL_MODE */;
/*!40014 SET aaa@qq.com_FOREIGN_KEY_CHECKS */;
/*!40014 SET aaa@qq.com_UNIQUE_CHECKS */;
/*!40101 SET aaa@qq.com_CHARACTER_SET_CLIENT */;
/*!40101 SET aaa@qq.com_CHARACTER_SET_RESULTS */;
/*!40101 SET aaa@qq.com_COLLATION_CONNECTION */;
/*!40111 SET aaa@qq.com_SQL_NOTES */;
-- Dump completed on 2019-11-26 8:15:18
[root@localhost dockerMysql]#