Mind Dump, Tech And Life Blog
written by Ivan Alenko
published under license CC4-BY
posted at 18. Jun '23

Howto Set Up MariaDB For Rails Development

Install MariaDB

For Debian like distributions. RPM distros: dnf/yum/zypper search mariadb.

apt-get install libmariadb-dev mariadb-client mariadb-server

Type ‘y’ and press Enter.

We now installed MariaDB server, client and development headers. In Debian the server starts automatically and also it is enabled by default to start on system start. In OpenSuse both aren’t.

Install Rails Project Dependencies And Run

mysql2 gem depends on development headers.

bundle install
bin/rails s
=> Booting Puma
=> Rails 6.1.7.1 application starting in development 
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 6.0.2 (ruby 3.2.2-p53) ("Sunflower")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 103641
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop
Started GET "/" for 127.0.0.1 at 2023-06-18 01:33:22 +0200
  
ActiveRecord::ConnectionNotEstablished (Access denied for user 'root'@'localhost'):
  
activerecord (6.1.7.1) lib/active_record/connection_adapters/mysql2_adapter.rb:45:in `rescue in new_client'
activerecord (6.1.7.1) lib/active_record/connection_adapters/mysql2_adapter.rb:39:in `new_client'
activerecord (6.1.7.1) lib/active_record/connection_adapters/mysql2_adapter.rb:23:in `mysql2_connection'
activerecord (6.1.7.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `public_send'
activerecord (6.1.7.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `new_connection'
activerecord (6.1.7.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:926:in `checkout_new_connection'
activerecord (6.1.7.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:905:in `try_to_checkout_new_connection'
activerecord (6.1.7.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:866:in `acquire_connection'
activerecord (6.1.7.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:588:in `checkout'
activerecord (6.1.7.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:428:in `connection'
activerecord (6.1.7.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:1128:in `retrieve_connection'

We cannot use root user with an empty password anymore.

Create A New Superuser User

Switch to root and open SQL console:

mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 105
Server version: 10.11.3-MariaDB-1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB root has invalid password by default and can be connected only from system root user. I didn’t check how it works exactly. We will create another superuser for Rails development named damon.

MariaDB [(none)]> CREATE USER 'damon' IDENTIFIED BY 'Isabelle';
Query OK, 0 rows affected (0,004 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'damon'@'%';
Query OK, 0 rows affected (0,003 sec)

MariaDB [(none)]> SHOW GRANTS FOR damon;
+---------------------------------------------------------------------------------------------------------------+
| Grants for damon@%                                                                                            |
+---------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO `damon`@`%` IDENTIFIED BY PASSWORD '*068D8C834E2B45EEE62C30020D17BAE3BCB3D57F' |
+---------------------------------------------------------------------------------------------------------------+
1 row in set (0,000 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0,000 sec)

For some reason the restriction to localhost instead of % is not accepted. Anyways, the database runs only on localhost and only for development, so it is fine. But I will look into in in the future.

Update database.yml

default: &default
  <<: *default
  database: teddybear
  adapter: mysql2
  username: damon
  password: Isabelle
  #password:
  encoding: utf8
  #host: 127.0.0.1
  host: localhost
  port: 3306
  #socket: /run/mysqld/mysqld.sock

Run Migrations and Run Server

Now create the database and run migrations and seeds:

bin/rails db:create db:migrate db:seeds
bin/rails server
=> Booting Puma
=> Rails 6.1.7.1 application starting in development 
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 6.0.2 (ruby 3.2.2-p53) ("Sunflower")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 113001
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop

Go to localhost:3000 and you’ll see something which is not a page with an exception.

And that’s it.

Add Comment