6

I am learning to use mysql for rails apps, and I finally have been able to get my app up and running (sort of, for now). I managed to install mysql with homebrew, and then populated my database.yml file and ran rake db:create. Now I am able to run my rails server and navigate to my homepage of my app (previously I got a no database error).

So, now that it seems that the database(s) is created, where is it located? I was wondering, because I previously used sqlite3 and was able to see my development database file in app/db/development.db

I just want to understand in greater detail how mysql works in a rails context.

4 Answers 4

6

When using MySQL or other full server-based RDBMS systems instead of SQLite, you won't have a database file created with your application. Instead, the database is stored inside the server's running MySQL instance.

Technically, there are files associated with the tables stored in the MySQL data directory, but their location isn't relevant to your application, and in fact may not even be readable by your regular shell user account. By using a client/server RDBMS instead of a file-based one like SQLite, you effectively delegate all responsibilities(*) related to database storage to the RDBMS, decoupling data storage from your application.

If you connect to your host's MySQL server with a client, you'll see the rails application databases listed with

# Connect from the command line
$ mysql -usomeusername -p

# Execute  MySQL commands
mysql> SHOW DATABASES;

Will output something like:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| demo               |
| demo_development   |
| demo_test          |
| mysql              |
| test               |
+--------------------+
7 rows in set (0.13 sec)

(*)Note - I'm referring to data model storage, not any additional file-based storage your application may use which is managed by Rails and your application code.

Sign up to request clarification or add additional context in comments.

2 Comments

once I get to the second line (show databases) I get "Ignoring query to other database" as the result, not your output.. any ideas as to why?
See this post Is your commandline correct, in particular the -usomeusername part? That is literally -u and someusername stuck together.
1

if you've created db successfully, then you probably have such conf. in database.yml:

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: site_db
  pool: 5
  username: root
  password: secret
  socket: /var/run/mysqld/mysqld.sock

you can view your database (sitedb) with phpmyadmin or by terminal

Comments

0

Make sure you connect to mysql with the -u command, such as root, then it will list the databases. (supply what is in your database.yml file). I was simply connecting with no -u and didn't see anything. Most likely connected with my local user and I didn't have permissions to view the databases! oops!

Comments

0

based on my recent experience I think you misspelled command option when you entered mysql credentials. In my case I entered the following:

root@myserver# mysql -u root -opabc123

where -o and -p is command options and the rest: abc123 is password. here I accidentally entered -o command option which was the cause of disappearance of databases. (see the -o -one-database.)

Do not pass -o and try.

hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.