1

I have been looking for a way to use mysql connection string to create a pool rather than using the credentials, this is so I can use the connection string from azure application settings, but so far could not find a solution.

I can create a pool this way:

pool = mysql.createPool({
    connectionLimit: 20,
    host: HOST,
    user: USER,
    password: PASSWORD,
    database: DATABASE,
    debug: false
});

But what I want to achieve is something like this:

pool = mysql.createPool(process.env.MYSQLCONNSTR_connectionstring);

I am using the module felixge/node-mysql.

3 Answers 3

3

I was trying with the connection string provided by azure mysql db which was in the format:

Database=DATABASE;Data Source=HOST;User Id=USERID;Password=PASSWORD

But it turns out this format was not supported and it worked when I used this:

mysql://USERID:PASSWORD@HOST/DATABASE
Sign up to request clarification or add additional context in comments.

Comments

1

Since the answer is not clearly written i wanted to clarify. I specified the connectionLimit value in the query string and now it works :

let pool = mysql.createPool('mysql://xxx@xxx/xxx?charset=utf8mb4&connectionLimit=10');

Comments

0

Based on the source of node-mysql, unfortunately there is no way to create a pool with connection string.

Ref: PoolConfig.js

I think you should parse the connection string, there are every necessary information in it.

For example:

let connectionString = 'mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700';

// remove mysql://
connectionString = connectionString.substring(8, connectionString.length)

// Get user and password
let userAndPassw = connectionString.split('@')[0];
let user = userAndPassw.split(':')[0];
let password = userAndPassw.split(':')[1];

// Host
let host = connectionString.split('@')[1];
host = host.split('?')[0];

1 Comment

Thanks for the suggestion, I was too thinking of doing that but it turns out it was possible to create a pool with connection string, but the format was different than that with which I had tried.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.