我是MemSQL数据库的新手,我在Linux Redhat V6上安装了MemSQL数据库服务器。 我还创建了一个名为" Test"的测试数据库,用户名为:root,密码为:root 我可以在本地连接到这个数据库,我也知道我可以使用MySQL提供程序从ODBC连接到Windows的MemSQL数据库。我尝试从Windows连接到MemSQL,但是我收到了一个错误(连接失败:驱动程序丢失了连接到MySQL服务器'读取初始通信数据包')。 我的问题是:是否可以从Windows连接到MemSQL数据库?如果是的话,我怎样才能使用C#(我应该使用什么提供商,配置等)。 谢谢
答案 0 :(得分:2)
这看起来更像是网络/防火墙问题。最简单的检查方法是在客户端和服务器上禁用防火墙,以查看是否一切正常。
MemSQL只使用MySQL兼容性,因此官方MySQL C#客户端可以正常工作。您可以从nuget:defaultdict
安装它using System;
using System.Data;
using System.Threading;
using System.Diagnostics;
using MySql.Data.MySqlClient;
namespace MemSQLTest {
//Thread creates a connection and inserts for 1000 milliseconds
public class WorkerThread {
public static void Thread() {
try {
IDbConnection conn;
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = "Server=127.0.0.1;Uid=root;Pwd=;";
conn.Open();
IDbCommand dbcmd = conn.CreateCommand();
dbcmd.CommandText = "USE db";
dbcmd.ExecuteNonQuery();
dbcmd.CommandText = "INSERT INTO t VALUES (DEFAULT)";
Stopwatch stop = new Stopwatch();
stop.Start();
while(stop.ElapsedMilliseconds < 1000)
{
dbcmd.ExecuteNonQuery();
}
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
class MemSQLTest {
static void Main(string[] args) {
try {
IDbConnection conn;
conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = "Server=127.0.0.1;Uid=root;Pwd=;";
conn.Open();
//Initialize database with auto increment table
IDbCommand dbcmd = conn.CreateCommand();
dbcmd.CommandText = "DROP DATABASE IF EXISTS db";
dbcmd.ExecuteNonQuery();
dbcmd.CommandText = "CREATE DATABASE db";
dbcmd.ExecuteNonQuery();
dbcmd.CommandText = "USE db";
dbcmd.ExecuteNonQuery();
dbcmd.CommandText = "CREATE TABLE t (id int primary key auto_increment)";
dbcmd.ExecuteNonQuery();
//Initialize threads
Thread[] threads = new Thread[10];
for(int i = 0; i < 10; i++)
{
threads[i] = new Thread(new ThreadStart(WorkerThread.Thread));
threads[i].Start();
}
for(int i = 0; i < 10; i++)
{
threads[i].Join();
}
//Show select count(*) on auto increment table
dbcmd.CommandText = "SELECT COUNT(*) FROM t";
IDataReader reader = dbcmd.ExecuteReader();
while(reader.Read()) {
Console.WriteLine(reader["COUNT(*)"]);
}
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
}
以下是MemSQL文档中用于测试插入的快速示例:
https://www.nuget.org/packages/MySql.Data/
[
{
id: 1,
name: 'test 1'
},
{
id: 2,
name: 'test 2'
},
{
id: 3,
name: 'test 3'
},
{
id: 4,
name: 'test 4'
}
]