配置 MYSQL 和 node.js 时遇到问题

问题描述 投票:0回答:1
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;

我在训练营,我们正在进行一个小组项目,我收到一个错误

connect ENOENT /tmp/mysql.sock

我的导师告诉我们这个

     socketPath: "/tmp/mysql.sock",```


is for mac users, and im on windows.
is there a way to write this so it ignores this if you're on windows but runs it if you're on mac?

i've searched the web and haven't really been able to find anything? so i've just been commenting it out locally and its been working, but theres gotta be a better way
mysql node.js sequelize.js config dotenv
1个回答
0
投票

通常人们解决这个问题的方法是,如果代码需要在多个环境(如 Mac、Windows 或托管提供商)上运行,他们不会将 MySQL 连接设置添加到代码中,而是将其添加到配置文件中。因此,只需将其作为您的

.env
配置的一部分即可。

© www.soinside.com 2019 - 2024. All rights reserved.