9

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.

Layton Everson
  • 1,148
  • 9
  • 18

2 Answers2

13

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:password@127.0.0.1/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:password@127.0.0.1/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"
}
magnetik
  • 4,351
  • 41
  • 58
  • 3
    FYI, you should use `localhost` instead of `127.0.0.1` (otherwise Doctrine won't ignore it) – Alain Tiemblo May 08 '19 at 07:15
  • 1
    It should be `/db_name?unix_socket...`, a trailing slash after `db_name` will be considered part of the database name. – janh Oct 27 '19 at 15:41
-4
  1. Configurate .env:

DATABASE_URL=mysql://root:root@127.0.0.1:3306/database_name

  1. Run command in your the project's root directory for create database:

php bin/console doctrine:database:create

  1. Run command for create schema, if you was generate entities:

php bin/console doctrine:schema:create

  1. Run command for update you schema which is based on your entities:

php bin/console doctrine:schema:update --force

Dmitry S.
  • 3,766
  • 3
  • 18
  • 26
  • I am not running this on a local server, and the server I am running it on does db server does not accept TCP connections. – Layton Everson Feb 06 '18 at 01:12
  • @LaytonEverson, in this case: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#pdo-mysql – Dmitry S. Feb 06 '18 at 13:37
  • @LaytonEverson, another thing to check for is that the mysql server in questions actually is configured to allow connections over the socket as opposed to using port 3306 which is the default TCP port. – Dmitry S. Feb 06 '18 at 13:39
  • Are you using 2 servers? You need to have access to socket file. If you cannot touch this file, you cannot connect. Socket is only for local connections. – Marcos Regis Feb 09 '18 at 16:14
  • @DmitrySmirnov I have used that as a reference but it doesn't specify how to construct the connection string... – Layton Everson Feb 13 '18 at 13:19
  • @MarcosRegis I'm using google cloud platform. the socket is created for me. I have since switched platforms. – Layton Everson Feb 13 '18 at 13:23