0

Is there any way to get Database Name from MySql connection string?

For example, MySQL data string can be any of the two types.

i). mysql://root:password@localhost/test

ii). mysql://root:password@localhost/test?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700

So how to get the database name like 'test'(in this case). From any one of the two above given strings?

Thanks :)

2 Answers 2

2

Node.js's built-in URL module will be able to reliably parse your example connection strings.

var url = require('url')
var parsed = url.parse('mysql://root:password@localhost/test?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700')
var databaseName = parsed.pathname
1

If regex is an option, this is an approach:

var regex = /\/([^\/\?]+)(?:\?.+)?$/;

"mysql://root:password@localhost/test".match(regex)[1]

"mysql://root:password@localhost/test?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700".match(regex)[1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.