這篇文章主要介紹“免安裝原生產(chǎn)環(huán)境的MySQL是什么”,在日常操作中,相信很多人在免安裝原生產(chǎn)環(huán)境的MySQL是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”免安裝原生產(chǎn)環(huán)境的MySQL是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
成都創(chuàng)新互聯(lián)主營渭濱網(wǎng)站建設(shè)的網(wǎng)絡公司,主營網(wǎng)站建設(shè)方案,手機APP定制開發(fā),渭濱h5微信平臺小程序開發(fā)搭建,渭濱網(wǎng)站營銷推廣歡迎渭濱等地區(qū)企業(yè)咨詢
就是它:
<dependency> <groupId>com.wix</groupId> <artifactId>wix-embedded-mysql</artifactId> <version>x.y.z</version> <scope>test</scope> </dependency>
代碼也簡單,直接定義你需要的版本,數(shù)據(jù)庫信息,把要初始化的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)勢:
測試可以跑在和生產(chǎn)環(huán)境基本一致的環(huán)境,同樣的版本,同樣的編碼和配置,database/schema/user settings 等等
比安裝一個更容易,想切換版本,改配置也更輕松;
本地每個項目可以使用不同的版本,不同的配置,啥都不用擔心;
對于MySQL的多個版本支持 - 5.5, 5.6, 5.7, 8.0;
多種平臺和環(huán)境都支持。
原理
這背后是怎么實現(xiàn)的呢?
咱們是「刨根究底」公眾號,一起來看看。
上面代碼配置之后的 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); }
其實這背后依賴了一個叫embed.process的開源項目,
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; } }
它又操作了什么呢?從名字你也猜到了,它是直接操作進程的,實際在運行時,會下載一個MySQL,然后通過腳本啟停。
初次啟動的時候,會直接下載
有了這些,在測試的時候就可以和生產(chǎn)環(huán)境一樣,啟動時加載初始化SQL腳本,開始你的工作了。
github地址:https://github.com/wix/wix-embedded-mysql
到此,關(guān)于“免安裝原生產(chǎn)環(huán)境的MySQL是什么”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
文章標題:免安裝原生產(chǎn)環(huán)境的MySQL是什么
文章出自:http://muchs.cn/article40/jcgiho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站改版、軟件開發(fā)、網(wǎng)站設(shè)計公司、靜態(tài)網(wǎng)站、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)