Linking MySQL Workbench to my Remote Server - mysql

I've just downloaded MySQL Workbench.
But I don't quite understand how to syn this with the databases on my remote server.
Work bench asks for "hostname" so I provided the hostname of my remote server. I designate port 3306.
I then provide a username. This is the username I use when I log into PhpAdmin -- should I be using a different one?
Then I provide a password, again the same one I use for PhpAdmin.
But this doesn't work.
Oddly, the error always tells me my user name is: username#current_network_im_using_to_access_the_internet
But this doesn't seem right -- on phpAdmin my user name says username#localhost.
I'm not quite sure what to do.
Can you help me?

MySQL treats logins as specific to the host they originate from. You can have a different password from your home machine than the one you use on the server itself, and you can have entirely different sets of permissions granted to the same username from different origin hosts.
On PHPMyadmin, the database is running on the same server as the web server, and therefore refers to itself as localhost, with IP 127.0.0.1. Your machine on which Workbench is installed must access MySQL with different credentials than your username#localhost. The server requires you to grant access to your username from any host you intend to connect from.
In PhpMyAdmin, you will need to grant access to your database from the remote host: (See also Pekka's answer for how to allow connections from any host)
GRANT ALL PRIVILEGES on dbname.* TO yourusername#your_remote_hostname IDENTIFIED BY 'yourpassword';
To see all the grants you currently have on localhost so that you can duplicate them for the remote host:
SHOW GRANTS FOR yourusername#localhost;
Additionally, the MySQL server needs to be setup to accept remote connections in the first place. This isn't always the case, especially on web hosting platforms. In the my.cnf file, the skip-networking line has to be removed or commented out. If there is no skip-networking line, you must comment out the line:
bind-address = 127.0.0.1
...then restart MySQL.

Your phpMyAdmin seems to run on the same server as the database itself.
Therefore, it can use username#localhost to connect to the server.
You would need to make mySQL accept connections from outside localhost by adding another user username#% (% meaning "any host").
Note however that this is not good practice - if you have a static IP, consider limiting access to that one address.

If you are planning to use MySQL workbench for managing MySQL databases and tables in a remote server, I am recommending connect over SSH. by following many articles and forums I tried many other ways by adding bind-address, adding a new user to MySql with uname#host with all privileges, etc. but in my case, all those steps were wasting of time, those steps will be useful if your server is so strict.
Follow the steps below.
Click on the + button beside the MySql connections Title to add a new connection.
Enter Connection Name(Whatever you need).
Select connection method Standard TCP/IP over SSH.
Enter the SSH details like Host(IP/Domain), Username, and Password. (SSH using Password Authorisation should be enabled in server)
MySQL host details like Hostname(By Default localhost or 127.0.0.1), port(3306), MySQL username and password.
Then click on test connection.

To get connected to MySQL Workbench installed on Windows 10, I found the above post written by Shihab and edited by Dharman useful to get connected instantly. But, here are the things that I did to connect via TCP/IP which was also useful for me to connect to MySQL via my code:
Step 1:Changing the bind address in MyMySQL
On the Ubuntu machine (where my MySQL Community Server is installed) using Putty, I changed the mysqld.cnf file with the command "sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf". Using arrow keys, I scrolled down to the row with "bind-address" and I changed it from "127.0.0.1" to "0.0.0.0". I used ctrl+x to close and entered "Y" to save the file.
Step 2: Restart the MySQL Server
You have to restart the MySQL server. To do that, you can restart using the command "sudo service mysql restart". If you are unable to restart, kill MySQL Process. First find the Process ID of MySQL using "ps ax | grep mysql" then kill the process using "sudo kill [process_id]"
Step 3:Changing the Firewall Settings of Ubuntu
To allow outside connections, you should change the settings of the firewall. I used UFW (Uncomplicated FireWall) to change the settings. Start the UFW if it is not yet enabled using the command "sudo ufw enable". Check the status using "sudo ufw status". Now, enable connections to the MySql port 3306 (default) from a particular IP address using the command "sudo ufw allow from [ip_address] to any port 3306" or you can allow all connections to the port from any IP address is your IP address keeps changing using the command "sudo ufw allow mysql"
Step 4: Creating a user in MySQL
I created a user in MySQL to avoid logging in using the root using the command "CREATE USER 'user_name'#'your_IP_Address' IDENTIFIED BY 'password';
Step 5: Connect via the MySQL workbench
Click Database
Click Connect to Database
In the Connection Method field, select "Standard TCP/IP"
In the Hostname, enter your server IP address
In the Server Port, leave as it is "3306" unless you have changed the port of MySql in your server
In the Username, enter your server user name
In the Password, enter your server password
Click Ok
Step 6: If you are still not able to connect
On your server, disable the firewall and check if you are able to connect
If you are able to connect, reset the firewall with the command "sudo ufw reset" (Make sure you take a copy or screenshot of all the connections in the firewall. You can get all the ports with the command "sudo ufw status"). If you are unable to connect go to step 9.
Now allow each and every port that was earlier present with the command "sudo ufw allow 80", "sudo ufw allow mysql", etc.
Disable and enable the firewall using the commands "sudo ufw disable" and "sudo ufw enable" respectively.
Check if you are able to connect via the workbench
If you are still not able to connect, just restart the server using the command "sudo reboot"
Once the reboot is complete, you should be able to connect to the MySQL server.
If you are still not able to connect, use something like nmap software to check for all the open ports and see if you can see 3306. If you can't see the port, you have to debug what is stopping the firewall from blocking your MySQL connections.
Check your MySQL privileges with the command "show grants for 'user_name'#'localhost';". If you are able to see the grants, that means your IP address is not allowed. Try to update the grants with your IP address with the following command "GRANT ALL PRIVILEGES ON database.* TO 'user'#'yourremotehost' IDENTIFIED BY 'newpassword';" or GRANT ALL PRIVILEGES ON database.* TO 'user'#'yourremotehost' IDENTIFIED BY 'newpassword'; (To allow connections from everywhere)
If the above steps don't work and still you want to connect via a quick hack
Premium Hack Step: Logging in using MySQL Workbench via SSH
Click Database
Click Connect to Database
In the Connection Method field, select "Standard TCP/IP over SSH"
In the SSH Hostname, enter your server IP address
In the SSH Username, enter your server user name
In the SSH Password, enter your server password
In the SSH Key file, don't do anything
In the MySQL Hostname, leave it as it is "127.0.0.1"
In the Server Port, leave as it is "3306" unless you have changed the port of MySQL in your server
In the Username, enter your MySQL username that you created in the previous step
In the Password, enter your MySQL password that you created in the previous step
Click Ok
Now, you should be able to enter your MySQL Workbench
Other useful commands:
To check if MySQL is running: systemctl status mysql
Entering MySQL: sudo mysql -u [username] -p (Default username is root and password is nothing (just press enter key))
To check the status of ufw: sudo ufw status
To check the ufw status numbered for deleting a port access: sudo ufw status numbered
To delete a ufw port access: sudo ufw delete [number_of_the_port_to_be_deleted_from_previous_command]
To start MySQL service: sudo service mysql start
To update all packages: sudo apt update
To uninstall MySQL: sudo apt-get remove mysql*
To install MySQL
Step 1: sudo apt update
Step 2: sudo apt install mysql-server
Step 3: sudo systemctl start mysql.service
Step 4 (if something is broken during installation): sudo apt --fix-broken install

Related

Cannot connect to MySQL installed on GCP Virtual Machine

I've installed MySQL on a Google Cloud Virtual Machine (debian buster). What I want to do is to make this accessible publicly (using username / password obviously).
As far as I can tell the server is visible from the outside world, as I can ping the IP and I get results, and I think I've set up a user correctly and given the appropriate permissions so I can log in.
For info, my firewall settings on GCP look like this (this is just the egress, there is one exactly the same for ingress):
Which I'm assuming is correct and leaves the correct port open?
The issue I have when I use MySQL Workbench is that when I try to create a new connection, it gives me the following error:
Your connection attempt failed for user 'username' to the MySQL server at [my ip address]:3306:
Unable to connect to localhost
Please:
1 Check that MySQL is running on address [my ip address]
2 Check that MySQL is reachable on port 3306 (note: 3306 is the default, but this can be changed)
3 Check the user username has rights to connect to [my ip address] from your address (MySQL rights define what clients can connect to the server and from which machines)
4 Make sure you are both providing a password if needed and using the correct password for [my ip address] connecting from the host address you're connecting from**
Any pointers would be gratefully received.
Update: What is really confusing me is the 'Unable to connect to localhost' error. I'm not trying to connect to localhost...?
Update 2: As per comments, results of the following commands:
Note I am trying to connect using the matprichardson username. The svc2toria user is pointing to my own IP address.
Mat, If you want to use your Google Cloud Instance Database using your MySQL workbench. I suggest you connect to it through an SSH tunnel. So, this problem won't happen. I also ran into this problem several times. Connecting through SSH made the job done.
But if your need is something else, this would not help you at all. If your only purpose is managing your database from your local machine using the MySQL workbench. This will work nicely. Create a USER in your Debian VM. and open port 22 to the public. Also, make sure to have strong credentials or a better key file when connecting through SSH. This method is working for every cloud VM database. I'm using this method for G-Cloud, Azure, and AWS. After all of your work is done. Close port 22 (SSH).
My best guess will be because of number 2. "Check that MySQL is reachable on port 3306 (note: 3306 is the default, but this can be changed)".
Your Virtual Machine will have network security controls / firewall which will be blocking port 3306 by default.
I don't use Google cloud but I believe you are looking for "network details" -> "Firewall rules".
did you change your mysqld.cnf already?
bind-address = 0.0.0.0
As none of the suggestions posted worked I went for the rather more nuclear option of deleting and rebuilding my VM and setting MySQL up again from scratch. I must have done something wrong in my initial setup, as things worked without any issues at all once I’d done this.
The location of the MySQL configuration file differs depending on the distribution.
In Ubuntu and Debian the file is located at /etc/mysql/mysql.conf.d/mysqld.cnf
while in Red Hat based distributions such as CentOS, the file is located at /etc/my.cnf
Open the file with your text editor :
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Search for a line that begins with bind-address and set its value to the IP address on which a MySQL server should listen.
By default, the value is set to 127.0.0.1 (listens only in localhost).
In this example, we’ll set the MySQL server to listen on all IPv4 interfaces by changing the value to 0.0.0.0
bind-address = 0.0.0.0
# skip-networking
If there is a line containing skip-networking, delete it or comment it out by adding # at the beginning of the line.
In MySQL 8.0 and higher, the bind-address directive may not be present. In this case, add it under the [mysqld] section.
Once done, restart the MySQL service for changes to take effect. Only root or users with sudo privileges can restart services.
To restart the MySQL service on Debian or Ubuntu, type:
sudo systemctl restart mysql
On RedHat based distributions like CentOS to restart the service run:
sudo systemctl restart mysqld
For more Detail Read Here

mariadb remote connection fail in centos

I have visited many websites for remote connection of MariaDb.
I have executed the command as below to create user with password in sql.
GRANT ALL PRIVILEGES ON . TO 'root#(my server ip)' IDENTIFIED BY '(my password)'
And i've added one line below [mysqld] in the file of /etc/my.cnf.d
bind-address = 0.0.0.0
Then restart MariaDb service as below
sudo systemctl restart mariadb
Everything runs good.
However when i access by below command, it runs failed.
mysql -u root -p -h (my server ip)
I've turn off my firewall in my server, and turn on the port 3306 in GCP server, and it can be expected, i must fail to connect in my local machine.
Since you want to use a TCP connection, I assume that you want to connect to a remote server, not to a server running on the same machine.
Make sure that you are able to connect physically to the database server, e.g. with telnet server_ip:3306.
Determine the IP address of the computer from which you want to connect to the server (= client_ip).
Add a user on the server:
GRANT ... TO root#client_ip
If client and server are running on the same machine, the preferred way is to use a linux socket (user#localhost) which is way faster.

How to connect to WSL mysql from Host Windows

I am trying to connect HeidiSql from the host to my WSL Mysql but I could not get it to connect it
Error "can't connect to Mysql server on '127.0.0.1'"
Tried SSH too but could not connect to the server
I'm also hosting mysql-server on WSL and running MySQL workbench on Windows.
I had to get the IP inside of WSL
And use this IP in MySQL Workbench
By default, MySql only listens on 127.0.0.1. If you are connecting using the IP address returned by wsl hostname -I (as mentioned by Permana) then you need to change /etc/mysql/mysql.conf.d/mysqld.cnf to listen on all IPs or your specific IP.
Edit your MySql config by typing this in your Ubuntu instance:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Change the line:
bind-address = 127.0.0.1
To:
bind-address = 0.0.0.0
or change 0.0.0.0 to the IP returned by wsl hostname -I.
Restart MySql using: sudo service mysql restart
Since your question is asked before WSL2 release, I assume you were using WSL1.
For WSL1
You can access WSL1 MySQL directly from Windows, but you were attempting access in a wrong way.
In the Network type, you should choose MariaDb or MySQL(TCP/IP) instead of MySQL (SSH Tunnel).
For WSL2
Check this WSL github issue. Save #edwindijas's powershell script and execute it by administrator. If you still cannot access MySQL and got access denied for user ... <you-computer-name>.mshome.net, you need to allow this user access from this host.
For example: let's say root, you need execute this in mysql cli:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%<you-computer-name>.mshome.net' IDENTIFIED BY '<password>';
Or allow root user access WSL2 MySQL from any host:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password';
As for me who's also using WSL for making web based application
first make sure mysql is running on WSL like sudo service mysql start
then once started, open HeidiSql and simply connect to it, here the example on my part
make sure the IP address is 127.0.0.1 not any IP, not your IP used to connect on the internet

Yet another "Lost connection to mysql server at 'reading initial communication packet', system error: 0" issue

This seems like a duplicate post, but I have checked all the solutions posted in relevant posts and none of them worked for me. So allow me to state the problem more accurately.
I have a server, where MySQL is installed. I have a user X with password P.
If I connect to the server (ssh or something) and try to run MySQL locally (mysql --user=X --password==P) it logs in perfectly, and I have access to everything:
mysql> show grants;
...
+------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'X'#'%' IDENTIFIED BY |
|PASSWORD 'somehash' WITH GRANT OPTION |
+------------------------------------------------------+
Now if I go to the config file: (sudo vim /etc/mysql/my.cnf) I see the following:
bind-address = 0.0.0.0
port = 3306
Then I go to hosts allow file (sudo vim /etc/hosts.allow) I see:
mysqld: ALL
Then I go to hosts allow file (sudo vim /etc/hosts.deny): the file is empty (except for some comments).
Still, when I try to connect with MySQL workbench I have the problem.
Here is how I do it. I go to add a new connection, I add the URL, username, pass, and port, and I click test connection and I get the message that is the title of this question. I tried with a random (non-existent) user pass combination and still I get the same response.
I tried commenting out bind-address too BTW. Also the server is generally accessible for other services like PostgreSQL and such.
not a solution to your server firewall issue but a workaround, as you are able to ssh into your database server:
You can try ssh remote port forwarding from your mysql server to your local machine, and then connect the mysql client to the local port. I use this method whenever I'm behind a firewall. As a bonus, data transmitted over this connection is also very secure.
For example, if you ssh'ed into the remote machine using
ssh hal#remote.machine.com -i ~/.ssh/hal.key
Then you could set up the port forwarding like this:
ssh -L 54321:127.0.0.1:3306 hal#remote.machine.com -i ~/.ssh/hal.key -f -N -M -S ~/.ssh/tunnel_54321_remote_machine_mysql
Then you can connect to the database as if you were connecting to the database locally (using the commanline mysql client as example):
mysql -h 127.0.0.1 -P 54321 -u my_user -p my_database
This should then prompt for your password.
To close the tunnel:
ssh -S ~/.ssh/tunnel_54321_remote_machine_mysql hal#remote.machine.com -i ~/.ssh/hal.key
I first learned about this method from the postgres docs.
This is more than likely a firewall issue.
Easiest way to debug that at first, is to try telnet to the server on port 3306 both locally, and from remote. MySQL will send the version string in plaintext that you can see inside telnet if you are being correctly connected.
If you do not get that string, then something such as a firewall is likely blocking the connection.

cant access remote mysql server need help

I have a unix server with mysql which I am trying to access from my machine. To clarify, access the mysql server. When I do I get this error.
Connecting to MySQL server 192.168.1.25...
Can't connect to MySQL server on '192.168.1.25' (10061)
Here are my credentials:
username: root
port: 3306
The server is running because I am able to remotely login onto the machine (with ip: 192.168.1.25" and then run mysql from root. What do you think is going on:
Is it a firewall issue?
Accessing as 'root' is being denied?
I am new to mysql.
1)
check the logs
you can see where the logs are by checking the my.cnf file.
mysqld.log might be the file name, but where
2)
a. see if it's a network issue.
telnet 192.168.1.25 3306
b. see if the service is at that port.
ssh to the box
# from the local host
mysql -u root
telnet 127.0.0.1 3306
ps auxw|grep mysql
you should hopefully have enough diagnostic info at this point to figure it out.
Probably because remote root login is not allowed. You can try this article on how to enable remote root:
http://benrobb.com/2007/01/15/howto-remote-root-access-to-mysql/
i have this problem and fix it by
first you must grant all to user
GRANT ALL PRIVILEGES ON databasename. TO 'user'#'%' IDENTIFIED BY 'password';
then
FLUSH PRIVILEGES;
and change the mysql config using
sudo nano /etc/mysql/my.cnf
find "bind-address" row and comment it.
save config and restart mysql using
sudo /etc/init.d/mysql restart
hope solve problem :)
The Message (10061) is do to offline severs or firewall blockage
If you get -the name of your pc - can not connect, then mysql is rejecting the connection and you will have to create a 'user' in the mysql database mysql, table user using the IP of the remote connection', and restart the database service
but for error 10061 check The Firewall in both machines.
Masters PC per say, is the one with the phpmyadmin installation
Allow Destination ip-[the remote] and port 3306 on the Master Machine
and
Allow source ip-[the masters]/with any port, and check your destination port(3306) too. destination ip can also be set on some firewalls.
And if you can, search in your firewall for logs, settings, recent activity, etc... to see what and how you are being blocked.