search

Home  >  Q&A  >  body text

Encountered the trouble of configuring MYSQL and node.js

<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>
P粉489081732P粉489081732547 days ago455

reply all(1)I'll reply

  • P粉190883225

    P粉1908832252023-08-16 00:30:11

    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.

    reply
    0
  • Cancelreply