首页 > 解决方案 > How can I connect my legacy PHP application to MySQL v8?

问题描述

I have a legacy PHP forum that I'd like to make available read-only for archive purposes. I've managed to get it running in a docker container using php 5.4, and I have it connecting just fine to a MySQL 5 container.

When I switch to using MySQL 8, it fails to connect for two reasons:

  1. MySQL 8 switched the default authentication protocol from mysql_native_password to caching_sha2_password, and php's mysql driver does not support the latter protocol. I fixed this by creating a user that uses the mysql_native_password authentication mechanism.

  2. MySQL 8 switched the default server's character set from utf8 to utf8mb4, which php's mysql driver does not support. It's this issue that has me stuck.

The PHP error is:

Warning: mysql_connect(): Server sent charset (255) unknown to the client.

I know I can fix it by changing the MySQL server's character set, but I'm running this in a Managed MySQL cluster in production (from Digital Ocean), and I don't have access to change that. So I'm wondering if I can change it on a database level, or client/connection level.

I've tried changing the database's encoding before importing the data using:

ALTER DATABASE
    database_name
    CHARACTER SET = utf8
    COLLATE = utf8_unicode_ci;

I've also tried changing the connection charset in PHP using:

$this->db_connect_id = mysql_connect($this->server, $this->user, $this->password);
mysql_set_charset("UTF8", $this->db_connect_id);

But the "Server sent charset unknown" error is on the mysql_connect line, so by the time mysql_set_charset runs, the error has already been thrown, and db_connect_id is false.

I also know that I could upgrade PHP and switch from the legacy mysql extension to mysqli or PDO, but this forum is from 2005, and I never intend to use it; it's just for archival purposes. I'm hoping it's possible to just get it working without having to upgrade it.

Any suggestions?

标签: phpmysqllegacymysql-8.0php-5.4

解决方案


推荐阅读