简体   繁体   中英

connection refused error in ruby while connecting to mysql database

When I am trying to connect to Mysql database using ruby then I get "Connection to Data Source [2] failed: java.net.ConnectException: Connection refused" error. I am using RubyMine IDE and I have downloaded Mysql server and workbench and installed mysql gem. I am not sure if I need to use SSH to connect as I see an option in Rubymine to connect to the Mysql data source. Here is my code and the error message:

require 'mysql2'
begin
  con = Mysql2.new 'localhost', 'root', 'password'
  puts con.get_server_info
  rs = con.query 'SELECT VERSION()'
  puts rs.fetch_row
rescue Mysql2::Error => e
  puts e.errno
  puts e.error
ensure
  con.close if con
end

And the error message is:

Data Source [2]
Connection to Data Source [2] failed: java.net.ConnectException: Connection refused

Could someone help me connect to mysql data source using Ruby on rails and fetch tables and manipulate the data and write back to the database?

Thanks

You don't have to use SSH to connect to a database. That's something you can decide with your application.

You first need to be sure that you can connect to MySQL running on your machine (the MySQL server on localhost). Are you able to do that? Always make sure you can do that; that helps you figure out if the problem is just with connecting to the db or if it's in your code. (Read the RubyMine help for info on how to do that.)

If you can connect using the RubyMine database tool, then you next need to create the database using the rake task "db:create".

Then you're ready to run some code to connect to your db.

Also -- your request is way too broad. You're essentially asking for someone to tutor you on connecting to and working with a database. There are many tutorials out on the web that will step you through connecting to a database and using it. (You might start with the well-known tutorial by Michael Hartl. (A quick search will point you right to it.) If/when you run into a specific problem, come back, describe that specific issue, and then carry on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM