简体   繁体   English

可以在内存中模拟mysql服务器吗?

[英]Possible to simulate mysql server in memory?

I wrote some functions that work with SQL. 我编写了一些可用于SQL的函数。 I test the functions using testthat and an in memory SQLite database. 我使用testthat和内存中的SQLite数据库测试功能。 However, some functions cannot be tested using SQLite because SQLite does not support the ALTER TABLE command . 但是,某些功能无法使用SQLite进行测试,因为SQLite 不支持ALTER TABLE命令

Is there some way to simulate a mySQL database in memory the same way that one can simulate a SQLite? 有什么方法可以像模拟SQLite一样模拟内存中的mySQL数据库?

> DBI::dbConnect(RSQLite::SQLite(), ":memory:")
<SQLiteConnection>
  Path: :memory:
  Extensions: TRUE
> DBI::dbConnect(RMySQL::MySQL(), ":memory:")
Error in .local(drv, ...) : 
  Failed to connect to database: Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

If not, how does one write automatic tests for mySQL functions? 如果没有,如何编写自动测试mySQL函数?

You can't make a whole MySQL instance run in memory like the SQLite :memory: option. 您不能像SQLite :memory:选项那样使整个MySQL实例在内存中运行。 MySQL is designed to be a persistent database server, not an ephemeral embedded database like SQLite. MySQL被设计为永久数据库服务器,而不是像SQLite这样的临时嵌入式数据库。

However, you can use MySQL's MEMORY Storage Engine for individual tables: 但是,您可以对单个表使用MySQL的MEMORY存储引擎:

CREATE TABLE MyTable ( ...whatever... ) ENGINE=MEMORY;

The equivalent in RMySQL seems to be the dbWriteTable() method, but as far as I can tell from documentation, you can't specify the ENGINE when creating a table with this method. RMySQL中的等效方法似乎是dbWriteTable()方法,但是据我从文档可以得知,使用此方法创建表时无法指定ENGINE。 You'll have to create the table manually in the MySQL client if you want to use the MEMORY engine. 如果要使用MEMORY引擎,则必须在MySQL客户端中手动创建表。

But you should be aware that every storage engine has some subtle different behavior. 但是您应该意识到,每个存储引擎都有一些细微的不同行为。 If any of your tests depend on features of InnoDB, you won't be able to simulate them with the MEMORY storage engine (eg row-level locking, foreign keys, fulltext search). 如果您的任何测试取决于InnoDB的功能,您将无法使用MEMORY存储引擎(例如,行级锁定,外键,全文搜索)对其进行仿真。

Read the manual on the MEMORY storage engine: https://dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html 阅读有关MEMORY存储引擎的手册: https : //dev.mysql.com/doc/refman/5.7/en/memory-storage-engine.html

PS: Ignore the suggestions on that manual page to use NDB Cluster. PS:请忽略该手册页上的建议以使用NDB群集。 It may be faster, but it requires multiple servers and special database design to achieve that performance. 它可能更快,但需要多台服务器和特殊的数据库设计才能实现该性能。 It's much harder to set up. 设置起来要困难得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM