How do I access my mysql database in Rails? - mysql

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

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.

Cannot connect to mysql server Error: mysqld.sock' (38)

Hi I am working on ruby on rails project. after setting up all environment for the project but when I run localhost:3000 on my web browser it gives my this error
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (38)
Can someone help me how can I solve this problem?
Check if mysql is running
mysqladmin -u root -p status
Rails by default tries to connect to MySQL using a socket file. You probably want it to connect using a TCP connection.
Check your database.yml; it should look similar to this:
development:
adapter: mysql2
encoding: utf8
reconnect: true
host: localhost
database: {database name}
pool: 5
username: {username}
password: {password}
Notice the host parameter and the missing socket parameter.

Can't connect to MySQL server on '127.0.0.1'

I am trying to clone a repo and run it on my local machine. I don't have a DB created and I am trying to manually create my database.yml file (my first time doing this). I am not quite sure what i'm doing and keep getting errors about connecting to the MySql server. Here's my database.yml file:
development:
adapter: mysql
host: 127.0.0.1
database: db/app_development
username: root
password:
I also tried localhost instead of 127.0.0.1, both give me errors when trying to create the db by doing 'bundle exec rake db:create' ...i get this error:
Can't connect to MySQL server on '127.0.0.1' (61)
Couldn't create database for {"username"=>"root", "adapter"=>"mysql", "database"=>"db/app_development", "host"=>"127.0.0.1", "password"=>nil}, charset: utf8, collation: utf8_unicode_ci
I know i'm doing something wrong but I can't quite figure out what it is. Do I need to start the mysql server or something?
This is the output when I run mysqld_safe:
120111 20:35:39 mysqld_safe Logging to '/usr/local/var/mysql/Matthew-Bermans-MacBook-Air.local.err'.
120111 20:35:39 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
120111 20:35:41 mysqld_safe mysqld from pid file /usr/local/var/mysql/Matthew-Bermans-MacBook-Air.local.pid ended
Try:
development:
adapter: mysql
database: app_development
username: root
password:
socket: /var/lib/mysql/mysql.sock
Then do your rake db:create to create it from rails.
or
in mysql do create database db_name
and then the above code for :development will let you use it.
Update: Matthew first needs to get mySQL installed on his (Mac) machine.
I directed him to http://dev.mysql.com/downloads/mysql/
Explicitly mention your port: 3306 in your database.yml
If that doesn't work then executed
netstat -tupln
and check if MySQL is listening to port 3306 or not
First of all, try executing the following to check if you have a connection to mysql from command prompt (without Ruby on Rails).
mysql -u root
If cannot connect you probably specified and password. You will need to either remember it or reinstall MySQL. If you can, then create your database:
create database app_development;
Last but no the least, remove prefix 'db/' from database.yml. You don't need a full path:
database: app_development
Hope that helps!
If you don't mind what database software you use (I don't think you specified whether you needed MySQL or just wanted to get the repo to work), you might try using SQLite3 instead. SQLite3 doesn't run a server or use ports, so it might be easier for you to get setup.
Try imitating the rails default database.yml, which looks like:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
Then add gem 'sqlite3' to your Gemfile and run bundle install
Hope this helps!
It can also be solved by changing MySQL version. Had the same issue with some project which was developed using MySQL 5.5 but installed version in my mac was MySQL 5.7 so I had to downgrade my version and it worked for me.

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

Ruby on Rails 3 Can't connect to local MySQL server through socket '/tmp/mysql.sock' on OSX

