php - 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:
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.
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?
解决方案
推荐阅读
- javascript - 除了 Vigenére Ciphers 之外,还有什么其他有效的加密长文本方法也可以在没有额外库的情况下进行压缩?
- android - 设计窗口只有一个预览窗口
- node.js - schedule.schedulejob 没有按预期工作
- git - 从 post-commit git-hook 运行 grunt
- ionic-framework - 深层链接提供空路径
- android - 使用 ConstraintLayout 的 Android 可访问性
- javascript - 如何防止 jquery 提交按钮刷新 HTML 页面?
- javascript - Selenium webdriver - 页面结束(使用垂直滚动)
- angular - APP_INITIALIZE 后初始化模块
- android - 无法在 android 设备的省电模式下以编程方式检查网络信息