Do I need MySQL installed locally as well? - mysql

I have 2 ubuntu 11.04 servers on IPs 192.168.9.14 and 192.168.9.15. On .14 I have installed ruby + rails and on .15 I have installed mysql and the database for my site resides on .15
I now want to get rails 2.3.8 to connect from .14 to the database on .15. I have the following in my database.yml on .14.
production:
adapter: mysql
encoding: utf8
reconnect: false
database: gtt_production
pool: 5
username: root
password: admin
host: 192.168.9.15
port: 3306
I'm currently getting an error showing up in my rails log as follows:
Can't connect to MySQL server on 192.168.9.15.
I originally thought this was due to user privileges on the mysql server but I have now added that in and still getting the same problem.
So I then tried connecting via a terminal (command line) as follows:
mysql -h 192.168.9.15 -uroot -p
and I got a message saying :
The program 'mysql' can be found in the following packages:
* mysql-client-cor-5.1
Try: sudo apt-get install <selected package>
...And that's what makes me think I need a local copy of mysql even though the database completely resides on another server.
Please can someone confirm whether my 'guess' is correct or whether there is some 'clever' way of connecting to the mysql server on .15 without also installing mysql on .14.

you need the client - sudo apt-get install mysql The server is usually mysql-server
Did you install the mysql gem for ruby? Rails will need this in order to connect to the DB. Also make sure .15 can accept remote requests from .14.

Have you configured your mysql db to listen to external IP traffic? I dont think MySQL by default allows external connections, only localhost traffic. Check your my.cnf.

Related

ActiveRecord NoDatabaseError when attempting to connect to AWS MySQL instance

We previously had a successful connection from Rails to an AWS MySQL instance through ActiveRecord, but as of our latest push to Heroku, that connection was severed. We don't understand why, as we have all traffic enabled on the AWS RDS settings, and the credentials in our database.yml config file have not changed. The only big thing that changed in the push to Heroku was adding the dovenv gem and passing control over environment variables to that gem.
When booting up our rails server in any of our three environments (dev, test, production), we receive a NoDatabaseError from ActiveRecord:
ActiveRecord::NoDatabaseError - Unknown database '<database_name>.us-east-1.rds.amazonaws.com'
We can successfully connect to the database through MySQL CLI.
database.yml:
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: "<username>"
password: "<password>"
host: <database_name>.us-east-1.rds.amazonaws.com
port: <port>
development:
<<: *default
database: test
test:
<<: *default
database: test
production:
<<: *default
Relevant local dependencies:
activerecord (5.1.4)
mysql2 (0.4.10)
rails (5.1.4)
AWS security groups:
default (<group_id>) CIDR/IP - Inbound 0.0.0.0/0
default (<group_id>) CIDR/IP - Outbound 0.0.0.0/0
Publicly accessible: Yes
What we have tried:
Restarting MySQL server: $ brew services list $ brew services restart mysql
Viewing MySQL logs on AWS, which reveals the error: [Warning] IP address '<ip_address>' could not be resolved: Name or service not known
Testing in production, which when attempting to connect to the database, gives the error: ActiveRecord::StatementInvalid: Mysql2::Error: No database selected
Running rake db:create RAILS_ENV=development, which gives the error:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`<possibly_private_id>`.`us-east-1`.`rds`.`amazonaws`.`com` DEFAULT CHARACTER SET `utf8`' at line 1
Uninstalling mysql, /usr/local/var/mysql, and reinstalling mysql (through brew)
Checking ENV variables, of which there are none that relate to MySQL.
Confirmed that my mysql server is running by running mysql.server status
I have a theory that when installing the gem dotenv-rails, we may have lost some MySQL credentials that granted access to the server from our project's local directory... Again, we still can connect to the server through MySQL CLI so we know everything on AWS is fine and healthy. I also can't seem to log in to mysql as my local username, or root. Currently trying to figure that issue out.
Solved - turns out it was related to the modifications I made in my environment variable setup. In this section of the Rails Guides, it notes that specifically declaring "DATABASE_URL" as an environment variable will automatically set or override the database variable declared in your config/database.yml file.
In my case, the host variable (set in config/database.yml) was correctly set to MY_DATABASE_NAME.amazon.aws, but DATABASE_URL variable in my environment was silently setting the config file's database variable to the same MY_DATABASE_NAME.amazon.aws.
I do not have a database called 'MY_DATABASE_NAME.amazon.aws', which is why it was freaking out. Hope this helps someone else!

Running MySQL and Webrick at the same time Windows 7

Hopefully this is a simple problem to fix
I have installed the Ruby installer for windows, installed mysql 5.5 and have configured mysql to listen on port 3000
When I run the mysql server and then try to run webrick with rails s i get the error message
Only one usage of each socket is normally permitted bind(2)
So both servers are trying to run off the same port? I cannot use them both at the same time, if i kill the processes on localhost:3000 then both services are reset
is there a way of configuring these to both work at the same time?
EDIT
My config/database.yml file
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: library_development
pool: 5
username: root
password:
host: localhost
port: 3000
Has anyone come across this before, very frustrating
Use the -p flag to specify that webrick bind to a different port. Something like
rails server -p 3001 # Assuming you are starting via command prompt
That should allow both services to run simultaneously.

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.

How do I connect a mysql database file to a local ruby on rails application

I have a database file (name.sql) that was sent to me that I am supposed to connect to a rails app locally hosted on my mac, that I downloaded (forked?) from github. How do I set up my database.yml file to connect with the sql files.
You can't connect a Rails application directly to a SQL file. The Rails application gets its data from a database server and you import the contents of the SQL file into a database hosted by the server.
You can download a DMG archive which will install MySQL Community Server on your Mac from http://dev.mysql.com/downloads/mysql/#downloads
That download also includes a handy Preference Pane for starting and stopping the server.
Once you have MySQL up and running then you should set a password for the root user (i.e. the database system administrator) using
mysqladmin -u root password "secret"
—Obviously replace secret with the real password you want to use.
Then you can set up the database.yml file for the Rails application. For an application named app it would look like this:
development:
adapter: mysql
database: app_development
username: root
password: secret
host: localhost
test:
adapter: mysql
database: app_test
username: root
password: secret
host: localhost
production:
adapter: mysql
database: app_production
username: root
password: secret
host: localhost
Note that typically in production you'd create a separate limited privilege database user account for the Rails application to connect to MySQL with, but for development on your local machine the root account is fine.
After this step you can run rake db:create from the root of the Rails application within the Terminal. This command will create the app_development database in MySQL (rake db:create:all creates the test and production databases too). Finally, you can import your SQL file by entering the following command in the Terminal:
mysql -u root -p app_development < path/to/file/name.sql
You will be prompted for the MySQL root password. Replace path/to/file with the full path to the SQL file if it's not within the Terminal's current directory. For example, use ~/Desktop/name.sql if it's on your desktop.
Probably easiest: you need to run a database server on your mac. Then import your data into your database server.
Tutorials on installing rails on a mac will also talk about how to install the local database server and setting up the database.yml file

Rails 3.o MYSQL connection problem

I have installed RVM in my ubunut linux box and configured the Rails 3 app in that ... i can able to start app server... my problem is when i invoke http://localhost:3000 . i getting the follwing error
Mysql::Error (Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)):
I checked mysqld service is running well.
I checked my database.yml file .... the defined well
development:
adapter: mysql
encoding: utf8
reconnect: false
database: test_development
username: root
password: admin
socket: /var/run/mysqld/mysqld.sock
my installed mysql gem version is 2.8.1.... I really don't know what is the problem here....
Your mysql server might be started, it seems the socket leading to it isn't available at the path you're providing to it (/var/run/mysqld/mysqld.sock).
You must change this socket path to the appropriate one in your machine.
The best way to find it is the following in a console : sudo find / -name mysqld.sock
Then you'll get the path and you'll just have to change it in your configuration file.