Can't connect to MySQL server on 'myhost.com' (4) - mysql

I'm able to connect to the remote database from a MySQL client and using terminal as well. But when trying to access the database from the Rails app, I got this error:
Mysql2::Error (Can't connect to MySQL server on 'myhost.com' (4))
My database.yml has the following configuration for development:
adapter: mysql2
encoding: utf8
reconnect: false
host: myhost.com
port: 3306
database: the_database
pool: 5
username: myusername
password: mypassword
socket: /var/lib/mysql/mysql.sock
I have changed the name of the host to post this question, as the username, password and database.
Googling around, I could see different reasons for not connecting to the database, but none of them are similar to mine and with this code 4 (is that an error code?).
I'm new to Rails, so I have no clue what should I do next in order to fix it. I have double checked my configuration, even the socket. I've personally checked on the server side and this is the right path to the socket.
I'm not sure if it's relevant, but this user has read only access to this database.
I'm using Rails 3.2.8 and Ruby 1.8.7.

I was having the same headache error (The error is "Can't connect to MySQL Server (4)) for about 3 weeks, finally i discovered that my problem was based on a firewall rule.
The solution was to ask to my hosting provider to open the port 3306 but in the OUT way.
I was using CPanel and the main point here was that i does not have any kind of control to manage the CPanel firewall.
DonĀ“t try to expect as this is the big solution, because there are many things to be considered in configuration, but hey! i finally got it and it was all about the firewall of my provider.
Hope this help for someone.

You are using both a host which can be a remote host or the localhost but you also use socket which is a path to a UNIX Socket thats located in your servers filesystem. The socket is the prefered way to connect to your local database. If yo dont know the path tpo your mysql socket you can look it up in your mysql config file thats located in /etc/mysql/my.cnf in most cases.
If you want to connect to a remote host you should check if you can ping it and if you can you should try telneting the port like this:
telnet <hostname> 3306
Which will allow you to check if theres a deamon listening on the port your trying to connect to. If you cant connect to the server and you control the server there might be some firewall rules restricting access. Its definitfly not a mysql restricting problem because host restrictions causes other error messages.

Related

Can't connect to digital ocean LEMP stack MySql via Workbench (or any gui) but CAN connect via terminal mysql cmd

I have tried suggestions in what seems like every post about this topic and none of them have worked for me, so I'm posting my own in case anyone spots anything that I missed.
I'm migrating a wordpress site into a LEMP (ubuntu, apache) stack instance on Digital Ocean, and I need temporary access to the database via MySQL Workbench or something similar on my local machine. I'm planning to turn it off once the migration is done. However I am having a very weird issue I can't figure out the cause for.
If I attempt to sign into the sql instance using
mysql -u user -h my.ip.address -p
I can get access from my local machine just fine.
I can also SSH into the server itself via terminal just fine, I have it set up in my ssh config file using the same ip address and the same user and ssh key i'm attempting to use for my workbench connection.
However doing the same via an SSH connection on workbench I constantly get the error:
Authentication failed, access denied.
Failed to connect to MySql at my.ip.address:3306 through ssh tunnel at user#127.0.0.1 with user mysqluser
Things I've done to attempt to rectify this:
set bind-address in mysql config to 0.0.0.0 on the server and restarted server's mysql
set bind-address in mysql config to my.ip.address on the server and restarted
allowed tcp connection
allowed connection via ufw firewall to 3306 and 33060 ports
created a mysql user with all privileges granted for 'localhost', '%', and '0.0.0.0' hosts
tried sshing in as root user
tried using the root user for the mysql user
I am probably missing some things on that list as I feel like I've tried a million things already!
I even have a different server on a LAMP stack (not LEMP) that I set up a few days ago and was able to gain access to the mysql db on workbench by following the same steps, and even attempted backtracing and copying what I did on the LAMP server, but no go.
I checked to see the netstats of the mysql instance and match the port and the ip and that didn't work, I made sure the port of the SSH was correct, nothing is working.
Does anyone have any tips? I've been using all of the "allow remote mysql connection on digital ocean" articles and questions i can find but nothing is working.
Here is my Workbench connection config at the moment but i have tried what feels like every iteration of it i could think of (re users, passwords, IP addresses, and ports)
SSH Hostname: my.ip.address
SSH Username: user
SSH Password: user-password (also tried empty, since i have a key)
SSH Key File: same key file i use to ssh on terminal
Mysql Hostname: 127.0.0.1 (also tried my.ip.address)
MySql Server Port: 3306 (also tried 33060)
Username: MySqlUser
Password: MySqlUser-Password (works when connecting to mysql via terminal)

Puphpet MySQL Workbench Connection in Vagrant on Cygwin

I am new to programming, and do not have much idea about ports/forwarding/connection etc.
Installed Vagrant, MySQL Workbench, and Cygwin on Windows 8.1, then
Puphpet on Cygwin and created Virtual Machine, for learning.
But, unable to connect to MySQL on Virtual Machine (MySQL Server is on, on Virtual Machine) from Workbench in Windows.
I did not do any change in vagrantfile or config.yaml.
Tried Out:
Changed to bind-address = 0.0.0.0 in my.conf, but no use.
Commented out # bind-address = 127.0.0.1 in my.conf, again
no use.
Note: (I DELETED id_rsa file when it was not properly doing vagrant up
second time. So, gave full path to id_rsa.ppk, for connecting)
Connection Method: Standard (TCP/IP over SSH)
Parameters:
Hostname: 192.168.56.101:8957
SSH Username: vagrant
SSH Password: vagrant
SSH Key File: c:\cygwin64\home\tomy\puphpet\sutfva\puphpet\files\dot\ssh\id_rsa.ppk
MySQl Hostname: 127.0.0.1
Port: 3306
Username: dbuser
Password: 123
Error message:
IO Error [Errno 10061] No Connection could be made because the target
machine actively refused it. Please refer to logs for details
The answer at Vagrant MySQL Access is not clear to me. How to configure port forwarding?
Can you please guide me in simple words, how I can establish connection? Also, how to disconnect, and any security issue.
Thank you very much Juan Treminio and Brian Morton! Both of them gave valuable tips and inputs for me to come to track! In addition, immense thanks to Juan Treminio for his https://puphpet.com which is a huge help to persons like me.
COMPLETE Connection Details:
Choose
Standard TCP/IP over SSH
Parameters (Mostly default values since I did not change it)
SSH Hostname: 192.168.56.101
SSH Username: vagrant
SSH Password: vagrant
SSH Key File: C:\cygwin64\home\tomy\puphpet\sutfva\puphpet\files\dot\ssh\id_rsa
MySQL Hostname: 127.0.0.1
MySQl Server Port: 3306
Username: root
Password: 123
Note: Remember to note down your connection parameters
Hostname: 192.168.56.101:8957
You shouldn't need the port here, you should probably just need to ender 192.168.56.101
To verify which port SQL Server is listening on, you can ssh into the box and run
netstat -lnutp
This will output a list of the currently open and listening ports on the box. If you do not see SQL Server in the list, then it is not currently running and the service will need to be started.

"Access denied for user 'username'#'localhost' (using password: YES)" when deploying Rails with Capistrano

I am standing up a new Ubuntu server running MySQL. I have Capistrano set up on my development server and am trying to deploy:cold after running deploy:setup. After the deploy script tries to run
executing "cd /home/adm1n/www/knowit/releases/20121112152400 && bundle exec rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile"
I keep getting this message:
Rake Aborted!
Access denied for user 'specialusername'#'localhost' (using password: YES)
Tasks: TOP => environment
I have 'specialusername' created in my database on the mysql server for both localhost and %.
I removed the empty user ' ' # localhost row in the user database.
I have added the IP address of the server to my my.cnf file and commented out the 127.0.0.1 line.
Here is my deploy.rb file for production:
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: mydatabasename
username: specialusername
password: crazypassword
socket: /tmp/mysql.sock
pool: 5
timeout: 5000
I think that I may not be understanding how this works. On my Site5 server I have never had to specify which host I was accessing mysql from. But in all my reading it seems that I must specify a specific user#hostname. This makes it a little difficult if I am deploying from many different locations around the country as I travel. OR do I merely need to use the hostname of my laptop regardless of my current IP address? Thanks for any insight and solutions you can provide. I have not found any articles that have provided me precisely what I need to fix this issue.
There are two ways to connect to a MySQL server.
First, using a UNIX socket, such as /tmp/mysql.sock, in which case there is no "host", so authentication uses username#localhost. This will work only with connection from the same machine.
Second, using a TCP/IP connection. In this case, the server listen to a specific port on the box running the database, and the way to connect to it is to provide a host + port number. The port number can be optional (the default will be used), and host can be given by hostname or by ip address. This allows to connect locally or remotely.
Note: I have no way to actually verify this (no access to your server), so below is just a possible explanation ...
In mysql -u knowitdbadmin -p -h192.168.0.50, a host (-h) is given, so TCP/IP is used, causing authentication to use username#hostname or username#% rules, but this will not use username#localhost.
In mysql -u knowitdbadmin -p, no host (-h) is given, so the default UNIX socket is used, causing authentication to use username#localhost (and therefore fails).
If you want to disable access using a UNIX socket (/tmp/mysql.sock) and username#localhost, and always use a TCP/IP connection, do it all the way:
* remove grants to username#localhost (done)
* always specify a host/IP (+ port) in configuration files from your application.
I think the problem is with:
socket: /tmp/mysql.sock
try using a hostname + port number instead.
As a side note, if you somehow have SQL access to the database itself, starting with 5.5, a new table performance_schema.host_cache will show you all the reasons a connection is failing, which saves a lot of time when troubleshooting things like this.

HeidiSQL connecting to mysql server - Lost connection...server at reading initial

New to connecting to databases and have no clue to where to begin to troubleshoot -
I am currently using HeidiSQL to connect to an external Mysql Database Server hosted on the Amazon Web Service - EC2.
I am able to access the database using HeidiSQL with the settings:
MySQL (SSH tunnel)
Hostname/ip: 127.0.0.1
user: (user)
password: (pw)
port 3306
in SSH tunnel tab:
plink.exe location is set to putty.exe
sshhost + port: (my host ip) port 22
private key file: (is set)
local port: 3307
Using this configuration I AM able to log into my database.
My problem is when I am trying to do it using my own putty commands using the port forwarding:
In Putty:
I set SSH>tunnels - source port: 3307, destination (my host ip):3306
Local and Auto.
I connect using my passwords and hostip
Back to HeidiSQL, I put my settings to:
MYSL (TCP/IP)
hostname: localhost
user: (user)
password(pw)
port 3307
and try to connect. I get the following error:
SL ERROR (2013): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
From what I have read, this means my ssh tunnel is working but something else is wrong. I am now at a loss on why this doesn't work. Am i not setting something else up properly? Firewalling myself? My ultimate goal is to hook up another application to communicate with the database using ODBC orJDBC using the same port forwarding idea. Are there logs to check somewhere?
You must set
AllowTcpForwarding yes
PermitTunnel yes
in /etc/ssh/sshd_config
It seem's like your're trying to do this:
http://realprogrammers.com/how_to/set_up_an_ssh_tunnel_with_putty.html
It should work. I assume the ssh server and mysql server are running on the remote machine.
In Putty: I set SSH>tunnels - source port: 3307, destination (my host ip):3306 Local and Auto. I connect using my passwords and hostip
It should be: set SSH>tunnels - source port: 3307, destination 127.0.0.1:3306 Local and Auto.
heidisql: MYSL (TCP/IP) hostname: localhost user: (user) password(pw) port 3307
Notice that if you have a MySQL service running on your local machine, on the port your trying to listen (3307 ) you'll get a 2013 error, even if the local service is down! In that case, use any arbitrary unused port.
Also, if the remote mysql server has the user table (mysql database) restricted to certain hosts this won't work. Unless you find out how the server recognize himself (computer name) and add him on the user table or simply you use the '%' wildcard.
Follow below steps it solved my issue while connecting to Live server:
Find your IP (Google - what is my ip).
Go to C-Panel of your Live Server.
Go to Remote MySQL in C-Panel.
Add IP address that you got from Step 1 and save it.
Now try to connect through Heidi SQL.
Please let me know if it helped.

can cakePHP connect to remote database?

I am setting up CakePHP for the first time in order to develop the server side of the website I'm building. I already have a database set up on a remote server. So I want CakePHP to connect to that server. That's not working. I get a notice "Cake is NOT able to connect to the database."
I've tried changing the settings in cake\app\database.php:
persistent: true or false
host: with or without prefix 'http://'
But none of that helps. All the examples I've seen have server set to 'localhost'.
Can anyone tell me if cakePHP can connect to a remote db? Or am I completely missing the point and should I be using the mysql server included in my WAMP set?
You have 2 options
Option 1
Your webhost needs to allow connections to the mysql instance from outside the local network.
Check the manuals for the webhost if you're allowed to change the mysql configuration yourself you may be allowed to accept incoming connections.
Your method only works when the host accepts incoming connections on for example port 3306 which is the default mysql port.
Option 2
If you have SSH access to the remote host, you're able to set up a tunnel.
Which would make you connect to the Mysql Instance though your SSH connection on a local port.
One way you could do this is by using PuTTY.
How to set up a MySQL tunnel in PuTTY
Initiate the connection and keep it alive.
Use the host 127.0.0.1 and port 3306 in your CakePHP Database config.
Username & Password would be your MySQL User Credentials