Home >Database >Mysql Tutorial >How to Fix the \'ECONNREFUSED\' Error When Connecting to MySQL from Node.js?
Troubleshooting ECONNREFUSED Error in Node.js MySQL Connection
When attempting to establish a database connection using Node.js and MySQL, you may encounter the "connect ECONNREFUSED" error, indicating a connection issue. Here are some potential solutions:
1. Enable MySQL Networking:
Check the MySQL configuration file (my.cnf) and ensure that "skip-networking" is commented out. This parameter disables network connections, which could be the cause of the error.
2. Set MySQL Socket:
Explicitly specify the MySQL socket by setting the "_socket" property in the client configuration. This ensures that the connection is established via the Unix socket instead of the TCP/IP interface.
<code class="js">var client = mysql.createClient({ user: uuuu, password: pppp, host: '127.0.0.1', port: '3306', _socket: '/var/run/mysqld/mysqld.sock', });</code>
The above is the detailed content of How to Fix the \'ECONNREFUSED\' Error When Connecting to MySQL from Node.js?. For more information, please follow other related articles on the PHP Chinese website!