Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 (Symfony4) database connection url over socket

I'm having a hard time getting a database connection to work with the mysql url format used in Symfony4 w/ doctrine2.

Here is what I'm trying to do:

mysql://dbusername:dbpassword@unix_socket(/path/to/socket)/dbname

What am I doing wrong? Please advise. The Doctrine2 documentation isn't clear on the format for connection over a socket.

like image 542
Layton Everson Avatar asked Feb 04 '18 19:02

Layton Everson


1 Answers

I also faced this problem when trying to use google cloud platform SQL for mysql that requires to use a local unix socket.

Every part of the URL is not mandary, it's in the form :

<type>://[user[:password]@][host][:port][/db][?param_1=value_1&param_2=value_2...]

So for a mysql unix socket if you need to specify user and password, you can do :

mysql://user:[email protected]/db_name/?unix_socket=/path/to/socket

The 127.0.0.1 part will be ignored.

You can test that this is working through the parse_url function (that is used by doctrine internally):

php > var_dump(parse_url("mysql://user:[email protected]/db_name/?unix_socket=/path/to/socket"));
php shell code:1:
array(6) {
  'scheme' =>
  string(5) "mysql"
  'host' =>
  string(9) "127.0.0.1"
  'user' =>
  string(4) "user"
  'pass' =>
  string(8) "password"
  'path' =>
  string(9) "/db_name/"
  'query' =>
  string(27) "unix_socket=/path/to/socket"
}
like image 195
magnetik Avatar answered Oct 23 '22 05:10

magnetik