Ciekawa sprawa z MySql po SSL'u. Po zapoznaniu się z dokumentacją http://dev.mysql.com/doc/refman/5.0/en/secure-create-certs.html wyszło mi takie coś (tak na szybko):
Primo:
Stworzyłem sobie usera w ten sposób:
CREATE DATABASE jdoe CHARSET utf8;
GRANT ALL PRIVILEGES ON jdoe.* TO 'jdoe'@'%' IDENTIFIED BY 'Pa$$w0rd' REQUIRE SSL;
FLUSH PRIVILEGES;
Secundo:
Mój serwer MySql śmiga na Debianie (Debian Squeeze 6.0.1 + MySql 5.1.49-3) pod adresem: 10.0.0.100. Certyfikaty ca-cert.pem, server-cert.pem i client-cert.pem wygenerowane na podstawie tego: http://dev.mysql.com/doc/refman/5.0/en/secure-create-certs.html (Example 1) tak więc client-cert.pem jest bez hasła (opcja -nodes o ile się nie mylę).
Tertio:
Aplikacja konsolowa z referencjami do: MySql.Data, System, System.Core, System.Data (używam VS 2008 Pro + MySQL Connector Net 6.3.5 ale w VS 2010 nie powinno być różnicy - wszystko zależy od wersji MySQL Connector)
using System;
using System.Text;
using MySql.Data.MySqlClient;
namespace MysqlSslConnectionTest
{
class Program
{
static void Main(string[] args)
{
StringBuilder connnectionString = new StringBuilder();
connnectionString.Append("Server=10.0.0.100;");
connnectionString.Append("User=jdoe;");
connnectionString.Append("Password=Pa$$w0rd;");
connnectionString.Append("Database=jdoe;");
connnectionString.Append("Port=3306;");
connnectionString.Append("Charset=utf8;");
connnectionString.Append("CertificateFile=client-cert.pem;");
connnectionString.Append("SSL Mode=Required;");
// Sprawdźmy connection string
Console.WriteLine("Connection string: ");
Console.WriteLine(connnectionString.ToString());
Console.WriteLine();
// ok no to jazda
Console.WriteLine("Connecting to MySql using SSL Connection...");
try
{
// Nowe połączenie
MySqlConnection mysqlConnection = new MySqlConnection(connnectionString.ToString());
// Otwórzmy je
mysqlConnection.Open();
// Jeśli do tej pory nie wywaliło błędu to mamy połączenie
Console.WriteLine("*** SUCCESS: Connection established...");
// Stwórzmy nowe polecenie
MySqlCommand mysqlCommand = mysqlConnection.CreateCommand();
// Na początek sprawdźmy datę i czas
mysqlCommand.CommandText = "SELECT NOW();";
mysqlCommand.Connection = mysqlConnection;
// Stwórzmy DataReader'a
MySqlDataReader mysqlDataReader = mysqlCommand.ExecuteReader();
while (mysqlDataReader.Read())
{
// Pętla po kolumnach
for (int i = 0; i < mysqlDataReader.FieldCount; i++)
{
// Wypiszmy je
Console.WriteLine(mysqlDataReader.GetValue(i).ToString());
}
}
// Zamykamy naszego DataReader'a bo inaczej dostaniemy błąd:
// There is already an open DataReader associated with this
// Connection which must be closed first.
mysqlDataReader.Close();
Console.WriteLine();
// Teraz sprawdźmy jaką wartość ma Ssl_cipher
mysqlCommand.CommandText = "SHOW STATUS LIKE 'Ssl_cipher';";
mysqlDataReader = mysqlCommand.ExecuteReader();
while (mysqlDataReader.Read())
{
for (int i = 0; i < mysqlDataReader.FieldCount; i++)
{
Console.WriteLine(mysqlDataReader.GetValue(i).ToString());
}
}
// cleanup
mysqlDataReader.Close();
mysqlConnection.Close();
}
catch (MySqlException mysqlEx)
{
// Jeśli napotkamy błąd MySql
Console.WriteLine("*** ERROR [" + mysqlEx.ErrorCode.ToString() + "]: Connection failed.");
Console.WriteLine("*** " + mysqlEx.Message);
}
catch (Exception ex)
{
// Jeśli to jest jakiś inny błąd
Console.WriteLine("*** ERROR: Unknown error.");
Console.WriteLine("*** " + ex.Message);
}
// by okienko konsoli za szybko nam nie umknęło...
Console.ReadKey();
}
}
}
no i wynik:
Connection string:
Server=10.0.0.100;User=jdoe;Password=Pa$$w0rd;Database=jdoe;Port=3306;Charset=utf8;CertificateFile=client-cert.pem;SSL Mode=Required;
Connecting to MySql using SSL Connection...
*** SUCCESS: Connection established...
2011-04-14 22:50:14
Ssl_cipher
AES256-SHA
jakby co mogę zapodać zipa z programem (kody źródłowe)