Database not being selected in rails project when attempting to rake db:migrate

痞子三分冷 提交于 2019-12-06 23:47:16

问题


Working with a rails app, having some manner of weird database / rake issues.

When I execute:

rake db:migrate

I am getting the following error:

Mysql2::Error: No database selected: SHOW TABLES

(See full trace by running task with --trace)

The trace isn't revealing much useful information. Can be seen here: http://pastebin.com/WdsguudC

The config file looks right, and the user is getting logged in, or I would have gotten some kind of access error. The database exists, the user has correct permission, and I can access and manipulate it manually. I have done a bunch of googling, and haven't found anything helpful. Not sure if there is any other code that needs provided, because this seems like fairly low level problem.


回答1:


after all that it was a spacing issue in the yaml.




回答2:


Note that ruby has exchanged its YAML parser in a recent 1.9.2 version.

This might also cause this problem.

In order to switch back to the old YAML parser syck, use this in boot.rb:

require 'yaml'
YAML::ENGINE.yamler= 'syck'



回答3:


I had the same issue with ruby 1.9.2-p180 , upgraded to p290 and it works




回答4:


Well, it is a common issue for us beginners. This issue comes from the moment when you create your new project in rails. Let’s say to have an example

$ rails new toy –d mysql
  • After you do the bundle and start your server, most likely you will have an error. To correct it you need to go to your database.yml and modify the following:

Add a password in the password field as shown below, this is the password you use to secure mysql.

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: mypassword
  socket: /tmp/mysql.sock

Also, comment out the database adding a hash tag (#)before the name as shown below

development:
 : *default
   database: #toy_development
  • Then restart your command line and go to the root of your application and type:
$ rails s 

You have to see the Ruby on Rails welcome page..

  • After, you need to create a database.

Create a DATABASE.

The issue message is saying that not DATABASE is selected. It is because I didn’t create one. When you work with MySQL you have to create one, so:

  • Go to the root of my application and type:
$ mysql –u root –p 
$ Passwor: mypassword (Enter your password, this is the one you entered to secure MySQL)

Note: This example works wit a project called toy and the user I wanted to grant privileges is mark and the password I’ll give is 45mark. Below you will see where I apply these elements. Remember to apply your own elements on each part of the statement.

Create and user for this project

  • Once you are in, you will see the pointer (mysql> ), so type after it:
mysql> GRANT ALL PRIVILEGES ON toy_development.* TO 'mark'@'localhost' IDENTIFIED BY '45mark';
  • Then type:
mysql> exit;
  • Check that it is working by typing:
$ mysql –u mark –p toy_development
Enter password: 45mark (You enter the one you gave)
  • Open database.yml file and configure what is needed and fix as required. In my case I will chance the username to mark and the password to 45mark
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: mark
  password: 45mark
  socket: /tmp/mysql.sock


- Also, REMOVE the hash tag (#) added before

development:
  : *default
     database: toy_development 

Save it.

  • Go to the root of the application and type
$ rake db:schema:dump

Done!!

I hope this helps. Happy coding!!

Thanks




回答5:


Just restart the server; in the command line:

  1. Press Ctrl + C

  2. execute:

    rails s
    



回答6:


I had a similar error when i typed rake db:schema:dump and it turns out that I just have to comment out all the databases on my yaml file except my development one.




回答7:


Give a try to this.

rake db:test:prepare

Install this to see if you have actually created a table or not. Open the "development.sqlite3" in db folder

http://sqlitebrowser.org/




回答8:


Its a simple error checkout the entire database.yml file and see that where is default decription is given database name is given or not if not then look below it there will another development name is also given where configuration of database is use check that give your database name in it

default: &default
     adapter: mysql2
     encoding: utf8
     pool: 5
     username: root
     password: 12345
     host: localhost

development:
      <<: *default
      database: db_name



回答9:


One potential cause is that there is a DATABASE_URL environment variable defined.

$ echo $DATABASE_URL
=> mysql2://root@localhost:3306

If you get a similar output to the above url (i.e., the database name is not present), then you might want to add the database name to the string or unset the env var.

$ export DATABASE_URL=mysql2://root@localhost:3306/my_rails_app_development
$ unset DATABASE_URL

If you unset the var, you probably want to specify the database details in database.yml instead.



来源:https://stackoverflow.com/questions/4912287/database-not-being-selected-in-rails-project-when-attempting-to-rake-dbmigrate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!