新OpenShift环境下Redmine无法连接本地MySQL服务器求助
Hey there, let's work through this MySQL socket connection issue you're facing when deploying Redmine on OpenShift. The error Mysql2::Error: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) is a common one, and it's almost always related to how your Redmine is configured to reach the MySQL instance in the OpenShift environment.
Here are the key steps to fix this:
Switch from socket-based to TCP/IP connection
In OpenShift, your MySQL database is almost certainly running as a separate Pod (not on the same node/container as Redmine), so local socket connections won't work. You need to update yourdatabase.ymlto use TCP/IP instead:- Remove the
socket: /var/lib/mysql/mysql.sockline from your configuration. - Add the
hostfield set to the name of your MySQL service in OpenShift (you can find this withoc get servicesin your project namespace). - Add the
portfield (default is3306unless you've modified the MySQL deployment).
Example production configuration snippet:
production: adapter: mysql2 database: redmine_production host: mysql-service-name port: 3306 username: your_db_username password: "your_db_password" encoding: utf8mb4- Remove the
Verify database credentials and service reachability
From your Redmine Pod, test if you can reach the MySQL service directly using themysqlclient (if installed):mysql -h <mysql-service-host> -u <db-username> -p <db-name>If this fails, double-check your credentials, ensure the MySQL service is up, and confirm the database name matches what's in
database.yml.Check MySQL Pod status
Make sure your MySQL Pod is in aRunningstate. Runoc get podsto check—if it's crashing or in a pending state, resolve those issues first (check logs withoc logs <mysql-pod-name>to debug startup errors).Validate network policies
Sometimes OpenShift network policies can block traffic between Redmine and MySQL. Ensure there's a policy in place that allows your Redmine Pod's namespace to access the MySQL service on port 3306.
After updating your database.yml, redeploy Redmine to apply the changes:
oc rollout restart deployment/redmine-deployment-name
Let me know if you hit any snags while working through these steps—I can help dig deeper if needed!
内容的提问来源于stack exchange,提问作者user3190036

