Rails no database name in connection - mysql

Trying to run a rails app in prod on Ubuntu 16.04 and installed mysql Ver 14.14 Distrib 5.7.24, Ruby 2.5.3, and using the mysql2 gem. It works fine on my Mac 10.13. My Rails app can't seem to read the DB name in Ubuntu.
Can't find the DB name:
{:adapter=>"mysql2", :encoding=>"utf8", :database=>nil, :username=>"myUser", :password=>"secret", :host=>"127.0.0.1", :port=>3306}
Then adds 127.0.0.1 as the DB name:
{:adapter=>"mysql2", :encoding=>"utf8", :database=>"127.0.0.1", :username=>"myUser", :password=>"secret", :host=>"127.0.0.1", :port=>3306}
My database.yml
production:
adapter: mysql2
encoding: utf8
database: mydb
username: myUser
password: secret
host: 127.0.0.1
port: 3306
I've been pulling my hair out as to why I can't connect to the DB in my Ubuntu server. I can log into mysql through the CLI fine with the DB user fine.
I'm also seeing the same issue on an Ubuntu box with Vagrant so thats consistent.
Any help is extremely appreciative at this time.
I have no idea but putting it in this format works. I tried everything else, different DB name, etc to no avail:
production:
url: mysql2://user:pass#localhost/myDb
Thank you for your responses.

Your database.yml should look something like this:
development:
adapter: mysql2
encoding: unicode
pool: 5
username: mysql_user
password: your_password
host: localhost_or_your_host
database: db_name
Do change your environment to match your requirements. This will work for your development mode.

Related

In Rails 6, how do I add "protocol" into my db configs?

I have my MySql 5.7 db running on a virtual server, whose port 3306 is forwarded to my local . I can connect using this
> mysql -h localhost --protocol=tcp -u mouser mydb_test -p
Which would fail if the params “--protocol=tcp” weren’t present. As such, I’m trying to figure out how to add “protocol” into my database.yml config for Rails 6. I discovered the below is not the way to do it
test:
adapter: mysql2
encoding: utf8
host: localhost
database: mydb_test
pool: 5
username: mouser
password: mypass
protocol: tcp
variables:
sql_mode: NO_ENGINE_SUBSTITUTION
Since I get connection errors when attempting to connect to the db.

Ruby On Rails data migration from sqlite3 to mysql regarding to many to many relations

I have a sqlite3 database in my rails app which has a development.sqlite3 file. I want to convert the database to mysql.Here are the steps I take but still I have problems :
1.First I add and install gem 'seed_dump' to add the data into my seed.rb (By running rake db:seed:dump) because I really need to migrate my data .
2.Change the database.yml configuration to mysql setting.
development:
adapter: mysql2
encoding: utf8
database: MyDB
username: root
password: ****
production:
adapter: mysql2
encoding: utf8
database: MyDB
username: root
password: ****
3.Run rails db:create then rails db:schema:load.
4.Then loading the data from seed by running rake db:seed:dump
The problem is my many to many relations data (which has a table in db schema ) can't be imported in mysql from my seed.rb.
The thing I wanna know is that is there any other safe way to migrate my data from sqlite3 to mysql instead of writing them into seed.rb and then read them ?
As I was looking for another way to resolve the issue I've found what is declared here. I followed the steps facing to another error of mysql : Data is too long for column summary . To skip this error I turned off mysql strict mode like this in database.yml:
config/database.yml
development:
adapter: mysql2
encoding: utf8
database: myDB
username: root
password: ****
host: localhost
strict: false
production:
adapter: mysql2
encoding: utf8
database: myDB
username: root
password: ****
host: localhost
strict: false
This solution is more clear than using gem 'seed_dump' which has problems with has and belongs to many relations!

How do I access my mysql database in Rails?

Say I have a database.yml that looks like this:
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: mysql2
database: arthouse_development
username: root
password:
host: localhost
port: 3306
#socket: /tmp/mysql.sock
If this were postgres, all I need to do is type psql arthouse_development. What do I need to do here?
Also, it seems I have to type this everytime:
mysql -u root to just access mysql. Why is this? Can I configure this so that I don't have to do this everytime?
You can save this line as shell script.
mysql -uroot -ppassword arthouse_development

Cannot Connect to MySQL Database in Rails 4

I'm new to Rails and I'm going through a tutorial on Lynda.com, Rails 4 Essentials. I'm on a Windows 7 x64 machine and I'm trying to connect the MySQL database to the Rails app. I've successfully created the database and created a new user.
I had issues running the mysql2 gem and I could only get version 0.3.11 to work (it's on version 0.3.16 as of this post). I copied the libmysql.dll from the C:\Program Files\MySQL\MySQL Connector.C 6.1\lib to my C:\RailsInstaller\Ruby2.0.0\bin directory as the instructions stated.
Everything seems to be ok at this point.
I configured my database.yml file to match my database credentials (I had to create this from scratch, nothing was generated when I ran mysql2).
#config\database.yml
development:
adapter: mysql2
database: simple_cms_development
username: craig
password: password
host: 127.0.0.1
socket: /tmp/mysql.sock
test:
adapter: mysql2
database: simple_cms_test
username: craig
password: password
host: 127.0.0.1
socket: /tmp/mysql.sock
production:
adapter: mysql2
database: simple_cms_production
username: craig
password: password
host: 127.0.0.1
socket: /tmp/mysql.sock
I got to try to connect my database with MySQL using:
rake db:schema:dump
and I get this crap:
LoadError: cannot load such file -- mysql2/2.0/mysql2
I see a directory called mysql2/1.9 and there is a file called mysql.so in there but there is no 2.0.
Any ideas? Thanks!
The joys of developing on a windows machine :)
I suggest you check this question - it looks like the same problem.
Error "...cannot load such file -- mysql2/2.0/mysql2 (LoadError)". On Windows XP with Ruby 2.0.0
I managed to get passed this issue by starting fresh. I followed these set of videos to set up Ruby, MySQL, and Rails on my Windows 7 x64 machine.
http://youtu.be/C5S7vjN6GLc
Worked like a gem, I'm rockin' and rollin' now.

Connecting Ruby and MySQL

I am in windows 7 and I installed ruby 1.9.2p180 and MySQL 5.5.15 and mysql gem.
now how can I connect ruby to mysql ?
You can create a new rails app with mysql instead of the default sqlite by using:
rails new APPNAME -d mysql
Or the long form:
rails new APPNAME --database=mysql
You can then take a look at the generated file config/database.yml to see the settings used for mysql. You will need to set up your username, password and database in here. Don't forget that with mysql, you will need to create the database manually for each environment.
Sample config/database.yml:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: test_database
pool: 20
username: root
password: root
host: localhost
socket: /var/run/mysqld/mysqld.sock
Just install mysql2 gem and when you want to create a new project run
rails new APPNAME --database=mysql