Search code examples
mysqlnpmvisual-studio-codelaragon

Change Node script MySQL default port


I've successfully changed my MySQL port from 3306 to 3400 and it works great on the workbench. However, I want to change the default port when I run npm script as well as it still runs on 3306, how will I change its default port.

The idea is I want to have an isolated port when I work on both Laragon (which should run on 3306) and MySQL port: 3400 (when I run npm on my VS Code and have my database queried).

I can't exactly find a question about this since I also don't know what exactly will I search in this scenario. Thank you very much for the help.


Solution

  • When you creating the MySQL connection you can provide your port also

    var client = mysql.createClient({
      userName: 'uname',
      password: 'pass123',
      port: 3400
    });
    

    Also, it's better to create a .env file and define your userName, password, port, API keys,etc on there and use the env variable to access those details.

    so with env,

    var client = mysql.createClient({
      userName: process.env.DB_USER,
      password: process.env.DB_PASS,
      port: process.env.PORT
    });