I have a standard Rails3 environment, RVM 1.2.9, Rails 3.0.5, Ruby 1.9.2p180, MySQL2 Gem 0.2.7, mysql-5.5.10-osx10.6-x86_64
Error I get when running rake db:migrate to create database is:
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
config/database.yml has
development:
adapter: mysql2
host: localhost
username: root
password: xxxx
database: xxxx
sure it's something simple I'm missing.
First, to find your socket file:
mysqladmin variables | grep socket
For me, this gives:
| socket | /tmp/mysql.sock |
Then, add a line to your config/database.yml:
development:
adapter: mysql2
host: localhost
username: root
password: xxxx
database: xxxx
socket: /tmp/mysql.sock
Found it!
Change host: localhost in config/database.yml to host: 127.0.0.1 to make rails connect over TCP/IP instead of local socket.
development:
adapter: mysql2
host: 127.0.0.1
username: root
password: xxxx
database: xxxx
Your mysql server may not be running. Below explains how to start the server. This is an excerpt from the README file that comes with the mysql download.
After the installation, you can start up MySQL by running the following
commands in a terminal window. You must have administrator privileges
to perform this task.
If you have installed the Startup Item, use this command:
shell> sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
(ENTER YOUR PASSWORD, IF NECESSARY)
(PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL)
If you don't use the Startup Item, enter the following command sequence:
shell> cd /usr/local/mysql
shell> sudo ./bin/mysqld_safe
(ENTER YOUR PASSWORD, IF NECESSARY)
(PRESS CONTROL-Z)
shell> bg
(PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL)
These are options to fix this problem:
Option 1: change you host into 127.0.0.1
staging:
adapter: mysql2
host: 127.0.0.1
username: root
password: xxxx
database: xxxx
socket: your-location-socket
Option 2: It seems like you have 2 connections into you server MySql.
To find your socket file location do this:
mysqladmin variables | grep socket
for me gives:
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' exists!
or
mysql --help
I get this error because I installed XAMPP in my OS X Version 10.9.5 for PHP application. Choose one of the default socket location here.
I choose for default rails apps:
socket: /tmp/mysql.sock
For my PHP apps, I install XAMPP so I set my socket here:
socket: /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
OTHERS Socket Location in OS X
For MAMPP:
socket: /Applications/MAMP/tmp/mysql/mysql.sock
For Package Installer from MySQL:
socket: /tmp/mysql.sock
For MySQL Bundled with Mac OS X Server:
socket: /var/mysql/mysql.sock
For Ubuntu:
socket: /var/run/mysqld/mysql.sock
Option 3: If all those setting doesn't work you can remove your socket location:
staging:
# socket: /var/run/mysqld/mysql.sock
I hope this help you.
"/tmp/mysql.sock" will be created automatically when you start the MySQL server. So remember to do that before starting the rails server.
With my installation of MAMP on OSX the MySQL socket is located at /Applications/MAMP/tmp/mysql/mysql.sock. I used locate mysql.sock to find my MySQL socket location.
So my config/database.yml looks like:
development:
adapter: mysql2
host: localhost
username: root
password: xxxx
database: xxxx
socket: /Applications/MAMP/tmp/mysql/mysql.sock
If you are on Mac OSX,
The default location for the MySQL Unix socket is different on Mac OS X and Mac OS X Server depending on the installation type you chose
MySQL Unix Socket Locations on Mac OS X by Installation Type
Package Installer from MySQL ------------------/tmp/mysql.sock
Tarball from MySQL -------------------------------/tmp/mysql.sock
MySQL Bundled with Mac OS X Server -------/var/mysql/mysql.sock
So just change your database.yml in socket: /tmp/mysql.sock to point to the right place depending on what OS and installation type you are using
The default location for the MySQL socket on Mac OS X is /var/mysql/mysql.sock.
I have had the same problem, but none of the answers quite gave a step by step of what I needed to do. This error happens because your socket file has not been created yet. All you have to do is:
Start you mysql server, so your /tmp/mysql.sock is created, to do that you run: mysql server start
Once that is done, go to your app directory end edit the config/database.yml file and add/edit the socket: /tmp/mysql.sock entry
Run rake:dbmigrate once again and everything should workout fine
I found that the problem is that I only have a production environment. I do not have a development or test environment.
By adding 'RAILS_ENV=production' to give the command
bundle exec rake redmine:plugins:migrate RAILS_ENV=production
it worked
If you are running MYSQL through XAMPP:
Open XAMPP mysql configuration file (on OSX):
/Applications/XAMPP/etc/my.cnf
Copy the socket path:
socket = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
Open rails project's database configuration file:
myproject/config/database.yml
Add the socket config to the development database config:
-->
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: difiuri_falcioni
pool: 5
username: root
password:
host: localhost
socket: /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
Restart rails server
Enjoy :)
You have problem with like this: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
Ans: $ sudo service mysql start
On my machine mysqld service stopped that's why it was giving me the same problem.
1:- Go to terminal and type
sudo service mysqld restart
This will restart the mysqld service and create a new sock file on the required location.
If you are running MYSQL through XAMPP or LAMPP on Ubuntu or other Linux, try:
socket: /opt/lampp/var/mysql/mysql.sock