Welcome Guest, Not a member yet? Register   Sign In
Cannot connect remote database
#1
Question 

I want get some data from aonther database, so i create a user [promission only have  "SELECT"], I check connect with terminal, it's good.
but in the codeigniter-4 . the logs is :
```
ERROR - 2023-10-10 11:26:03 --> Error connecting to the database: mysqli_sql_exception: Connection refused in /www/wwwroot/example.com/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php:185
Stack trace:
#0 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php(185): mysqli->real_connect()
#1 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/Database/BaseConnection.php(391): CodeIgniter\Database\MySQLi\Connection->connect()
#2 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/Database/BaseConnection.php(574): CodeIgniter\Database\BaseConnection->initialize()
#3 /www/wwwroot/example.com/app/Common.php(26): CodeIgniter\Database\BaseConnection->query()
#4 /www/wwwroot/example.com/app/Controllers/Detail.php(54): get_data()
#5 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/CodeIgniter.php(919): App\Controllers\Detail->index()
#6 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/CodeIgniter.php(494): CodeIgniter\CodeIgniter->runController()
#7 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/CodeIgniter.php(353): CodeIgniter\CodeIgniter->handleRequest()
#8 /www/wwwroot/example.com/public/index.php(79): CodeIgniter\CodeIgniter->run()
#9 {main}

Next CodeIgniter\Database\Exceptions\DatabaseException: Connection refused in /www/wwwroot/example.com/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php:229
Stack trace:
#0 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/Database/BaseConnection.php(391): CodeIgniter\Database\MySQLi\Connection->connect()
#1 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/Database/BaseConnection.php(574): CodeIgniter\Database\BaseConnection->initialize()
#2 /www/wwwroot/example.com/app/Common.php(26): CodeIgniter\Database\BaseConnection->query()
#3 /www/wwwroot/example.com/app/Controllers/Detail.php(54): get_data()
#4 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/CodeIgniter.php(919): App\Controllers\Detail->index()
#5 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/CodeIgniter.php(494): CodeIgniter\CodeIgniter->runController()
#6 /www/wwwroot/example.com/vendor/codeigniter4/framework/system/CodeIgniter.php(353): CodeIgniter\CodeIgniter->handleRequest()
#7 /www/wwwroot/example.com/public/index.php(79): CodeIgniter\CodeIgniter->run()
#8 {main}
CRITICAL - 2023-10-10 11:26:03 --> Unable to connect to the database.
Main connection [MySQLi]: Connection refused
in SYSTEMPATH/Database/BaseConnection.php on line 427.
1 SYSTEMPATH/Database/BaseConnection.php(574): CodeIgniter\Database\BaseConnection->initialize()
2 APPPATH/Common.php(26): CodeIgniter\Database\BaseConnection->query()
3 APPPATH/Controllers/Detail.php(54): get_data()
4 SYSTEMPATH/CodeIgniter.php(919): App\Controllers\Detail->index()
5 SYSTEMPATH/CodeIgniter.php(494): CodeIgniter\CodeIgniter->runController()
6 SYSTEMPATH/CodeIgniter.php(353): CodeIgniter\CodeIgniter->handleRequest()
7 FCPATH/index.php(79): CodeIgniter\CodeIgniter->run()

```
in my .env file, I set a database group named "remote".
Reply
#2

The reason is "Connection refused".

I asked ChatGPT.
Quote:The error you're encountering indicates that CodeIgniter is unable to connect to the database using the "remote" database group configuration specified in your .env file. The error message "Connection refused" suggests that there might be an issue with the database server, credentials, or networking.

Here are some steps you can take to troubleshoot and resolve the issue:

1. **Check Database Credentials**:
  Ensure that the database credentials (e.g., hostname, username, password, database name) specified in the .env file for the "remote" database group are correct.

2. **Verify Database Server**:
  Confirm that the database server specified in the .env file is accessible and running. Ensure that the server is accepting connections from the machine running your CodeIgniter application.

3. **Check Database Host and Port**:
  Double-check the database hostname and port specified in the .env file. Ensure that the port is correct and that the database server is listening on that port.

4. **Check Database Permissions**:
  Verify that the database user associated with the credentials in the .env file has the necessary permissions to perform SELECT queries on the specified database.

5. **Verify Database Connection**:
  Test the database connection manually using the credentials from the .env file. You can use a MySQL client or a tool like `mysqli` in PHP to establish a connection and execute a simple SELECT query to ensure connectivity.

6. **Check Firewall and Network Settings**:
  Ensure that there are no firewall rules or network restrictions preventing the CodeIgniter application from connecting to the database server.

7. **Review CodeIgniter Configuration**:
  Double-check the database configuration in CodeIgniter (e.g., app/Config/Database.php) to ensure it matches the settings in your .env file.

8. **Check Database Service Status**:
  Verify that the database service is running and accessible on the specified host and port. Check the database server logs for any related errors.

By carefully reviewing and verifying the database credentials, server settings, and CodeIgniter configurations, you should be able to identify and resolve the issue causing the "Connection refused" error.
Reply
#3

When you say,

Quote:I check connect with terminal, it's good.

is that from a terminal on the web server?
Reply
#4

Here are some specific things to check in your .env file:

Make sure that the database.remote.hostname setting is correct. This should be the IP address or hostname of the remote database server.
Make sure that the database.remote.username and database.remote.password settings are correct. These should be the credentials for the database user that you created with limited permissions.
Make sure that the database.remote.database setting is correct. This should be the name of the database that you want to connect to.
Once you have verified that all of the settings in your .env file are correct, you can try restarting CodeIgniter and connecting to the remote database again.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB