Specifying password in MySQL connection string

谁说我不能喝 提交于 2020-01-23 08:08:06

问题


I created ExpressJS MVC app using MySQL as DB with Yeoman generator and in config.js I want to change MySQL connection strings, but I don't know to specify password in string.

my string is mysql://root@localhost:3306/

Please help me.


回答1:


Password and DB name also a part of connection string.
Ex: mysql://root:password@localhost:port/dbName




回答2:


Maybe you can try something like this:

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'YOURPASSWORD',
  database : 'YOURDATABASE'
});

Take a look at this tutorial.

If this doesn't work, you should provide us with more information.




回答3:


Here's generic connection-string parser, which offers a flexible optional syntax, while adhering to the URL syntax. Works in any environment.

var ConnectionString = require('connection-string');
var obj = new ConnectionString('my-server:12345');



回答4:


I suggest using knex. Setting up a connection pool that manages your pool for you is trivial. See the documentation.

var knex = require('knex')({
  client: 'mysql',
  connection: {
    host     : '127.0.0.1',
    user     : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test'
  }
});


来源:https://stackoverflow.com/questions/39105583/specifying-password-in-mysql-connection-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!