

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 我無法連接到 MySQL (SSL 和授權的問題)
<a name="troubleshoot-connect-mysql"></a>

若要在 MySQL 中檢查一些常見的連線問題，請使用下列步驟。此程序可協助您了解是否已啟用 SSL 並授予使用權利。

**若要尋找一些 MySQL 中常見連接問題的解決方案**

1. 檢查 `/etc/my.cnf` 確保已為 MySQL 啟用 SSL。

1. 在 MySQL 中，執行以下命令。

   ```
   show status like 'Ssl%';
   ```

   如果 SSL 可運作，您會看到如下所示的結果。

   ```
   +--------------------------------+----------------------+
   | Variable_name                  | Value                |
   +--------------------------------+----------------------+
   | Ssl_accept_renegotiates        | 0                    |
   | Ssl_accepts                    | 1                    |
   | Ssl_callback_cache_hits        | 0                    |
   | Ssl_cipher                     |                      |
   | Ssl_cipher_list                |                      |
   | Ssl_client_connects            | 0                    |
   | Ssl_connect_renegotiates       | 0                    |
   | Ssl_ctx_verify_depth           | 18446744073709551615 |
   | Ssl_ctx_verify_mode            | 5                    |
   | Ssl_default_timeout            | 0                    |
   | Ssl_finished_accepts           | 0                    |
   | Ssl_finished_connects          | 0                    |
   | Ssl_session_cache_hits         | 0                    |
   | Ssl_session_cache_misses       | 0                    |
   | Ssl_session_cache_mode         | SERVER               |
   | Ssl_session_cache_overflows    | 0                    |
   | Ssl_session_cache_size         | 128                  |
   | Ssl_session_cache_timeouts     | 0                    |
   | Ssl_sessions_reused            | 0                    |
   | Ssl_used_session_cache_entries | 0                    |
   | Ssl_verify_depth               | 0                    |
   | Ssl_verify_mode                | 0                    |
   | Ssl_version                    |                      |
   +--------------------------------+----------------------+
   ```

   如果 SSL 已停用，您會看到如下所示的結果。

   ```
   +--------------------------------+-------+
   | Variable_name                  | Value |
   +--------------------------------+-------+
   | Ssl_accept_renegotiates        | 0     |
   | Ssl_accepts                    | 0     |
   | Ssl_callback_cache_hits        | 0     |
   | Ssl_cipher                     |       |
   | Ssl_cipher_list                |       |
   | Ssl_client_connects            | 0     |
   | Ssl_connect_renegotiates       | 0     |
   | Ssl_ctx_verify_depth           | 0     |
   | Ssl_ctx_verify_mode            | 0     |
   | Ssl_default_timeout            | 0     |
   | Ssl_finished_accepts           | 0     |
   | Ssl_finished_connects          | 0     |
   | Ssl_session_cache_hits         | 0     |
   | Ssl_session_cache_misses       | 0     |
   | Ssl_session_cache_mode         | NONE  |
   | Ssl_session_cache_overflows    | 0     |
   | Ssl_session_cache_size         | 0     |
   | Ssl_session_cache_timeouts     | 0     |
   | Ssl_sessions_reused            | 0     |
   | Ssl_used_session_cache_entries | 0     |
   | Ssl_verify_depth               | 0     |
   | Ssl_verify_mode                | 0     |
   | Ssl_version                    |       |
   +--------------------------------+-------+
   ```

1. 確定您已在資料庫伺服器上安裝支援的 SSL 憑證。

1. 為特定使用者授予使用權利以使用 SSL 連線。

   ```
   GRANT USAGE ON *.* TO 'encrypted_user'@'%' REQUIRE SSL;                        
   ```

**注意**  
TLS 1.2 for MySQL 連線需要 MySQL 5.7.28 版或更新版本。如果您的 MySQL 伺服器僅強制執行 TLS 1.2 （例如 `tls_version = TLSv1.2`)，且伺服器版本低於 5.7.28，則 SSL 交握會失敗並顯示`Communications link failure`錯誤。若要解決此問題，請將 MySQL 或 Aurora MySQL 資料庫升級至 5.7.28 版或更新版本。

如需此範例中解決方案的更多詳細資訊，請參閱下列各項：
+ 《Amazon RDS 使用者指南》**中的[對 MySQL 資料庫執行個體的 SSL 支援](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_MySQL.html#MySQL.Concepts.SSLSupport.html)。
+ 《Amazon RDS 使用者指南》**中的[使用 SSL 加密與 DB 執行個體的連線](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html)。
+ [MySQL 文件](https://dev.mysql.com/doc/refman/5.6/en/using-encrypted-connections.html)