溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

免安裝原生產(chǎn)環(huán)境的MySQL是什么

發(fā)布時(shí)間:2021-10-22 16:33:07 來(lái)源:億速云 閱讀:153 作者:iii 欄目:數(shù)據(jù)庫(kù)

這篇文章主要介紹“免安裝原生產(chǎn)環(huán)境的MySQL是什么”,在日常操作中,相信很多人在免安裝原生產(chǎn)環(huán)境的MySQL是什么問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”免安裝原生產(chǎn)環(huán)境的MySQL是什么”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

就是它:

<dependency>        <groupId>com.wix</groupId>        <artifactId>wix-embedded-mysql</artifactId>        <version>x.y.z</version>        <scope>test</scope> </dependency>

代碼也簡(jiǎn)單,直接定義你需要的版本,數(shù)據(jù)庫(kù)信息,把要初始化的SQL 給它,走起。

MysqldConfig config = aMysqldConfig(v5_6_23) //這里是版本   .withCharset(UTF8)   .withPort(2215)   .withUser("user1", "pwd2")   .withTimeZone("Europe/Vilnius")   .withTimeout(2, TimeUnit.MINUTES)   .withServerVariable("max_connect_errors", 666)   .build();  EmbeddedMysql mysqld = anEmbeddedMysql(config)   .addSchema("aschema", ScriptResolver.classPathScript("db/001_init.sql"))   .start();  //do work  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 了啥?

咱們看下面這幾小段代碼:

protected EmbeddedMysql(             final MysqldConfig mysqldConfig,             final DownloadConfig downloadConfig) {         this.config = mysqldConfig;         IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaults(mysqldConfig, downloadConfig).build();         MysqldStarter mysqldStarter = new MysqldStarter(runtimeConfig);         localRepository.lock();         try {             this.executable = mysqldStarter.prepare(mysqldConfig);         } finally {             localRepository.unlock();         }          try {             executable.start();             getClient(SCHEMA, mysqldConfig.getCharset()).executeCommands(                     format("CREATE USER '%s'@'%%' IDENTIFIED BY '%s';", mysqldConfig.getUsername(), mysqldConfig.getPassword()));         } catch (IOException e) {             throw new RuntimeException(e);         }     }
protected MysqldProcess start(             final Distribution distribution,             final MysqldConfig config,             final IRuntimeConfig runtime) throws IOException {         logger.info("Preparing mysqld for startup");         Setup.apply(config, executable, runtime);         logger.info("Starting MysqldProcess");         return new MysqldProcess(distribution, config, runtime, this);     }

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

免安裝原生產(chǎn)環(huán)境的MySQL是什么

public AbstractProcess(Distribution distribution, T config, IRuntimeConfig runtimeConfig, E executable)       throws IOException {     this.config = config;     this.runtimeConfig = runtimeConfig;     this.executable = executable;     this.distribution = distribution;     // pid file needs to be set before ProcessBuilder is called     this.pidFile = pidFile(this.executable.getFile().executable());      ProcessOutput outputConfig = runtimeConfig.getProcessOutput();      // Refactor me - to much things done in this try/catch     String nextCall="";     try {        nextCall="onBeforeProcess()";        onBeforeProcess(runtimeConfig);        nextCall="newProcessBuilder()";        ProcessBuilder processBuilder = ProcessControl.newProcessBuilder(           runtimeConfig.getCommandLinePostProcessor().process(distribution,               getCommandLine(distribution, config, this.executable.getFile())),           getEnvironment(distribution, config, this.executable.getFile()), true);         nextCall="onBeforeProcessStart()";        onBeforeProcessStart(processBuilder, config, runtimeConfig);        nextCall="start()";        process = ProcessControl.start(config.supportConfig(), processBuilder);        nextCall="writePidFile()";        if (process.getPid() != null) {         writePidFile(pidFile, process.getPid());       }        nextCall="addShutdownHook()";        if (runtimeConfig.isDaemonProcess() && !executable.isRegisteredJobKiller()) {         ProcessControl.addShutdownHook(new JobKiller());         registeredJobKiller = true;       }        nextCall="onAfterProcessStart()";       onAfterProcessStart(process, runtimeConfig);     } catch (IOException iox) {       stop();       throw iox;     }   }

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

免安裝原生產(chǎn)環(huán)境的MySQL是什么

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

免安裝原生產(chǎn)環(huán)境的MySQL是什么

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

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

到此,關(guān)于“免安裝原生產(chǎn)環(huán)境的MySQL是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI