简体   繁体   English

Doctrine2(Symfony4)数据库连接url over socket

[英]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. 我很难获得数据库连接以使用Symfony4 w / doctrine2中使用的mysql url格式。

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. Doctrine2文档不清楚套接字连接的格式。

I also faced this problem when trying to use google cloud platform SQL for mysql that requires to use a local unix socket. 当我尝试使用需要使用本地unix套接字的mysql的google云平台SQL时,我也遇到了这个问题。

Every part of the URL is not mandary, it's in the form : URL的每个部分都不是强制性的,它的形式如下:

<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 unix套接字,如果需要指定用户和密码,可以执行以下操作:

mysql://user:password@127.0.0.1/db_name/?unix_socket=/path/to/socket

The 127.0.0.1 part will be ignored. 127.0.0.1部分将被忽略。

You can test that this is working through the parse_url function (that is used by doctrine internally): 您可以通过parse_url函数(内部使用doctrine)测试这是否正常工作:

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"
}
  1. Configurate .env : 配置.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: 在项目的根目录中运行create database命令:

php bin/console doctrine:database:create

  1. Run command for create schema, if you was generate entities: 如果生成实体,请运行create schema命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM