Encountered the trouble of configuring MYSQL and node.js
P粉489081732
2023-08-15 10:55:24
<p><br /></p>
<pre class="brush:php;toolbar:false;">const Sequelize = require("sequelize");
require("dotenv").config();
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASSWORD,
{
host: "localhost",
dialect: "mysql",
port: 3306,
dialectOptions: {
socketPath: "/tmp/mysql.sock",
},
}
);
module.exports = sequelize;</pre>
<p>I'm at a coding bootcamp and we're working on a group project and I ran into a bug</p>
<p><code>connect ENOENT /tmp/mysql.sock</code></p>
<p>My instructor told us that this code is for Mac users, and I am using Windows.
Is there a way to write a conditional statement that ignores this code if running on Windows, but runs it when running on Mac?
I searched online and found nothing. So I had to comment it out locally, and while that worked, there had to be a better way. </p><p><br /></p>
Typically the way people solve this problem is that instead of adding the MySQL connection settings to the code if the code needs to run on multiple environments such as Mac, Windows or hosting providers, they add into the configuration file. So, make this part of your
.env
configuration.Having said that, you can also connect to MySQL on a Mac without using Unix sockets.