自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

免安裝,還原生產(chǎn)環(huán)境,運(yùn)行中切換版本,這不是我認(rèn)識(shí)的MySQL

數(shù)據(jù)庫(kù) MySQL
MySQL,用了好多年了吧,在你印象里是不是一直都是四平八穩(wěn),做為一個(gè)基礎(chǔ)組件,也不期待啥了。

 MySQL,用了好多年了吧,在你印象里是不是一直都是四平八穩(wěn),做為一個(gè)基礎(chǔ)組件,也不期待啥了。

如果說(shuō)想線下調(diào)度,集成測(cè)試,想用一個(gè)內(nèi)存數(shù)據(jù)庫(kù),你可能會(huì)說(shuō)那H2, Derby吧,不都可以嘛。

但差別是你在自己線下時(shí)跑了多少不說(shuō),但不同的數(shù)據(jù)庫(kù),不同的特性,可能有些地方無(wú)法真正還原線上。為什么不安裝一個(gè)?費(fèi)事,哈哈。

今天咱們介紹的這位,可以理解為嵌入MySQL,免安裝。不同的測(cè)試時(shí)還可以切換不同的版本,Cool。

使用起來(lái)也不費(fèi)勁,加個(gè) 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> 

代碼也簡(jiǎn)單,直接定義你需要的版本,數(shù)據(jù)庫(kù)信息,把要初始化的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 

這有啥優(yōu)勢(shì):

  • 測(cè)試可以跑在和生產(chǎn)環(huán)境基本一致的環(huán)境,同樣的版本,同樣的編碼和配置,database/schema/user settings 等等
  • 比安裝一個(gè)更容易,想切換版本,改配置也更輕松;
  • 本地每個(gè)項(xiàng)目可以使用不同的版本,不同的配置,啥都不用擔(dān)心;
  • 對(duì)于MySQL的多個(gè)版本支持 - 5.5, 5.6, 5.7, 8.0;
  • 多種平臺(tái)和環(huán)境都支持。

原理

這背后是怎么實(shí)現(xiàn)的呢?

咱們是「刨根究底」公眾號(hào),一起來(lái)看看。

上面代碼配置之后的 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.     } 

其實(shí)這背后依賴了一個(gè)叫embed.process的開(kāi)源項(xiàng)目,

  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.   } 

它又操作了什么呢?從名字你也猜到了,它是直接操作進(jìn)程的,實(shí)際在運(yùn)行時(shí),會(huì)下載一個(gè)MySQL,然后通過(guò)腳本啟停。

 

初次啟動(dòng)的時(shí)候,會(huì)直接下載

 

有了這些,在測(cè)試的時(shí)候就可以和生產(chǎn)環(huán)境一樣,啟動(dòng)時(shí)加載初始化SQL腳本,開(kāi)始你的工作了。

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

本文轉(zhuǎn)載自微信公眾號(hào)「Tomcat那些事兒」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系Tomcat那些事兒公眾號(hào)。

 

責(zé)任編輯:武曉燕 來(lái)源: Tomcat那些事兒
相關(guān)推薦

2010-04-01 10:44:14

MySQL

2016-04-29 19:53:15

2016-12-07 07:17:11

云計(jì)算科技新聞早報(bào)

2015-02-09 10:00:38

谷歌衛(wèi)星互聯(lián)網(wǎng)

2023-03-03 13:30:18

設(shè)計(jì)模式編程語(yǔ)言

2019-04-23 10:30:23

機(jī)器學(xué)習(xí)人工智能計(jì)算機(jī)

2019-03-21 04:47:20

口令網(wǎng)絡(luò)安全數(shù)據(jù)泄露

2016-08-23 18:25:47

Linux系統(tǒng)開(kāi)源

2017-06-13 16:12:49

大型機(jī)云計(jì)算

2021-05-23 12:05:15

3DAI 人工智能

2018-08-27 10:24:03

UbuntuPHP版本

2016-05-24 17:24:45

云計(jì)算

2018-08-23 09:56:03

Linux程序版本

2017-01-15 10:19:21

2024-10-09 18:39:30

AI諾貝爾物理獎(jiǎng)

2021-06-24 20:30:38

辦公

2018-12-04 14:15:29

容器誤區(qū)開(kāi)發(fā)

2025-04-30 00:20:00

OpenAIChatGPTAI聊天

2024-09-11 14:02:13

硅谷模式管理者

2020-02-25 15:47:05

ElasticsearLucene地方
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)