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

3

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
Sign up to request clarification or add additional context in comments.

Comments

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]

Comments

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.