【问题标题】:Why won't the database connection work in the console with cakephp?为什么在使用 cakephp 的控制台中数据库连接不起作用?
【发布时间】:2013-09-13 13:41:00
【问题描述】:

我正在尝试将 bake 与 cakephp 一起使用。我已经到了下面的蛋糕欢迎屏幕:

Welcome to CakePHP v2.3.0-RC2 Console
---------------------------------------------------------------
App : app
Path: /Users/philip7899/Desktop/Philips Server 2/blog/app/
---------------------------------------------------------------
Interactive Bake Shell
---------------------------------------------------------------
[D]atabase Configuration
[M]odel
[V]iew
[C]ontroller
[P]roject
[F]ixture
[T]est case
[Q]uit
What would you like to Bake? (D/M/V/C/P/F/T/Q)

我输入 C,它会询问我的数据库配置:

Bake Controller
Path: /Users/philip7899/Desktop/Philips Server 2/blog/app/Controller/
---------------------------------------------------------------
Use Database Config: (default/test) 
[default] > 

我按下回车键确认使用“默认”数据库配置。我已经贴在下面了:

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => 'root',
    'database' => 'cakephp_blog',
    'prefix' => '',

    //'encoding' => 'utf8',
);

然后我收到以下错误:

Warning Error: PDO::__construct(): [2002] No such file or directory 
(trying to connect via unix:///var/mysql/mysql.sock) in 
[/Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Model/Datasource/Database/Mysql.php, line 157]

Error: Database connection "Mysql" is missing, or could not be created.
#0 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Model/Datasource/DboSource.php(261): Mysql->connect()
#1 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Model/ConnectionManager.php(101): DboSource->__construct(Array)
#2 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Console/Command/Task/ModelTask.php(903): ConnectionManager::getDataSource('default')
#3 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Console/Command/Task/ControllerTask.php(396): ModelTask->getAllTables('default')
#4 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Console/Command/Task/ControllerTask.php(419): ControllerTask->listAll(NULL)
#5 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Console/Command/Task/ControllerTask.php(136): ControllerTask->getName()
#6 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Console/Command/Task/ControllerTask.php(61): ControllerTask->_interactive()
#7 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Console/Command/BakeShell.php(113): ControllerTask->execute()
#8 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Console/Shell.php(391): BakeShell->main()
#9 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Console/ShellDispatcher.php(199): Shell->runCommand(NULL, Array)
#10 /Users/philip7899/Desktop/Philips Server 2/blog/lib/Cake/Console/ShellDispatcher.php(67): ShellDispatcher->dispatch()
#11 /Users/philip7899/Desktop/Philips Server 2/blog/app/Console/cake.php(33): ShellDispatcher::run(Array)
#12 {main}

过去几个小时我一直在搜索互联网并试图解决这个问题,但我无法解决它。我知道数据库配置正在运行,因为网站本身运行良好。我怎样才能让蛋糕在控制台中工作并让我继续?谢谢。

【问题讨论】:

  • 首先我要评论的是,当 2.3 分支已经在 2.3.10 中时,您正在使用 PHP 2.3.0 的候选发布版本。不说了,既然你好像在windows下,那你是怎么安装mysql、php和apache的呢?
  • 尝试'host' => '127.0.0.1',而不是'host' => 'localhost',
  • 我有 Mac 和 Mamp,我相信我正在使用 php 5.3。我尝试了 Joshuas 关于更改 'host' => '127.0.0.1' 的建议,但这也不起作用。有效的一件事(我认为)是在要求数据库配置时输入“test”(记住它提示我输入默认值或测试)并且它有效。但我不明白,因为“测试”数据库配置和“默认”数据库配置都是相同的。这怎么可能?
  • 您是否集中安装了 CakePHP / 您是否调用 cake bake 相对于您的 APP 目录?

标签: php cakephp console database-connection cakephp-bake


【解决方案1】:

类 DATABASE_CONFIG {

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => 'admin',// your password
    'database' => 'gps',
    'prefix' => '',
    //'encoding' => 'utf8',
);

public $test = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'user',
    'password' => 'password',
    'database' => 'test_database_name',
    'prefix' => '',
    //'encoding' => 'utf8',
);

} 你在 config 文件夹中的 database.php 上试试这个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多