I am having trouble connecting to a MySQL DB on a remote server via Ruby and the DBI gem.
For this example, the DB host’s address is 000.00.00.000, and the name of the DB is testdb.
Should username be my personal username I SSH into the server with or a specific MySQL username?
Should password be my personal password I SSH with or the MySQL password I use once I’m on?
Do I need to specify the port or anything else along with the IP address?
Here is the code I’m currently trying to use, which I found on this tutorial
require 'rubygems'
require 'mysql'
require 'dbi'
#connect to mysql database
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:testdb:000.00.00.000",
"username", "password")
# get server version string and display it
row = dbh.select_one("SELECT VERSION()")
puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
When I run:
Error code: 2003
Error message: Can't connect to MySQL server on ... (60)
On SSH I am currently getting into MySQL with this command:
mysql -u root testdb -p
I have the password for this and that functions in my terminal.
What should I be putting in the DBI connect statement to connect correctly via Ruby?
Assuming that you run MySQL server on your local machine, try changing your connect line to: