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.
By default, Rails uses SQLite3. The database files are stored in the /db directory in the root of your app. There should be a file called development.
You do this in the file database. yml, available in the library\config subdirectory of Rails Application you created.
Rails defaults to using a SQLite database when creating a new project, but you can always change it later.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With