Communications link failure when connecting to MySQL database from Java - java

If I specify a URL like jdbc:mysql://localhost/database, then it works. If I specify either a local or remote IP address it doesn't work. Do I need to change any network settings, etc? The platform is Ubuntu.

You need to use GRANT ALL PRIVILEGES in mysql to allow external ip address
see https://dev.mysql.com/doc/refman/5.1/en/grant.html

1) change my.conf (whatever your mysql conf file is called). And set bind-address to 0.0.0.0 as it is prob 127.0.0.1
2) stop/restart mysql daemon
3) add database user perhaps coming in from diff ip addr
CREATE USER 'fred7'#'192.168.2.7' IDENTIFIED BY 'my_password';
4) grants
GRANT ALL PRIVILEGES ON test7db.* TO 'fred7'#'192.168.2.7';
5) firewall
For 3 and 4, let's say the mysql server is on aws ec2 and i am sitting at a public library using sqlyog or workbench. That prog will alert me if connect failure stating something like connection failed for user 'fred7'#'gfs6.nyc.comcastbusiness.net'. So it is pretty obvious how to do 3 and 4 then.
Good luck!

Related

java desktop application - sharing one database to client computers [duplicate]

This should be dead simple, but I cannot get it to work for the life of me.
I'm just trying to connect remotely to my MySQL server.
Connecting as:
mysql -u root -h localhost -p
works fine, but trying:
mysql -u root -h 'any ip address here' -p
fails with the error:
ERROR 1130 (00000): Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
In the mysql.user table, there is exactly the same entry for user 'root' with host 'localhost' as another with host '%'.
I'm at my wits' end and have no idea how to proceed.
Any ideas are welcome.
Possibly a security precaution. You could try adding a new administrator account:
mysql> CREATE USER 'monty'#'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
-> WITH GRANT OPTION;
mysql> CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
-> WITH GRANT OPTION;
Although as Pascal and others have noted it's not a great idea to have a user with this kind of access open to any IP. If you need an administrative user, use root, and leave it on localhost. For any other action specify exactly the privileges you need and limit the accessibility of the user as Pascal has suggest below.
Edit:
From the MySQL FAQ:
If you cannot figure out why you get
Access denied, remove from the user
table all entries that have Host
values containing wildcards (entries
that contain '%' or '_' characters). A
very common error is to insert a new
entry with Host='%' and
User='some_user', thinking that this
allows you to specify localhost to
connect from the same machine. The
reason that this does not work is that
the default privileges include an
entry with Host='localhost' and
User=''. Because that entry has a Host
value 'localhost' that is more
specific than '%', it is used in
preference to the new entry when
connecting from localhost! The correct
procedure is to insert a second entry
with Host='localhost' and
User='some_user', or to delete the
entry with Host='localhost' and
User=''. After deleting the entry,
remember to issue a FLUSH PRIVILEGES
statement to reload the grant tables.
See also Section 5.4.4, “Access
Control, Stage 1: Connection
Verification”.
One has to create a new MySQL User and assign privileges as below in Query prompt via phpMyAdmin or command prompt:
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'localhost' WITH GRANT OPTION;
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Once done with all four queries, it should connect with username / password
My error message was similar and said 'Host XXX is not allowed to connect to this MySQL server' even though I was using root. Here's how to make sure that root has the correct permissions.
My setup:
Ubuntu 14.04 LTS
MySQL v5.5.37
Solution
Open up the file under etc/mysql/my.cnf
Check for:
port (by default this is port = 3306)
bind-address (by default this is bind-address = 127.0.0.1; if you want to open to all then just comment out this line. For my example, I'll say the actual server is on 10.1.1.7)
Now access the MySQL Database on your actual server (say your remote address is 123.123.123.123 at port 3306 as user root and I want to change permissions on database 'dataentry'. Remember to change the IP Address, Port, and database name to your settings)
mysql -u root -p
Enter password: <enter password>
mysql>GRANT ALL ON *.* to root#'123.123.123.123' IDENTIFIED BY 'put-your-password';
mysql>FLUSH PRIVILEGES;
mysql>exit
sudo service mysqld restart
You should now be able to remote connect to your database. For example, I'm using MySQL Workbench and putting in 'Hostname:10.1.1.7', 'Port:3306', 'Username:root'
Just perform the following steps:
Connect to MySQL (via localhost)
mysql -uroot -p
If the MySQL server is running in Kubernetes (K8s) and being accessed via a NodePort
kubectl exec -it [pod-name] -- /bin/bash
mysql -uroot -p
Create user
CREATE USER 'user'#'%' IDENTIFIED BY 'password';
Grant permissions
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%' WITH GRANT OPTION;
Flush privileges
FLUSH PRIVILEGES;
You need to grant access to the user from any hostname.
This is how you add new privilege from phpmyadmin
Goto Privileges > Add a new User
Select Any Host for the desired username
Simple way:
Grant All Privileges ON *.* to 'USER_NAME'#'%' Identified By 'YOUR_PASSWORD';
then
FLUSH PRIVILEGES;
done!
The message *Host ''xxx.xx.xxx.xxx'' is not allowed to connect to this MySQL server is a reply from the MySQL server to the MySQL client. Notice how its returning the IP address and not the hostname.
If you're trying to connect with mysql -h<hostname> -u<somebody> -p and it returns this message with the IP address, then the MySQL server isn't able to do a reverse lookup on the client. This is critical because thats how it maps the MySQL client to the grants.
Make sure you can do an nslookup <mysqlclient> FROM the MySQL server. If that doesn't work, then there's no entry in the DNS server. Alternatively, you can put an entry in the MySQL server's HOSTS file (<ipaddress> <fullyqualifiedhostname> <hostname> <- The order here might matter).
An entry in my server's host file allowing a reverse lookup of the MySQL client solved this very problem.
This working for any future remote mysql connection !
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Navigate to the line that begins with the bind-address directive. It should look like this:
bind-address = 0.0.0.0
Login to your mysql as root terminal
mysql -u root -p
-- root password
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'localhost' WITH GRANT OPTION;
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
finally Grant that machine exclusive permission to connect to the database remotely with the following command.
sudo ufw allow from remote_IP_address to any port 3306
If you modify the grant tables manually (using INSERT, UPDATE, etc.), you should execute
a FLUSH PRIVILEGES statement to tell the server to reload the grant tables.
PS: I wouldn't recommend to allow any host to connect for any user (especially not the root use). If you are using mysql for a client/server application, prefer a subnet address. If you are using mysql with a web server or application server, use specific IPs.
Just use the interface provided by MySql's GUI Tool (SQLyog):
Click on User manager:
Now, if you want to grant access FOR ANY OTHER REMOTE PC, just make sure that, just like in the underneath picture, the Host field value is % (which is the wildcard)
Most of the answers here show you creating users with two host values: one for localhost, and one for %.
Please note that except for a built-in localhost user like root, you don't need to do this. If you simply want to make a new user that can log in from anywhere, you can use
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypassword';
GRANT <whatever privileges are appropriate> ON <relevant tables> TO myuser;
and it will work just fine. (As others have mentioned, it's a terrible idea to grant administrative privileges to a user from any domain.)
If you are using MySQL WorkBench, you can achieve this easily:
From the menu, select Server -> Users And Privileges
On the lower left, click on "Add account"
Fill the form with username, host matching (% means every host) and the password
Click on "Apply" on the lower right
After this you are good to go. Then, if you want to refine your configuration, you can use the "Administrative Roles" tab to set the command that can be used by the user (SELECT, ALTER etc etc) and the "Schema privileges" tab to restrict the user interaction to specific schemas.
Well, nothing of the above answer worked for me. After a lot of research, I found a solution. Though I may be late this may help others in future.
Login to your SQL server from a terminal
mysql -u root -p
-- root password
GRANT ALL ON *.* to root#'XX.XXX.XXX.XX' IDENTIFIED BY 'password';
This should solve the permission issue.
Happy coding!!
simple way is to login to phpmyadmin with root account , there goto mysql database and select user table , there edit root account and in host field add % wild card . and then through ssh flush privileges
FLUSH PRIVILEGES;
If this is a recent mysql install, then before changing anything else, try simply to execute this command and then try again:
flush privileges;
This alone fixes the issue for me on Ubuntu 16.04, mysql 5.7.20. YMMV.
Just find a better way to do that from your hosting control panel (I'm using DirectAdmin here)
simply go to the target server DB in your control panel, in my case:
MySQL management -> select your DB -> you will find: "Access Hosts", simply add your remote host here and its working now!
I guess there is a similar option on other C.panels like plesk, etc..
I'm hope it was helpful to you too.
If you happen to be running on Windows; A simple solution is to run the MySQL server instance configuration wizard. It is in your MYSQL group in the start menu. On the second from last screen click the box that says "allow root access from remote machines".
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
this error because no password to the root , and this Maybe occurred with you when you trying to connect from outside .
If you have WAMP Server + Windows 10 and you are using it for development than Right Click on Wamp Icon => Wamp Settings => Check Allow Virtual Hosts other than 127*
I was also facing same issue, It resolved in 2 min for me i just white list ip through cpanel
Suppose you are trying to connect database of server B from server A.
Go to Server B Cpanel->Remote MySQL-> enter Server A IP Address and That's it.
Well what you can do is just open mysql.cfg file and you have to change Bind-address to this
bind-address = 127.0.0.1
and then Restart mysql and you will able to connect that server to this.
Look this you can have idea form that.
this is real sol
This answer might help someone...
All these answers didnt help, then I realised I forgot to check one crucial thing.. The port :)
I have mysql running in a docker container running on a different port. I was pointing to my host machine on port 3306, which I have a mysql server running on. My container exposes the server on port 33060. So all this time, i was looking at the wrong server! doh!
This working for DirectAdmin;
Go to your DirectAdmin.
Go to your MySQL Management.
Select your database.
Under your Accesse Host tab, there is a field.
You should fill this field by xxx.xx.xxx.xx.
Click on Add Host.
Finished. Now you can access to this DB by your your_database_username & your_database_password.
So Simple!
CPANEL solution
Go to Cpanel, look for Remote MySQL.
Add the the IP in the input field:
Host (% wildcard is allowed)
Comment to remember what IP that is.
That was it for me.
1. From a terminal, connect you to your MySQL running container
docker exec -it your_container_name_or_id bash
2. In your container, connect you to the MySQL database
mysql -u your_user -p
enter your password to connect to database.
3. execute this SQL script to list all existing database users:
SELECT host, user FROM mysql.user;
The result will be some thing like below:
host
user
127.0.0.1
root
::1
root
localhost
mysql.sys
localhost
root
you should add a new row:
host
user
%
root
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%' WITH GRANT OPTION;
Problem: root#localhost is unable to connect to a fresh installation of mysql-community-server on openSUSE 42.2-1.150.x86_64.
Mysql refuses connections - period.
Solution:
$ ls -l /var/lib/mysql/mysql/user.*
-rw-rw---- 1 mysql mysql 0 Apr 29 19:44 /var/lib/mysql/mysql/user.MYD
-rw-rw---- 1 mysql mysql 1024 Apr 29 19:44 /var/lib/mysql/mysql/user.MYI
-rw-rw---- 1 mysql mysql 10684 Apr 29 19:44 /var/lib/mysql/mysql/user.frm
File user.MYD has 0 size (really ?!).
I copied all 3 files from another working system.
$ /usr/sbin/rcmysql stop
$ cd /var/lib/mysql/mysql/
$ scp root#othersytem:/var/lib/mysql/mysql/user.* ./
$ /usr/sbin/rcmysql start
$ cd -
$ mysql -u root -p
I was able to log in. Then, it was just a matter of re-applying all schema privileges.
Also, if you disabled IPv6, re-enable it temporary so that root#::1 account can also work.
if you are trying to execute mysql query withouth defining connectionstring, you will get this error.
Probably you forgat to define connection string before execution. have you check this out?
(sorry for bad english)
All of the answers here didn't work in my case so I guest this may help other users in the future. This can also be a problem in our code, not just in MySQL alone.
If you are using VB.NET
Instead of this code:
Dim server As String = My.Settings.DB_Server
Dim username As String = My.Settings.DB_Username
Dim password As String = My.Settings.DB_Password
Dim database As String = My.Settings.DB_Database
MysqlConn.ConnectionString = "server=" & server & ";" _
& "user id=" & username & ";" _
& "password=" & password & ";" _
& "database=" & database
MysqlConn = New MySqlConnection()
You need to move MysqlConn = New MySqlConnection() on the first line. So it would be like this
MysqlConn = New MySqlConnection()
Dim server As String = My.Settings.DB_Server
Dim username As String = My.Settings.DB_Username
Dim password As String = My.Settings.DB_Password
Dim database As String = My.Settings.DB_Database
MysqlConn.ConnectionString = "server=" & server & ";" _
& "user id=" & username & ";" _
& "password=" & password & ";" _
& "database=" & database

PHPMyAdmin access denied for user [duplicate]

MySQL 5.1.31 running on Windows XP.
From the local MySQL server (192.168.233.142) I can connect as root as follows:
>mysql --host=192.168.233.142 --user=root --password=redacted
From a remote machine (192.168.233.163), I can see that the mysql port is open:
# telnet 192.168.233.142 3306
Trying 192.168.233.142...
Connected to 192.168.233.142 (192.168.233.142).
But when trying to connect to mysql from the remote machine, I receive:
# mysql --host=192.168.233.142 --user=root --password=redacted
ERROR 1045 (28000): Access denied for user 'root'#'192.168.233.163' (using password: YES)
I have only 2 entries in mysql.user:
Host User Password
--------------------------------------
localhost root *blahblahblah
% root [same as above]
What more do I need to do to enable remote access?
EDIT
As suggested by Paulo below, I tried replacing the mysql.user entry for % with an IP specific entry, so my user table now looks like this:
Host User Password
------------------------------------------
localhost root *blahblahblah
192.168.233.163 root [same as above]
I then restarted the machine, but the problem persists.
You have to put this as root:
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'IP' IDENTIFIED BY 'PASSWORD' with grant option;
;
where IP is the IP you want to allow access, USERNAME is the user you use to connect, and PASSWORD is the relevant password.
If you want to allow access from any IP just put % instead of your IP
and then you only have to put
FLUSH PRIVILEGES;
Or restart mysql server and that's it.
I was getting the same error after granting remote access until I made this:
From /etc/mysql/my.cnf
In newer versions of mysql the location of the file is
/etc/mysql/mysql.conf.d/mysqld.cnf
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
(comment this line: bind-address = 127.0.0.1)
Then run service mysql restart.
By default in MySQL server remote access is disabled. The process to provide a remote access to user is.
Go to my sql bin folder or add it to PATH
Login to root by mysql -uroot -proot (or whatever the root password is.)
On success you will get mysql>
Provide grant access all for that user.
GRANT ALL PRIVILEGES ON *.* TO 'username'#'IP' IDENTIFIED BY 'password';
Here IP is IP address for which you want to allow remote access, if we put % any IP address can access remotely.
Example:
C:\Users\UserName> cd C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin
C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin>mysql -uroot -proot
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.27 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.25 sec)
This for a other user.
mysql> GRANT ALL PRIVILEGES ON *.* TO 'testUser'#'%' IDENTIFIED BY 'testUser';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
Hope this will help
Paulo's help lead me to the solution. It was a combination of the following:
the password contained a dollar sign
I was trying to connect from a Linux shell
The bash shell treats the dollar sign as a special character for expansion to an environment variable, so we need to escape it with a backslash. Incidentally, we don't have to do this in the case where the dollar sign is the final character of the password.
As an example, if your password is "pas$word", from Linux bash we must connect as follows:
# mysql --host=192.168.233.142 --user=root --password=pas\$word
Do you have a firewall ? make sure that port 3306 is open.
On windows , by default mysql root account is created that is permitted to have access from localhost only unless you have selected the option to enable access from remote machines during installation .
creating or update the desired user with '%' as hostname .
example :
CREATE USER 'krish'#'%' IDENTIFIED BY 'password';
Try to flush privileges again.
Try to restart server to reload grants.
Try create a user with host "192.168.233.163". "%" appears to not allow all (it's weird)
In my case I was trying to connect to a remote mysql server on cent OS. After going through a lot of solutions (granting all privileges, removing ip bindings,enabling networking) problem was still not getting solved.
As it turned out, while looking into various solutions,I came across iptables, which made me realize mysql port 3306 was not accepting connections.
Here is a small note on how I checked and resolved this issue.
Checking if port is accepting connections:
telnet (mysql server ip) [portNo]
Adding ip table rule to allow connections on the port:
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
Would not recommend this for production environment, but if your iptables are not configured properly, adding the rules might not still solve the issue. In that case following should be done:
service iptables stop
Hope this helps.
if you are using dynamic ip just grant access to 192.168.2.% so now you dont have to worry about granting access to your ip address every time.
I was struggling with remote login to MYSQL for my Amazon EC2 Linux instance. Found the solution was to make sure my security group included an inbound rule for MySQL port 3306 to include my IP address (or 0.0.0.0/0 for anywhere). Immediately could connect remotely as soon as I added this rule.
MySQL ODBC 3.51 Driver is that special characters in the password aren't handled.
"Warning – You might have a serious headache with MySQL ODBC 3.51 if the password in your GRANT command contains special characters, such as ! # # $ % ^ ?. MySQL ODBC 3.51 ODBC Driver does not support these special characters in the password box. The only error message you would receive is “Access denied” (using password: YES)" - from http://www.plaintutorials.com/install-and-create-mysql-odbc-connector-on-windows-7/
The user/host combination may have been created without password.
I was assuming that when adding a new host for an existing user (using a GUI app), the existing password would also be used for the new user/host combination.
I could log in with
mysql -u username -p PASSWORD
locally, but not from IPADDRESS with
mysql -u --host=HOST -p PASSWORD
(I could actually log in from IPADDRESS without using a password)
mysql -u --host=HOST
Setting the password allowed access:
set password for '<USER>'#'<IPADDRESS>' = '<PASSWORD>';
New location for mysql config file is
/etc/mysql/mysql.conf.d/mysqld.cnf
My case is absolutely simple.
You may have this problem in case if you type in WRONG password. No create user is needed (user already existed), no other permissions. Basically make sure that the password is correct. So make double-sure the password is correct

How to access MySQL Local Server, Error Class File Editor when use dynamic ip to change localhost [duplicate]

This should be dead simple, but I cannot get it to work for the life of me.
I'm just trying to connect remotely to my MySQL server.
Connecting as:
mysql -u root -h localhost -p
works fine, but trying:
mysql -u root -h 'any ip address here' -p
fails with the error:
ERROR 1130 (00000): Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server
In the mysql.user table, there is exactly the same entry for user 'root' with host 'localhost' as another with host '%'.
I'm at my wits' end and have no idea how to proceed.
Any ideas are welcome.
Possibly a security precaution. You could try adding a new administrator account:
mysql> CREATE USER 'monty'#'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
-> WITH GRANT OPTION;
mysql> CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
-> WITH GRANT OPTION;
Although as Pascal and others have noted it's not a great idea to have a user with this kind of access open to any IP. If you need an administrative user, use root, and leave it on localhost. For any other action specify exactly the privileges you need and limit the accessibility of the user as Pascal has suggest below.
Edit:
From the MySQL FAQ:
If you cannot figure out why you get
Access denied, remove from the user
table all entries that have Host
values containing wildcards (entries
that contain '%' or '_' characters). A
very common error is to insert a new
entry with Host='%' and
User='some_user', thinking that this
allows you to specify localhost to
connect from the same machine. The
reason that this does not work is that
the default privileges include an
entry with Host='localhost' and
User=''. Because that entry has a Host
value 'localhost' that is more
specific than '%', it is used in
preference to the new entry when
connecting from localhost! The correct
procedure is to insert a second entry
with Host='localhost' and
User='some_user', or to delete the
entry with Host='localhost' and
User=''. After deleting the entry,
remember to issue a FLUSH PRIVILEGES
statement to reload the grant tables.
See also Section 5.4.4, “Access
Control, Stage 1: Connection
Verification”.
One has to create a new MySQL User and assign privileges as below in Query prompt via phpMyAdmin or command prompt:
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'localhost' WITH GRANT OPTION;
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Once done with all four queries, it should connect with username / password
My error message was similar and said 'Host XXX is not allowed to connect to this MySQL server' even though I was using root. Here's how to make sure that root has the correct permissions.
My setup:
Ubuntu 14.04 LTS
MySQL v5.5.37
Solution
Open up the file under etc/mysql/my.cnf
Check for:
port (by default this is port = 3306)
bind-address (by default this is bind-address = 127.0.0.1; if you want to open to all then just comment out this line. For my example, I'll say the actual server is on 10.1.1.7)
Now access the MySQL Database on your actual server (say your remote address is 123.123.123.123 at port 3306 as user root and I want to change permissions on database 'dataentry'. Remember to change the IP Address, Port, and database name to your settings)
mysql -u root -p
Enter password: <enter password>
mysql>GRANT ALL ON *.* to root#'123.123.123.123' IDENTIFIED BY 'put-your-password';
mysql>FLUSH PRIVILEGES;
mysql>exit
sudo service mysqld restart
You should now be able to remote connect to your database. For example, I'm using MySQL Workbench and putting in 'Hostname:10.1.1.7', 'Port:3306', 'Username:root'
Just perform the following steps:
Connect to MySQL (via localhost)
mysql -uroot -p
If the MySQL server is running in Kubernetes (K8s) and being accessed via a NodePort
kubectl exec -it [pod-name] -- /bin/bash
mysql -uroot -p
Create user
CREATE USER 'user'#'%' IDENTIFIED BY 'password';
Grant permissions
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%' WITH GRANT OPTION;
Flush privileges
FLUSH PRIVILEGES;
You need to grant access to the user from any hostname.
This is how you add new privilege from phpmyadmin
Goto Privileges > Add a new User
Select Any Host for the desired username
Simple way:
Grant All Privileges ON *.* to 'USER_NAME'#'%' Identified By 'YOUR_PASSWORD';
then
FLUSH PRIVILEGES;
done!
The message *Host ''xxx.xx.xxx.xxx'' is not allowed to connect to this MySQL server is a reply from the MySQL server to the MySQL client. Notice how its returning the IP address and not the hostname.
If you're trying to connect with mysql -h<hostname> -u<somebody> -p and it returns this message with the IP address, then the MySQL server isn't able to do a reverse lookup on the client. This is critical because thats how it maps the MySQL client to the grants.
Make sure you can do an nslookup <mysqlclient> FROM the MySQL server. If that doesn't work, then there's no entry in the DNS server. Alternatively, you can put an entry in the MySQL server's HOSTS file (<ipaddress> <fullyqualifiedhostname> <hostname> <- The order here might matter).
An entry in my server's host file allowing a reverse lookup of the MySQL client solved this very problem.
This working for any future remote mysql connection !
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Navigate to the line that begins with the bind-address directive. It should look like this:
bind-address = 0.0.0.0
Login to your mysql as root terminal
mysql -u root -p
-- root password
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'localhost' WITH GRANT OPTION;
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
finally Grant that machine exclusive permission to connect to the database remotely with the following command.
sudo ufw allow from remote_IP_address to any port 3306
If you modify the grant tables manually (using INSERT, UPDATE, etc.), you should execute
a FLUSH PRIVILEGES statement to tell the server to reload the grant tables.
PS: I wouldn't recommend to allow any host to connect for any user (especially not the root use). If you are using mysql for a client/server application, prefer a subnet address. If you are using mysql with a web server or application server, use specific IPs.
Just use the interface provided by MySql's GUI Tool (SQLyog):
Click on User manager:
Now, if you want to grant access FOR ANY OTHER REMOTE PC, just make sure that, just like in the underneath picture, the Host field value is % (which is the wildcard)
Most of the answers here show you creating users with two host values: one for localhost, and one for %.
Please note that except for a built-in localhost user like root, you don't need to do this. If you simply want to make a new user that can log in from anywhere, you can use
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypassword';
GRANT <whatever privileges are appropriate> ON <relevant tables> TO myuser;
and it will work just fine. (As others have mentioned, it's a terrible idea to grant administrative privileges to a user from any domain.)
If you are using MySQL WorkBench, you can achieve this easily:
From the menu, select Server -> Users And Privileges
On the lower left, click on "Add account"
Fill the form with username, host matching (% means every host) and the password
Click on "Apply" on the lower right
After this you are good to go. Then, if you want to refine your configuration, you can use the "Administrative Roles" tab to set the command that can be used by the user (SELECT, ALTER etc etc) and the "Schema privileges" tab to restrict the user interaction to specific schemas.
Well, nothing of the above answer worked for me. After a lot of research, I found a solution. Though I may be late this may help others in future.
Login to your SQL server from a terminal
mysql -u root -p
-- root password
GRANT ALL ON *.* to root#'XX.XXX.XXX.XX' IDENTIFIED BY 'password';
This should solve the permission issue.
Happy coding!!
simple way is to login to phpmyadmin with root account , there goto mysql database and select user table , there edit root account and in host field add % wild card . and then through ssh flush privileges
FLUSH PRIVILEGES;
If this is a recent mysql install, then before changing anything else, try simply to execute this command and then try again:
flush privileges;
This alone fixes the issue for me on Ubuntu 16.04, mysql 5.7.20. YMMV.
Just find a better way to do that from your hosting control panel (I'm using DirectAdmin here)
simply go to the target server DB in your control panel, in my case:
MySQL management -> select your DB -> you will find: "Access Hosts", simply add your remote host here and its working now!
I guess there is a similar option on other C.panels like plesk, etc..
I'm hope it was helpful to you too.
If you happen to be running on Windows; A simple solution is to run the MySQL server instance configuration wizard. It is in your MYSQL group in the start menu. On the second from last screen click the box that says "allow root access from remote machines".
CREATE USER 'username'#'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
this error because no password to the root , and this Maybe occurred with you when you trying to connect from outside .
If you have WAMP Server + Windows 10 and you are using it for development than Right Click on Wamp Icon => Wamp Settings => Check Allow Virtual Hosts other than 127*
I was also facing same issue, It resolved in 2 min for me i just white list ip through cpanel
Suppose you are trying to connect database of server B from server A.
Go to Server B Cpanel->Remote MySQL-> enter Server A IP Address and That's it.
Well what you can do is just open mysql.cfg file and you have to change Bind-address to this
bind-address = 127.0.0.1
and then Restart mysql and you will able to connect that server to this.
Look this you can have idea form that.
this is real sol
This answer might help someone...
All these answers didnt help, then I realised I forgot to check one crucial thing.. The port :)
I have mysql running in a docker container running on a different port. I was pointing to my host machine on port 3306, which I have a mysql server running on. My container exposes the server on port 33060. So all this time, i was looking at the wrong server! doh!
This working for DirectAdmin;
Go to your DirectAdmin.
Go to your MySQL Management.
Select your database.
Under your Accesse Host tab, there is a field.
You should fill this field by xxx.xx.xxx.xx.
Click on Add Host.
Finished. Now you can access to this DB by your your_database_username & your_database_password.
So Simple!
CPANEL solution
Go to Cpanel, look for Remote MySQL.
Add the the IP in the input field:
Host (% wildcard is allowed)
Comment to remember what IP that is.
That was it for me.
1. From a terminal, connect you to your MySQL running container
docker exec -it your_container_name_or_id bash
2. In your container, connect you to the MySQL database
mysql -u your_user -p
enter your password to connect to database.
3. execute this SQL script to list all existing database users:
SELECT host, user FROM mysql.user;
The result will be some thing like below:
host
user
127.0.0.1
root
::1
root
localhost
mysql.sys
localhost
root
you should add a new row:
host
user
%
root
CREATE USER 'username'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'%' WITH GRANT OPTION;
Some tips:
list privileges using show grants;
create a VPN and just add the ip of the tunnel
Problem: root#localhost is unable to connect to a fresh installation of mysql-community-server on openSUSE 42.2-1.150.x86_64.
Mysql refuses connections - period.
Solution:
$ ls -l /var/lib/mysql/mysql/user.*
-rw-rw---- 1 mysql mysql 0 Apr 29 19:44 /var/lib/mysql/mysql/user.MYD
-rw-rw---- 1 mysql mysql 1024 Apr 29 19:44 /var/lib/mysql/mysql/user.MYI
-rw-rw---- 1 mysql mysql 10684 Apr 29 19:44 /var/lib/mysql/mysql/user.frm
File user.MYD has 0 size (really ?!).
I copied all 3 files from another working system.
$ /usr/sbin/rcmysql stop
$ cd /var/lib/mysql/mysql/
$ scp root#othersytem:/var/lib/mysql/mysql/user.* ./
$ /usr/sbin/rcmysql start
$ cd -
$ mysql -u root -p
I was able to log in. Then, it was just a matter of re-applying all schema privileges.
Also, if you disabled IPv6, re-enable it temporary so that root#::1 account can also work.
if you are trying to execute mysql query withouth defining connectionstring, you will get this error.
Probably you forgat to define connection string before execution. have you check this out?
(sorry for bad english)
All of the answers here didn't work in my case so I guest this may help other users in the future. This can also be a problem in our code, not just in MySQL alone.
If you are using VB.NET
Instead of this code:
Dim server As String = My.Settings.DB_Server
Dim username As String = My.Settings.DB_Username
Dim password As String = My.Settings.DB_Password
Dim database As String = My.Settings.DB_Database
MysqlConn.ConnectionString = "server=" & server & ";" _
& "user id=" & username & ";" _
& "password=" & password & ";" _
& "database=" & database
MysqlConn = New MySqlConnection()
You need to move MysqlConn = New MySqlConnection() on the first line. So it would be like this
MysqlConn = New MySqlConnection()
Dim server As String = My.Settings.DB_Server
Dim username As String = My.Settings.DB_Username
Dim password As String = My.Settings.DB_Password
Dim database As String = My.Settings.DB_Database
MysqlConn.ConnectionString = "server=" & server & ";" _
& "user id=" & username & ";" _
& "password=" & password & ";" _
& "database=" & database

Connection to <hostname>:5432 refused

I'm not a pro in communication networks, so I have no idea what should be my "hostname" and where can I find it?
I created a postgreSQL server on ubuntu 14.04, I can connect to it from the same computer without problem by setting hostname to 127.0.0.1(localhost), or even 192.168.1.42 (my private ip). But I can not connect to my server from any other computer, even if they are on local network or not. I always get this message:
"Connection to <hostname>:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."
where is either 127.0.1.1, 127.0.0.1, 192.168.1.42, 98.765.432.123(public ip) or anything else, it never works.
What is the right hostname?
I already set listen_addresses to '*', already edited pg_hba.conf to accept 0.0.0.0/0 IP-s, and disabled ubuntu firewall.
This could be caused by many things.
The server has an IP address. That is the address that the JDBC connection string should use for <hostname>, unless you have some local DNS solution. To rule out problems with the latter, use the IP address of the Ubuntu server. On Ubuntu type ifconfig to see it; it probably is 192.168.1.42 like you mentioned. So from the client with the Java app, you use the IP address of the server.
If you have a standard PostgreSQL installation, it should use port 5432, you can check the setting in postgresql.conf. In the JDBC connection string you should also indicate the port: "jdbc:postgresql://192.168.1.42:5432/...", just to be on the safe side.
You need to have a database that you can connect to. Note that by default only the owner of the database (probably the user postgres in your case) can connect to it. See further down.
In pg_hba.conf you need to create an entry for your new database so it can be connected to (typical settings, check your network setup). The IP address (range) you specify here is that of the clients connecting to the database!
host my_db all 192.168.0.0/16 md5
You must restart your PostgreSQL server after modifying pg_hba.conf. (Typically on Ubuntu, do sudo ./etc/init.d/postgresql restart)
To create database my_db (or whatever name you prefer), go to your Ubuntu box and enter psql as the postgres user:
vekszor#ubuntu:~$ sudo -u postgres psql
[sudo] password for user:
psql (9.3.5)
Type "help" for help
postgres=#
In order to connect to the database, you should have a database:
postgres=# CREATE DATABASE my_db;
CREATE DATABASE
You also want a user (role) that is less powerful than the postgres superuser, but still able to manipulate the database. So create this user role and assign ownership of the database:
postgres=# CREATE ROLE vekszor LOGIN PASSWORD 'secret123' CREATEROLE;
CREATE ROLE
postgres=# ALTER DATABASE my_db OWNER TO vekszor;
ALTER
Now you can go back to your client computer with the Java app and finish the JDBC connection string with the name of the database, the user and password.
Note that if you want to access your database from the internet, you should set up a NAT rule in your router to point traffic on port 5432 to the IP of your Ubuntu server.So long as the internal address of the router is in the same address range of the client computers that you indicated in pg_hba.conf this should be easy to set up, otherwise add a new entry in pg_hba.conf.
Reading your comments to the correct answer:
This is how you add a rule to iptables:
iptables -A INPUT -s 0/0 -p tcp --dport 5432 -j ACCEPT
0/0: If you want anybody to access it. You can change it to a specific ip address or range of ip addresses.
If you are connected to the server using UI you can run the below query to get the IP Address of server using below query to verify if you are using the correct IP.
Select inet_server_addr();

JDBC Connection error to mysql Access Denied for user <user>#<host>

I have already checked the other Stack Overflow and web links on this:
MySQL is on a Linux server
Linux uslx600 2.6.32-358.14.1.el6.x86_64 #1 SMP Mon
Says its IP is 1.2.3.4
I can connect to it from my laptop using MySQL Workbench. I cannot attach image due to Stack Overflow policy, but the dialog is as follows:
Connection Method: tcp/ip
Hostname: <host of the server, so e.g. 1.2.3.4>
Port: 3306 <This is same port as mentioned below for JDBC connection>
username: bugs
Default Schema: bugs
Within that mysql I ran [select user(), current_user()], I get
user() = bugs#<my laptop IP>
current_user() = bugs#%
Now I am trying to connect from the Linux server where the MySQL database is.
Following works, user is user by itself, i.e. no "#%" or something:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bugs", user, pasword);
Following also works:
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bugs", user, pasword);
But it does not work, if I give the IP address of the box or hostname. And what I am really working towards is accessing this from another Linux server, which does not work either probably due to same issue.
The error message when it fails is:
Access Denied for user 'bugs'#'<hostname of the server>' (using password=YES)
If there was some access issue on the server, shouldn't the MySQL Workbench have the same issue?
And current_user() says bug#% so does it not imply that user is setup correctly?
I have also tried changing user to 'user'#'%' etc., but in all those cases error message was always:
'bugs'#'%'#'<hostname>'
Make sure you have granted all privileges to all hosts.
Check your my.cnf file and comment out bind-address if it's pointing to 127.0.0.1 only.
MySQL root access from all hosts
The error says that it cannot connect to the server with the given password. May be your password is incorrect or if it is having special characters, you have to escape that string properly.
Another thing, the user bugs could not have privilege to connect to the server from the specified host. Try providing GRANTS to the user from the host you are connecting and then check if you are able to connect to MySQL.
GRANT ALL PRIVILEGES ON *.* TO 'bugs'#'%' IDENTIFIED BY 'password';
or comment the line in my.cnf file
#bind-address = 127.0.0.1
restart mysql service.

Categories

Resources