Ruby-on-rails 使用 rake db:create 创建 Rails DB 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2672756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:33:52  来源:igfitidea点击:

Error creating Rails DB using rake db:create

ruby-on-railsrubyrake

提问by Simon

I'm attempting to get my first "hello world" rails example going using the rails' getting started guideon my OSX 10.6.3 box.

我正在尝试使用我的 OSX 10.6.3 机器上的 rails入门指南来获得我的第一个“hello world” rails 示例。

When I go to execute the first rake db:createcommand (I'm using mysql) I get:

当我去执行第一个rake db:create命令(我正在使用 mysql)时,我得到:

simon@/Users/simon/source/rails/blog/config: rake db:create  (in /Users/simon/source/rails/blog) Couldn't create database for {"reconnect"=>false, "encoding"=>"utf8", "username"=>"root", "adapter"=>"mysql", "database"=>"blog_development", "pool"=>5, "password"=>nil, "socket"=>"/opt/local/var/run/mysql5/mysqld.sock"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)

I found plenty of stackoverflow questions addressing this problem with the following advice:

我发现了很多 stackoverflow 问题通过以下建议解决了这个问题:

  1. Verify that user and password are correct (I'm running w/ no password for root on my dev box)

  2. Verify that the socket is correct - I can cat the socket, so I assume it's correct

  3. Verify that the user can create a DB (As you can see root can connect and create a this DB no problem)

    simon@/Users/simon/source/rails/blog/config: mysql -uroot -hlocalhost Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 5.1.45 Source distribution

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    mysql> create database blog_development; Query OK, 1 row affected (0.00 sec)

  1. 验证用户和密码是否正确(我在我的开发框中运行时没有 root 密码)

  2. 验证套接字是否正确 - 我可以找到套接字,所以我认为它是正确的

  3. 验证用户是否可以创建数据库(如您所见,root 可以连接并创建此数据库没问题)

    simon@/Users/simon/source/rails/blog/config: mysql -uroot -hlocalhost 欢迎使用 MySQL 监视器。命令以 ; 结尾 或\g。您的 MySQL 连接 ID 为 16 服务器版本:5.1.45 源分发

    输入“帮助;” 或 '\h' 寻求帮助。键入 '\c' 清除当前输入语句。

    mysql> 创建数据库 blog_development; 查询正常,1 行受影响(0.00 秒)

to ensure that this wasn't a charset issue I also tried:

为确保这不是字符集问题,我还尝试过:

mysql> create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

Note: here is my database.yaml:

注意:这是我的database.yaml:

development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: blog_development
  pool: 5
  username: root
  password:
  socket: /opt/local/var/run/mysql5/mysqld.sock
#  host: localhost

Note that I tried switching socket to localhost with no effect.

请注意,我尝试将套接字切换到 localhost 没有任何效果。

Any idea on what might be going on here?

对这里可能发生的事情有任何想法吗?

回答by Simon

Thanks for all the help guys. Looks like the problem had to do with my install of the mysql gem under OSX.

感谢所有帮助的家伙。看起来问题与我在 OSX 下安装 mysql gem 有关。

@tim after I proceeded to the next step and got up and going I got an error on the console, so I did a bit of searching and found this helpful thread.

@tim 在我进行下一步并起身后,控制台上出现错误,因此我进行了一些搜索并找到了这个有用的线程

After I uninstalled my ruby gems gem uninstall mysqlI installed the proper mysql gems using this command (from the thread):

卸载我的 ruby​​ gems 后,我gem uninstall mysql使用以下命令(来自线程)安装了正确的 mysql gems:

export ARCHFLAGS="-arch i386 -arch x86_64" ; gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/opt/local/lib/mysql5 --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

export ARCHFLAGS="-arch i386 -arch x86_64" ; gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/opt/local/lib/mysql5 --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

After executing this one I was able to successfully run rake db:createand proceed.

执行此操作后,我能够成功运行rake db:create并继续。

Thanks!!

谢谢!!

回答by Pran

It could be a number of things.

这可能是很多事情。

  • Is your database set for utf8 character set?
  • Is the path to the socket correct, since it varies from OS.
  • Have you reinstalled the mysql gem? sudo gem install mysql
  • It might be MySQL, you might want to downgrade the version to 5.0
  • 您的数据库是否设置为 utf8 字符集?
  • 套接字的路径是否正确,因为它因操作系统而异。
  • 您是否重新安装了mysql gem? sudo gem install mysql
  • 可能是 MySQL,您可能想将版本降级到 5.0

Other than that, I'm not sure.

除此之外,我不确定。

回答by VP.

You should post here your database.yml

你应该在这里发布你的 database.yml

To make your test better, i would try to create a database UTF-8 to see if your database supports utf-8

为了让你的测试更好,我会尝试创建一个 UTF-8 数据库,看看你的数据库是否支持 utf-8

create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci

回答by Tim Snowhite

Does the blog_developmentdatabase already exist?

blog_development数据库是否已经存在?

If so, you can just continue to the next step.

如果是这样,您可以继续下一步。

Try running ruby script/serverin the blog/directory.

尝试ruby script/serverblog/目录中运行。

If it doesn't error out, then you should be able to navigate to localhost:3000and continue the tutorial from here http://guides.rubyonrails.org/getting_started.html#hello-rails.

如果它没有出错,那么您应该能够localhost:3000从这里导航到并继续本教程http://guides.rubyonrails.org/getting_started.html#hello-rails

Leave me a comment if ruby script/servererrors out.

如果有ruby script/server错误,请给我留言。

回答by user341080

I just had the same issue : it was an old mysql gem which was not up to date. Re-installing the mysql gem did the trick.

我只是遇到了同样的问题:这是一个旧的 mysql gem,不是最新的。重新安装 mysql gem 成功了。

回答by Nimesh Nikum

Re-install mysql-server and mysql-client using this command:

使用以下命令重新安装 mysql-server 和 mysql-client:

sudo apt-get install mysql-server mysql-client

and then some libraries you need to install to make MySQL available to ruby:

然后你需要安装一些库来使 MySQL 可用于 ruby​​:

sudo apt-get install libmysql-ruby

This all solved my problem. Try it !!! :)

这一切都解决了我的问题。尝试一下 !!!:)