connection refused error in ruby while connecting to mysql database - mysql

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.

Related

How to check if MySQL service is running with DBD::mysql

I am using the following DBD::mysql statement to connect to a MySQL database:
use DBI;
# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost",
"usr", "usr's password",
{'RaiseError' => 1});
Is there a way to check if MySQL service is running, before trying to connect to the database? What if the database is running on a remote server?
If you want a solution to check if the service is running without attempting to connect to it, you could use some Perl package to check the process table (works locally only), or check that the MySQL port (3306 by default) has a process listening to it.
I'm not sure what the purpose of this check is, because even if the service is running, the next thing you'll probably want to do is open a DB connection. Opening a DB connection is a quick and easy thing to do, and it has good error reporting if it doesn't work. So your intention to check that the service is running first is just unnecessary overhead.
I would just try to connect as you are doing. This is the most direct way of checking that the service is running, and it works both locally and remotely.
If there's an error, catch the error and interpret the error message. It'll be error 2002 (for localhost) or 2003 (for TCP/IP, whether it's the same host or a remote host).
These errors are mostly reliable. But there could be red herrings, for example if the service is running on a remote host, but your client host can't reach it because of firewalls or routing issues.
If you get an error 1045 (Access Denied), at least you know the service is running and you can reach it, the problem is only that your user & password are incorrect, or you tried to access a schema you don't have privilege to use.

Failed to connect to database: Error: Can't connect to MySQL server on 'xxxxxx.rds.amazonaws.com' (111)

I am trying to run this command locuscompare(in_fn1 = gwas_fn, in_fn2 = eqtl_fn, title1 = 'CAD GWAS', title2 = 'Coronary Artery eQTL') within R in ubuntu behind a corporate proxy
but having the following persistent issue for three days.
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on 'locuscompare-us-west-2a.xxxxxx.us-west-2.rds.amazonaws.com' (111)
Warning message:
In rm(config) : object 'config' not found
I have already tried the solutions mentioned in stack overflow, such as installing MySQL, assigning users' privilege, editing bind-address in configuration file, but still do not know how to run it smoothly.
I thought at first it was an issue related to the lacking of MySQL server and AWS account on my device. But, when I use it on my mac with home Wi-Fi, it is working like a charm without any installation and creation of MySQL server and AWS account. However, I still need to use this tool on my work PC because of input data privacy issue.
Does it mean I need to create an AWS account and adjust the network setting in it to access MySQL server on this URL 'locuscompare-us-west-2a.xxxxx.us-west-2.rds.amazonaws.com'? (Sorry that I am very new to AWS and MySQL.)
Please let me know how to run this tool behind a corporate proxy.

Connecting R to remote MySQL server - Can't Connect to server

Backstory, I would like to build shiny apps to give to some of our data collectors so they can review what has been collected. We currently house all of our data in a cloud based MySQL server. Ideally, I would like the shiny app to pull data directly from the MySQL server so it can be fully automated without any data pulls and up 24/7.
I have been trying to first just build the connection between R and MySQL using the RMySQL package and can't seem to get it working. I have set up a specific username/password for this connection that is read only(however I have also tried my regular username which has all privileges granted). This is the code I am running;
mydb=dbConnect(
MySQL(),
user='myuser',
password='mypass',
dbname='vgtg',
host='ipaddress',
port=3306,
)
Obviously the 'ipaddress' of the server has been changed for the sake of posting here but it is a generic looking address like
'192.168.1.1'
When I run the code above I get this error message;
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to MySQL server on
'ipaddress' (0)
I have tried looking for previous questions posted but none seem to be exactly this error message that I am receiving. It makes me think that for some reason RMySQL is looking locally for the server when it is actually a cloud based, remote, server.
Also, is there anything more I should set up server side to allow the connection? I do have a server admin to help out but I am not sure how familiar he is with R and likewise I am not particularly familiar with working with servers. He has opened port 3306 for me and is able to see my attempts to connect through the port.
Your syntax is correct with the exception of
port=3306,
You need to drop the comma. That said, the error you received is unrelated to the syntax.
Without knowing the details of your setup, it is hard to diagnose. Where does the MySQL DB reside? For example, if it is on an AWS RDS instance, then the host isn't a standard IP address, it is something like this
mydb.cm1abc2v4mod.us-west-1.rds.amazonaws.com
Assuming that the IP address you used is correct, then the problem is most likely on the server. You need to ensure that port 3306 is open to traffic. Otherwise, R will not be able to connect to the DB.

Outsystems: configuring a connection to access a MySQL database

I'm using Outsystems Service Studio to develop a web application. I need to configure a connection to access a local server database. I get "Connection String test failed: Unable to connect to any of the specified MySQL hosts." I just figured out I can't connect using "localhost", because the Outsystems server is not local, but I'm not able to find a solution. what is wrong? Other details:
MySQL Server is up and running
I selected MySQL in DBMS
Inserted my schema name
Inserted the username (with all privileges granted)
Inserted the user password
Tried both basic and advanced configuration. I inserted j"dbc:mysql://127.0.0.1:3306/mydb?user=outsystems2" as connection string parameters and I get "Connection String test failed: Keyword not supported.Parameter name: mysql://127.0.0.1:3306/mydb?user". I know this connection string can't work , but I'm not able to find a functioning one. I've read many guides about this configuration, but no solution was found. Thank you all for your time and help, feel free to ask for more details
Luciano,
Is your OutSystems environment on-premises or in the cloud? Either way, you need to make sure that this server is able to reach - it as connectivity - to your MySQL database server. Using localhost or 127.0.0.1 is pretty much the same thing as this is an address for the machine where the request is running, which is, in this case, the OutSystems server. Do you have the MySQL database on your local machine? This is not a good approach as you will need to have an address that won't change otherwise the connection won't be stable and you'd have to reconfigure it all the time.
Regards

Set up local database for use with Node.Js with Visual Studio

I've been trying to learn Node.js recently, and I now want to access a database, which I use this library for. Problem is that I want to create a database on my computer and connect to it from my node.js program, but I can't for my life figure out how this is done!
In my solution I added a SQL Server Database Project, and tried to connect using this following string:
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'Database1'
});
But I'm getting an exception saying error connecting: Error: connect ECONNREFUSED 127.0.0.1:3306, which is thrown by some timeout (which I figure is because of the database isn't visible or something?).
So my question is how do I set this up so I can work with data in Visual Studio, without having to rent a server to host my database?
I'm not even sure I'm using the right project-template for my database, but I cannot see any other type of it. From what I've read on the Internet I see that there usually is some other template you should use to create a database.
I really don't know what to do, so any suggestions are welcomed.
It looks like to be an issue with the port.
This might help you out:
Cannot connect to SQL Server with Node.js and Tedious