免安装,还原生产环境,运行中切换版本,这不是我认识的MySQL

数据库 MySQL
MySQL,用了好多年了吧,在你印象里是不是一直都是四平八稳,做为一个基础组件,也不期待啥了。

 MySQL,用了好多年了吧,在你印象里是不是一直都是四平八稳,做为一个基础组件,也不期待啥了。

如果说想线下调度,集成测试,想用一个内存数据库,你可能会说那H2, Derby吧,不都可以嘛。

但差别是你在自己线下时跑了多少不说,但不同的数据库,不同的特性,可能有些地方无法真正还原线上。为什么不安装一个?费事,哈哈。

今天咱们介绍的这位,可以理解为嵌入MySQL,免安装。不同的测试时还可以切换不同的版本,Cool。

使用起来也不费劲,加个 Maven 依赖就行,分分钟的事儿。

就是它:

  1. <dependency> 
  2.        <groupId>com.wix</groupId> 
  3.        <artifactId>wix-embedded-mysql</artifactId> 
  4.        <version>x.y.z</version> 
  5.        <scope>test</scope> 
  6. </dependency> 

代码也简单,直接定义你需要的版本,数据库信息,把要初始化的SQL 给它,走起。

  1. MysqldConfig config = aMysqldConfig(v5_6_23) //这里是版本 
  2.   .withCharset(UTF8) 
  3.   .withPort(2215) 
  4.   .withUser("user1""pwd2"
  5.   .withTimeZone("Europe/Vilnius"
  6.   .withTimeout(2, TimeUnit.MINUTES) 
  7.   .withServerVariable("max_connect_errors", 666) 
  8.   .build(); 
  9.  
  10. EmbeddedMysql mysqld = anEmbeddedMysql(config) 
  11.   .addSchema("aschema", ScriptResolver.classPathScript("db/001_init.sql")) 
  12.   .start(); 
  13.  
  14. //do work 
  15.  
  16. mysqld.stop(); //optional, as there is a shutdown hook 

这有啥优势:

  • 测试可以跑在和生产环境基本一致的环境,同样的版本,同样的编码和配置,database/schema/user settings 等等
  • 比安装一个更容易,想切换版本,改配置也更轻松;
  • 本地每个项目可以使用不同的版本,不同的配置,啥都不用担心;
  • 对于MySQL的多个版本支持 - 5.5, 5.6, 5.7, 8.0;
  • 多种平台和环境都支持。

原理

这背后是怎么实现的呢?

咱们是「刨根究底」公众号,一起来看看。

上面代码配置之后的 start ,到底 start 了啥?

咱们看下面这几小段代码:

  1. protected EmbeddedMysql( 
  2.             final MysqldConfig mysqldConfig, 
  3.             final DownloadConfig downloadConfig) { 
  4.         this.config = mysqldConfig; 
  5.         IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaults(mysqldConfig, downloadConfig).build(); 
  6.         MysqldStarter mysqldStarter = new MysqldStarter(runtimeConfig); 
  7.         localRepository.lock(); 
  8.         try { 
  9.             this.executable = mysqldStarter.prepare(mysqldConfig); 
  10.         } finally { 
  11.             localRepository.unlock(); 
  12.         } 
  13.  
  14.         try { 
  15.             executable.start(); 
  16.             getClient(SCHEMA, mysqldConfig.getCharset()).executeCommands( 
  17.                     format("CREATE USER '%s'@'%%' IDENTIFIED BY '%s';", mysqldConfig.getUsername(), mysqldConfig.getPassword())); 
  18.         } catch (IOException e) { 
  19.             throw new RuntimeException(e); 
  20.         } 
  21.     } 
  1. protected MysqldProcess start( 
  2.             final Distribution distribution, 
  3.             final MysqldConfig config, 
  4.             final IRuntimeConfig runtime) throws IOException { 
  5.         logger.info("Preparing mysqld for startup"); 
  6.         Setup.apply(config, executable, runtime); 
  7.         logger.info("Starting MysqldProcess"); 
  8.         return new MysqldProcess(distribution, config, runtime, this); 
  9.     } 

其实这背后依赖了一个叫embed.process的开源项目,

  1. public AbstractProcess(Distribution distribution, T config, IRuntimeConfig runtimeConfig, E executable) 
  2.       throws IOException { 
  3.     this.config = config; 
  4.     this.runtimeConfig = runtimeConfig; 
  5.     this.executable = executable; 
  6.     this.distribution = distribution; 
  7.     // pid file needs to be set before ProcessBuilder is called 
  8.     this.pidFile = pidFile(this.executable.getFile().executable()); 
  9.  
  10.     ProcessOutput outputConfig = runtimeConfig.getProcessOutput(); 
  11.  
  12.     // Refactor me - to much things done in this try/catch 
  13.     String nextCall=""
  14.     try { 
  15.  
  16.       nextCall="onBeforeProcess()"
  17.  
  18.       onBeforeProcess(runtimeConfig); 
  19.  
  20.       nextCall="newProcessBuilder()"
  21.  
  22.       ProcessBuilder processBuilder = ProcessControl.newProcessBuilder( 
  23.           runtimeConfig.getCommandLinePostProcessor().process(distribution, 
  24.               getCommandLine(distribution, config, this.executable.getFile())), 
  25.           getEnvironment(distribution, config, this.executable.getFile()), true); 
  26.  
  27.  
  28.       nextCall="onBeforeProcessStart()"
  29.  
  30.       onBeforeProcessStart(processBuilder, config, runtimeConfig); 
  31.  
  32.       nextCall="start()"
  33.  
  34.       process = ProcessControl.start(config.supportConfig(), processBuilder); 
  35.  
  36.       nextCall="writePidFile()"
  37.  
  38.       if (process.getPid() != null) { 
  39.         writePidFile(pidFile, process.getPid()); 
  40.       } 
  41.  
  42.       nextCall="addShutdownHook()"
  43.  
  44.       if (runtimeConfig.isDaemonProcess() && !executable.isRegisteredJobKiller()) { 
  45.         ProcessControl.addShutdownHook(new JobKiller()); 
  46.         registeredJobKiller = true
  47.       } 
  48.  
  49.       nextCall="onAfterProcessStart()"
  50.       onAfterProcessStart(process, runtimeConfig); 
  51.     } catch (IOException iox) { 
  52.       stop(); 
  53.       throw iox; 
  54.     } 
  55.   } 

它又操作了什么呢?从名字你也猜到了,它是直接操作进程的,实际在运行时,会下载一个MySQL,然后通过脚本启停。

 

初次启动的时候,会直接下载

 

有了这些,在测试的时候就可以和生产环境一样,启动时加载初始化SQL脚本,开始你的工作了。

github地址:https://github.com/wix/wix-embedded-mysql

本文转载自微信公众号「Tomcat那些事儿」,可以通过以下二维码关注。转载本文请联系Tomcat那些事儿公众号。

 

责任编辑:武晓燕 来源: Tomcat那些事儿
相关推荐

2010-04-01 10:44:14

MySQL

2015-02-09 10:00:38

谷歌卫星互联网

2016-04-29 19:53:15

2019-03-21 04:47:20

口令网络安全数据泄露

2016-12-07 07:17:11

云计算科技新闻早报

2019-04-23 10:30:23

机器学习人工智能计算机

2023-03-03 13:30:18

设计模式编程语言

2016-05-24 17:24:45

云计算

2016-08-23 18:25:47

Linux系统开源

2017-06-13 16:12:49

大型机云计算

2018-08-27 10:24:03

UbuntuPHP版本

2018-08-23 09:56:03

Linux程序版本

2021-05-23 12:05:15

3DAI 人工智能

2017-01-15 10:19:21

2021-06-24 20:30:38

办公

2018-12-04 14:15:29

容器误区开发

2020-02-25 15:47:05

ElasticsearLucene地方

2020-10-19 09:50:04

Spinnaker

2010-05-28 12:50:04

MySQL 免安装版

2010-11-11 14:29:27

Ubuntu工作区
点赞
收藏

51CTO技术栈公众号