简体   繁体   English

如何使用Doctrine为Symfony2建立数据库?

[英]How do I set up a database for Symfony2 using Doctrine?

I'm following the Symfony book and arrived at the chapter on dealing with the database . 我遵循Symfony的书,到达了有关处理数据库的章节 I have a few questions, and can't seem to find an answer. 我有几个问题,似乎找不到答案。

I'm just following the parameters.yml file that I have looks like this (auto-generated) 我只是遵循我看起来像这样的parameters.yml文件(自动生成)

parameters:
    database_driver: pdo_mysql
    database_host: 127.0.0.1
    database_port: null
    database_name: myproject
    database_user: root
    database_password: null
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    locale: en
    secret: ThisTokenIsNotSoSecretChangeIt
    debug_toolbar: true
    debug_redirects: false
    use_assetic_controller: true

Then I head over to the terminal and enter: 然后我前往终端并输入:

php app/console doctrine:database:create

I receive the following error: 我收到以下错误:

Could not create database for connection named `myproject`
SQLSTATE[HY000] [2002] Connection refused

I have a few questions: 我有几个问题:

  • How do I fix this 我该如何解决
  • Where is the database server? 数据库服务器在哪里? Did it come when I installed Symfony2 with Composer? 当我在Composer上安装Symfony2时出现了吗?

After a long search I found it. 经过长时间的搜索,我找到了它。 It had something to do with the mysql.default_socket as pointed out in the comments. 如注释中所指出的,它与mysql.default_socket有关。

MAMP showed me the socket was MAMP告诉我插座是

':/Applications/MAMP/tmp/mysql/mysql.sock'

After doing some more searching, I found that the socket can be set in config.yml, here: 经过更多搜索后,我发现可以在config.yml中设置套接字,这里:

doctrine:
  dbal:
    driver:   "%database_driver%"
    [...]
    unix_socket: "/Applications/MAMP/tmp/mysql/mysql.sock"
    [...]

If the unix_socket line is not there, just add it. 如果unix_socket行不存在,则添加它。 Then tried to execute the same command in Terminal, which gave this result: 然后尝试在Terminal中执行相同的命令,结果如下:

Created database for connection named `myproject`

And the database was really created. 数据库是真正创建的。

Symfony doesn't install mysql I assume you are working in a lamp stack. Symfony不会安装mysql,我假设您正在使用灯泡堆栈。

If I remember well it happened to me once and I changed database_host: 127.0.0.1 to localhost and it worked well since. 如果我没记错的话,那是我一次,所以我将database_host:127.0.0.1更改为localhost,此后运行良好。

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

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