一、使用SSL安全連接配接
To use SSL connections between the MySQL server and client programs, your system must support either OpenSSL or yaSSL and your version of MySQL must be built with SSL support.
To make it easier to use secure connections, MySQL is bundled with yaSSL as of MySQL 5.0.10. (MySQL and yaSSL employ the same licensing model, whereas OpenSSL uses an Apache-style license.) yaSSL support initially was available only for a few platforms, but now it is available on all platforms supported by MySQL AB.
To get secure connections to work with MySQL and SSL, you must do the following:
If you are not using a binary (precompiled) version of MySQL that has been built with SSL support, configure a MySQL source distribution to use SSL. When you configure MySQL, invoke the configure script with the appropriate option to select the SSL library that you want to use.
For yaSSL:
For OpenSSL:
Before MySQL 5.0, it was also neccessary to use <code>--with-vio</code>, but that option is no longer required.
Note that yaSSL support on Unix platforms requires that either <code>/dev/urandom</code> or <code>/dev/random</code> be available to retrieve true random numbers. For additional information (especially regarding yaSSL on Solaris versions prior to 2.8 and HP-UX)
Make sure that you have upgraded your grant tables to include the SSL-related columns in the <code>mysql.user</code> table. This is necessary if your grant tables date from a version of MySQL older than 4.0.
To check whether a server binary is compiled with SSL support, invoke it with the <code>--ssl</code> option. An error will occur if the server does not support SSL:
To check whether a running mysqld server supports SSL, examine the value of the <code>have_openssl</code> system variable:
If the value is <code>YES</code>, the server supports SSL connections. If the value is <code>DISABLED</code>, the server supports SSL connections but was not started with the appropriate <code>--ssl-<code>xxx</code></code> options (described later in this section). If the value is <code>YES</code>, the server supports SSL connections.
To start the MySQL server so that it allows clients to connect via SSL, use the options that identify the key and certificate files the server needs when establishing a secure connection:
<code>--ssl-ca</code> identifies the Certificate Authority (CA) certificate.
<code>--ssl-cert</code> identifies the server public key. This can be sent to the client and authenticated against the CA certificate that it has.
<code>--ssl-key</code> identifies the server private key.
To establish a secure connection to a MySQL server with SSL support, the options that a client must specify depend on the SSL requirements of the user account that the client uses.
If the account has no special SSL requirements or was created using a <code>GRANT</code> statement that includes the <code>REQUIRE SSL</code> option, a client can connect securely by using just the <code>--ssl-ca</code> option:
To require that a client certificate also be specified, create the account using the <code>REQUIRE X509</code> option. Then the client must also specify the proper client key and certificate files or the server will reject the connection:
In other words, the options are similar to those used for the server. Note that the Certificate Authority certificate has to be the same.
A client can determine whether the current connection with the server uses SSL by checking the value of the <code>Ssl_cipher</code> status variable. The value of <code>Ssl_cipher</code> is non-empty if SSL is used, and empty otherwise. For example:
For the mysql client, you can use the <code>STATUS</code> or <code>/s</code> command and check the <code>SSL</code> line:
Or:
To establish a secure connection from within an application program, use the <code>mysql_ssl_set()</code> C API function to set the appropriate certificate options before calling <code>mysql_real_connect()</code>.
二、對資料庫帳号設定不同的安全連接配接類型
There are a number of different possibilities for limiting connection types for a given account:
<code>REQUIRE NONE</code> indicates that the account has no SSL or X509 requirements. This is the default if no SSL-related <code>REQUIRE</code> options are specified. Unencrypted connections are allowed if the username and password are valid. However, encrypted connections can also be used, at the client's option, if the client has the proper certificate and key files. That is, the client need not specify any SSL commmand options, in which case the connection will be unencrypted. To use an encrypted connection, the client must specify either the <code>--ssl-ca</code> option, or all three of the <code>--ssl-ca</code>, <code>--ssl-key</code>, and <code>--ssl-cert</code> options.
The <code>REQUIRE SSL</code> option tells the server to allow only SSL-encrypted connections for the account.
To connect, the client must specify the <code>--ssl-ca</code> option, and may additionally specify the <code>--ssl-key</code> and <code>--ssl-cert</code> options.
<code>REQUIRE X509</code> means that the client must have a valid certificate but that the exact certificate, issuer, and subject do not matter. The only requirement is that it should be possible to verify its signature with one of the CA certificates.
To connect, the client must specify the <code>--ssl-ca</code>, <code>--ssl-key</code>, and <code>--ssl-cert</code> options. This is also true for <code>ISSUER</code> and <code>SUBJECT</code> because those <code>REQUIRE</code> options imply <code>X509</code>.
<code>REQUIRE ISSUER '<code>issuer</code>'</code> places the restriction on connection attempts that the client must present a valid X509 certificate issued by CA <code>'<code>issuer</code>'</code>. If the client presents a certificate that is valid but has a different issuer, the server rejects the connection. Use of X509 certificates always implies encryption, so the <code>SSL</code> option is unnecessary in this case.
Note that the <code>'<code>issuer</code>'</code> value should be entered as a single string.
<code>REQUIRE SUBJECT '<code>subject</code>'</code> places the restriction on connection attempts that the client must present a valid X509 certificate containing the subject <code>subject</code>. If the client presents a certificate that is valid but has a different subject, the server rejects the connection.
Note that the <code>'<code>subject</code>'</code> value should be entered as a single string.
<code>REQUIRE CIPHER '<code>cipher</code>'</code> is needed to ensure that ciphers and key lengths of sufficient strength are used. SSL itself can be weak if old algorithms using short encryption keys are used. Using this option, you can ask that a specific cipher method is used to allow a connection.
The <code>SUBJECT</code>, <code>ISSUER</code>, and <code>CIPHER</code> options can be combined in the <code>REQUIRE</code> clause like this:
The <code>AND</code> keyword is optional between <code>REQUIRE</code> options.
三、為Mysql制作ssl證書
This section demonstrates how to set up SSL certificate and key files for use by MySQL servers and clients. The first example shows a simplified procedure such as you might use from the command line. The second shows a script that contains more detail. Both examples use the openssl command that is part of OpenSSL.
The following example shows a set of commands to create MySQL server and client certificate and key files. You will need to respond to several prompts by the openssl commands. For testing, you can press Enter to all prompts. For production use, you should provide non-empty responses.