Error creating Rails DB using rake db:create

2019-02-15 10:29发布

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

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

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:

  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)

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:

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.

Any idea on what might be going on here?

6条回答
够拽才男人
2楼-- · 2019-02-15 10:40

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

sudo apt-get install mysql-server mysql-client

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

sudo apt-get install libmysql-ruby

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

查看更多
孤傲高冷的网名
3楼-- · 2019-02-15 10:45

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.

查看更多
老娘就宠你
4楼-- · 2019-02-15 10:52

Does the blog_development database already exist?

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

Try running ruby script/server in the blog/ directory.

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

Leave me a comment if ruby script/server errors out.

查看更多
放荡不羁爱自由
5楼-- · 2019-02-15 10:56

You should post here your database.yml

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

create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci
查看更多
The star\"
6楼-- · 2019-02-15 10:57

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

Other than that, I'm not sure.

查看更多
Lonely孤独者°
7楼-- · 2019-02-15 11:03

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

@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.

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

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:create and proceed.

Thanks!!

查看更多
登录 后发表回答