Connecting Ruby and MySQL - 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

Related

Rails no database name in connection

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.

localhost:3000 database adapter

If my localhost:3000 page is showing "Database adapter postgresql", does this mean postgresql is running and not mysql?
Background: I originally installed postgresql and later installed mysql.
My database.yml file currently contains the following so I'm not sure where I'm going wrong here.
development:
adapter: mysql2
host: localhost
database: projects
username: root
password:
pool: 5
timeout: 5000
1: add gem 'mysql2' in your gemfile
2: run bundle install
3: mysql -u root # for do login and create db
4: mysql> create Database projects;
Hope this help you !!!

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.

Gem::LoadError Specified 'mysql2' for database adapter

I am using ruby and rails in window 7. I am create a basic blog application which is given by ruby and rails site
I have create db connection with mysql like this
development:
adapter: mysql2
encoding: utf8
database: message
username: root
password:
pool: 5
socket: /tmp/mysql.sock
timeout: 5000
Now i am getting error. I have search but not get so many ans but that is not related to window :(
Please help me, how can create connection with db
Gem::LoadError
Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile.
Tthis

I cannot connect mysql database server with rails project? (Windows 7)

I am very new in rails and using windows 7. In my windows machine i ve installed ruby 1.93, rails 3.2, Mysql 5.1 properly (also Install the MYSQL driver using this command "gem install mysql2"). I also put "libmySQL.dll" file into ruby/bin directory. I can create rails project and can run webrick server successfully..database server also get connected(using sqlite). But when i use mysql using "rails new app_name -d mysql" this command....mysql server is not get connected with rails project.
In my database.yml I have:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: untitled5_development
pool: 5
username: root
password:
host: localhost
Sorry for my bad english.
Thanks everyone
Hi you have to set the password for the root user. Also note that older versions of ruby on rails would expect you to comment the database name. If you have not yet created the db.