How to connect to Mysql Server inside VirtualBox Vagrant? - mysql

I mounted a new VirtualBox Machine with Vagrant, and inside that VM I installed Mysql Server. How can I connect to that server outside the vm? I already forward the port 3306 of the Vagrantfile , but when I try to connect to the mysql server, it`s resposts with the error:
'reading initial communication packet'
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

Make sure MySQL binds to 0.0.0.0 and not 127.0.0.1 or it will not be accessible from outside the machine
You can ensure this by editing the /etc/mysql/my.conf file and looking for the bind-address item--you want it to look like bind-address = 0.0.0.0. Then save this and restart MySQL:
sudo service mysql restart
If you are doing this on a production server, you want to be aware of the security implications, discussed here: https://serverfault.com/questions/257513/how-bad-is-setting-mysqls-bind-address-to-0-0-0-0

Log in to your box with ssh vagrant#127.0.0.1 -p 2222 (password vagrant)
Then: sudo nano /etc/mysql/my.cnf and comment out the following lines with #
#skip-external-locking
#bind-address
save it & exit
then: sudo service mysql restart
Then you can connect through SSH to your MySQL server.

I came across this issue recently. I used PuPHPet to generate a config.
To connect to MySQL through SSH, the "vagrant" password was not working for me, instead I had to authenticate through the SSH key file.
To connect with MySQL Workbench
Connection method
Standard TCP/IP over SSH
SSH
Hostname: 127.0.0.1:2222 (forwarded SSH port)
Username: vagrant
Password: (do not use)
SSH Key File: C:\vagrantpath\puphpet\files\dot\ssh\insecure_private_key
(Locate your insercure_private_key)
MySQL
Server Port: 3306
username: (root, or username)
password: (password)
Test the connection.

For anyone trying to do this using mysql workbench or sequel pro these are the inputs:
Mysql Host: 192.168.56.101 (or ip that you choose for it)
username: root (or mysql username u created)
password: **** (your mysql password)
database: optional
port: optional (unless you chose another port, defaults to 3306)
ssh host: 192.168.56.101 (or ip that you choose for this vm, like above)
ssh user: vagrant (vagrants default username)
ssh password: vagrant (vagrants default password)
ssh port: optional (unless you chose another)
source: https://coderwall.com/p/yzwqvg

Well, since neither of the given replies helped me, I had to look more, and found solution in this article.
And the answer in a nutshell is the following:
Connecting to MySQL using MySQL Workbench
Connection Method: Standard TCP/IP over SSH
SSH Hostname: <Local VM IP Address (set in PuPHPet)>
SSH Username: vagrant (the default username)
SSH Password: vagrant (the default password)
MySQL Hostname: 127.0.0.1
MySQL Server Port: 3306
Username: root
Password: <MySQL Root Password (set in PuPHPet)>
Using given approach I was able to connect to mysql database in vagrant from host Ubuntu machine using MySQL Workbench and also using Valentina Studio.

Here are the steps that worked for me after logging into the box:
Locate MySQL configuration file:
$ mysql --help | grep -A 1 "Default options"
Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
On Ubuntu 16, the path is typically /etc/mysql/mysql.conf.d/mysqld.cnf
Change configuration file for bind-address:
If it exists, change the value as follows. If it doesn't exist, add it anywhere in the [mysqld] section.
bind-address = 0.0.0.0
Save your changes to the configuration file and restart the MySQL service.
service mysql restart
Create / Grant access to database user:
Connect to the MySQL database as the root user and run the following SQL commands:
mysql> CREATE USER 'username'#'%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON mydb.* TO 'username'#'%';

This worked for me: Connect to MySQL in Vagrant
username: vagrant password: vagrant
sudo apt-get update sudo apt-get install build-essential zlib1g-dev
git-core sqlite3 libsqlite3-dev sudo aptitude install mysql-server
mysql-client
sudo nano /etc/mysql/my.cnf change: bind-address = 0.0.0.0
mysql -u root -p
use mysql GRANT ALL ON *.* to root#'33.33.33.1' IDENTIFIED BY
'jarvis'; FLUSH PRIVILEGES; exit
sudo /etc/init.d/mysql restart
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant::Config.run do |config|
config.vm.box = "lucid32"
config.vm.box_url = "http://files.vagrantup.com/lucid32.box"
#config.vm.boot_mode = :gui
# Assign this VM to a host-only network IP, allowing you to access
it # via the IP. Host-only networks can talk to the host machine as
well as # any other machines on the same network, but cannot be
accessed (through this # network interface) by any external
networks. # config.vm.network :hostonly, "192.168.33.10"
# Assign this VM to a bridged network, allowing you to connect
directly to a # network using the host's network device. This makes
the VM appear as another # physical device on your network. #
config.vm.network :bridged
# Forward a port from the guest to the host, which allows for
outside # computers to access the VM, whereas host only networking
does not. # config.vm.forward_port 80, 8080
config.vm.forward_port 3306, 3306
config.vm.network :hostonly, "33.33.33.10"
end

Related

Mysql: Connection refused ERROR: 111

I can't connect to my server when I use a remote IP address.
Localhost is working fine.
Some info:
OS: Ubuntu 16.04
Database: MariaDB
Port 3306 is open
User has remote (%) access.
I've changed bind-address in 50-server.conf to bind-address = 0.0.0.0 and I tried commenting it out. none of them worked. Also tried putting bind-address = 0.0.0.0in my.conf under a [mysqld] grouptag. After every change I restarted Mysql and tried to connect with this command mrsql -u root -h xxx.xxx.xxx.xxx -p. It works fine for localhost, but does not work when I try my servers IP.
How do I solve this problem?
After days of surfing and trying all the solutions given in stackoverflow, I finally managed to establish connection to mysql or Mariadb remotely.
Here is the solution for accessing mysql db from a remote machine:
Firstly, In your /etc/my.cnf or whatever location my.cnf is located in your machine:
bind-address= THE_HOST_IP_ON_WHICH_MYSQLDB_IS_INSTALLED
# skip-networking ( This will allow TCP/IP connection)
Secondly, Make sure there is no ip's in /etc/hosts.deny ( This can be root cause for connection in many cases)
> Important:
> 1. restart mysqld = systemctl restart mysqld ( Ubuntu might have diff cmd)
> 2. check status = systemctl status mysqld ( Ubuntu might have diff cmd)
> 3. check port and ip on which mysqld is running = netstat -tlnp It should be running on THE_HOST_IP_ON_WHICH_MYSQLDB_IS_INSTALLED: 3306 ( Ubuntu might have diff cmd)
Thirdly, Create a user with ALL privileges like :
> GRANT ALL ON db_name.* TO username#THE_HOST_IP_ON_WHICH_MYSQLDB_IS_INSTALLED IDENTIFIED BY 'password';
> FLUSH PRIVILEGES;
LASTLY, if using workbench or any other client to establish connection. Enter values as:
> HOSTNAME= THE_HOST_IP_ON_WHICH_MYSQLDB_IS_INSTALLED
>username=username
I hope this helps any of my fellow users!

mysql not connect from remove pc installed on Amazon EC2

I have just created one instance on Amazon EC2 for CentOs, and installed mysql on it with root user (password is blank). Then I had created another user for me to connect this instance from remote pc (my local pc). for that I had run following command on terminal one by one.
CREATE USER 'demouser'#'%' IDENTIFIED BY 'demopassword';
GRANT ALL PRIVILEGES ON *.* to 'demouser'#'%';
FLUSH PRIVILEGES;
Then I wrote following line in /etc/my.cnf under [mysqld] section
bind-address=0.0.0.0
Then restarted mysql with following command
sudo /sbin/service mysqld restart
Still, It is not allowed to connect that instance from my local pc. I don't understand what is the problem? I had checked and confirmed that rules on Amazon Security Groups are set properly. (e.g. port 3306 & 22 set to 0.0.0.0 ip address, means any ip address can connect using both ports).
Can someone saw me the mistake of mine?
Slowed
I ran following command to update ipaddress in centos terminal;
sudo iptables -I INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
and It's getting connected... !!!!

Vagrant: open mysql connection to host

I'd like to use MySql Workbench to browse my database in Vagrant from host machine.
So far I just commented the line bind-address = 127.0.0.1 in my.cnf and all was ok, but recently I connected to a new wi-fi and things seemed to change, I had to change guest ip address from 192.168.0.200 to 192.168.1.200 and I can't open mysql connection from host.
I tryed also to add bind-address = 10.0.2.2 guessing (not sure) this was my host ip but still the same.
What do I need to do?
Thank you
Define a static ip for your box like in this example:
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "10.0.0.6"
In Mysql Workbench create a new connection over SSH:
Connection Method: Standard TCP/IP over SSH
SSH Hostname: 10.0.0.6 (like above)
SSH Username: vagrant
SSH password: vagrant
MySQL Hostname: 127.0.0.1
MySQL Username: root
MySQL Password: root
SSH Host Key identification:
Instead of a password you can use the box specific Private Key. You can get the path by executing vagrant ssh-config.

can't access fortrabbit mysql db through terminal (ssh)

I can't access mysql through terminal on fortrabbit. I follow all steps but it is rejecting my password. However I can regullary login through ssh and edit my application. Anyone had that issue ? Thanks.
I have solved this in the past using a SSH tunnel. You open an SSH tunnel to the server, and then you connect to the MySQL server there from the endpoint of that tunnel. As such, to MySQL you appear to be connecting locally.
From the terminal:
First you need to open the tunnel, you can do it like this:
ssh -N -L8889:127.0.0.1:3306 username#your.fortrabbit.domain.com &
This opens port 8889, then opens a tunnel to your.fortrabbit.domain.com, then forwards that local port through the tunnel to the IP 127.0.0.1 and port 3306 relative to the server at your.fortrabbit.domain.com.
The options in more detail:
-N: Do not execute a remote command.
-L: Specifies the ports (local and remote).
8889: Your local port that is being forwarded.
127.0.0.1: the remote IP to which you're forwarding, relative to the server which ssh is connecting to
3306: the remote port to which you're forwarding.
username#your.fortrabbit.domain.com: Your username and domain with fortrabbit.
Now you're ready to open the connection. In the same terminal, use the following command:
mysql -h 127.0.0.1 -P 8889 -u mysql-username -p
port 8889 is now being forwarded to the port and IP of your MySQL server on the fortrabbit side, so just replace mysql-username with your username on the mysql server, and you're connected!
From a GUI:
You mentioned in your comments that you're using Ubuntu, so install MySQL Workbench from the Software Centre or here, create a New Connection and select the connection type as "Standard TCP/IP over SSH".
You will need to configure the following:
SSH Hostname: the hostname or IP of your ssh account with fortrabbit
SSH Username: your username with them
SSH Password: your password with them
SSH Keyfile: If you use keys for authentication, select the private one here.
MySQL Hostname: 127.0.0.1 (because it's local to the endpoint of your tunnel.
MySQL Server Port: normally "3306".
Username: The username for the DB
Password: The password for the DB
Default Schema: Whatever should be the default schema for this DB (can be left blank).
That should then connect from wherever you are!

ERROR 2003 (HY000): Can't connect to MySQL server (111)

This question is related to the following questions:
Can't connect to MySQL server error 111
Trying to connect to remote MySQL host (error 2003)
I am configuring a new MySQL (5.1) server on my local machine. I need to provide remote access to the database. I did the following steps:
Comment bind-address in my.cnf:
# bind-address = 192.168.1.3
Grant privileges:
GRANT ALL PRIVILEGES ON *.* TO 'nickruiz'#'%' IDENTIFIED BY PASSWORD 'xxxx';
Set port forwarding on router (TCP and UDP, port 3306, 192.168.1.3)
Configure iptables for firewall
sudo iptables -I INPUT -p udp --dport 3306 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 3306 --syn -j ACCEPT
sudo iptables-save
Restart mysql server sudo /etc/init.d/mysql restart
When testing, I get the following:
LAN:
mysql -h 192.168.1.3 -u nickruiz -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 95
Server version: 5.1.63-0ubuntu0.11.04.1 (Ubuntu)
Remote:
mysql -h 1xx.xx.4.136 -u nickruiz -p
ERROR 2003 (HY000): Can't connect to MySQL server on '1xx.xx.4.136' (111)
Clearly there's something wrong that's preventing me from being able to use my global IP address.
Notes:
I've tried testing the remote connection on the same machine and also
via SSH from a remote machine.
I'm not sure if my ISP has given me a static IP.
Any ideas?
Update:
telnet doesn't seem to be working.
telnet 192.168.1.3 3306
Trying 192.168.1.3...
Connected to 192.168.1.3.
Escape character is '^]'.
E
5.1.63-0ubuntu0.11.04.1,0g8!:#pX;]DyY0#\)SIConnection closed by foreign host.
Please check your listenning ports with :
netstat -nat |grep :3306
If it show
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
Thats is ok for your remote connection.
But in this case i think you have
tcp 0 192.168.1.3:3306 0.0.0.0:* LISTEN
Thats is ok for your remote connection.
You should also check your firewall (iptables if you centos/redhat)
services iptables stop
for testing or use :
iptables -A input -p tcp -i eth0 --dport 3306 -m state NEW,ESTABLISHED -j ACCEPT
iptables -A output -p tcp -i eth0 --sport 3306 -m state NEW,ESTABLISHED -j ACCEPT
And another thing to check your grant permission for remote connection :
GRANT ALL ON *.* TO remoteUser#'remoteIpadress' IDENTIFIED BY 'my_password';
errno 111 is ECONNREFUSED, I suppose something is wrong with the router's DNAT.
It is also possible that your ISP is filtering that port.
Check that your remote host (i.e. the web hosting server you're trying to connect FROM) allows OUTGOING traffic on port 3306.
I saw the (100) error in this situation. I could connect from my PC/Mac, but not from my website. The MySQL instance was accessible via the internet, but my hosting company wasn't allowing my website to connect to the database on port 3306.
Once I asked my hosting company to open my web hosting account up to outgoing traffic on port 3306, my website could connect to my remote database.
/etc/mysql$ sudo nano my.cnf
Relevant portion that works for me:
#skip-networking
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = MY_IP
MY_IP can be found using ifconfig or curl -L whatismyip.org |grep blue.
Restart mysql to ensure the new config is loaded:
/etc/mysql$ sudo service mysql restart
I had the same problem trying to connect to a remote mysql db.
I fixed it by opening the firewall on the db server to allow traffic through:
sudo ufw allow mysql
if the system you use is CentOS/RedHat, and rpm is the way you install MySQL, there is no my.cnf in /etc/ folder, you could use:
#whereis mysql
#cd /usr/share/mysql/
cp -f /usr/share/mysql/my-medium.cnf /etc/my.cnf
I have got a same question like you, I use wireshark to capture my sent TCP packets, I found when I use mysql bin to connect the remote host, it connects remote's 3307 port, that's my falut in /etc/mysql/my.cnf, 3307 is another project mysql port, but I change that config in my.cnf [client] part, when I use -P option to specify 3306 port, it's OK.
i set my bind-address correctly as above but forgot to restart the mysql server (or reboot) :) face palm - so that's the source of this error for me!
Sometimes when you have special characters in password you need to wrap it in '' characters, so to connect to db you could use:
mysql -uUSER -p'pa$$w0rd'
I had the same error and this solution solved it.
I had this same error and I didn't understand but I realized that my modem was using the same port as mysql. Well, I stop apache2.service by sudo systemctl stop apache2.service and restarted the xammp, sudo /opt/lampp/lampp start
Just maybe, if you were not using a password for mysql yet you had, 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES), then you have to pass an empty string as the password
Not sure as cant see it in steps you mentioned.
Please try FLUSH PRIVILEGES [Reloads the privileges from the grant tables in the mysql database]:
flush privileges;
You need to execute it after GRANT
Hope this help!