MySQL allow remote connections from multiple ip addresses - mysql

I am aware of the command
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'1.2.3.4' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
That will allow remote connections from the ip 1.2.3.4, but what if I wanted to allow connections from both 1.2.3.4 and 5.6.7.8?
Would it look like this?
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'1.2.3.4, 5.6.7.8' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
Or is this not allowed?

You can run above command many times to GRANT access from multiple IPs.
mysql> GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'1.2.3.4' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'5.6.7.8' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
to all ips
mysql> GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

With MySQL wildcards.
instead of using limited numbers you can implement a wildcard. this can be done by adding '%' instead of the numbers.

Related

Allow MySQL access from given IP range

Normally, I use host "localhost". But now I wish to allow MySQL Workbench to access. Workbench is located on a Windows PC with IP 10.x.x.x, and MySql is located on 10.y.y.y. I've tried the following to no avail. How is this accomplished?
CREATE USER 'myUserName'#'10.%' IDENTIFIED BY 'myPassword';
GRANT ALL PRIVILEGES ON * . * TO 'myUserName'#'10.%';
This is what phpMyAdmin displays when exporting the users:
# Privileges for `myUserName`#`10%`
GRANT ALL PRIVILEGES ON *.* TO 'myUserName'#'10%' IDENTIFIED BY PASSWORD '*xxx';
# Privileges for `myUserName`#`10.%`
GRANT ALL PRIVILEGES ON *.* TO 'myUserName'#'10.%' IDENTIFIED BY PASSWORD '*xxx';
# Privileges for `myUserName`#`localhost`
GRANT ALL PRIVILEGES ON *.* TO 'myUserName'#'localhost' IDENTIFIED BY PASSWORD '*xxx' WITH GRANT OPTION;

What does each part of this command to grant all privileges mean?

Can someone explain the following command?
mysql> GRANT ALL PRIVILEGES ON *.* TO 'user1'#'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES
This gives the user specified later in the command all privileges.
http://dev.mysql.com/doc/refman/5.7/en/grant.html
ON *.*
Matches everything.
TO 'user1'#'localhost'
The user with name ‘user1’ on localhost which is to be granted the privileges.
WITH GRANT OPTION
The GRANT OPTION privilege enables you to give to other users or remove from other users those privileges that you yourself possess.
https://dev.mysql.com/doc/refman/5.6/en/privileges-provided.html#priv_grant-option

Grant all privileges to all users on a host in mysql

How to grant all privileges to all users in mySQL ?
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'1.2.3.4' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
What do I replace username with in order to allow it for every user on the given IP?Even if it is '' i.e no username at all in input ?
I tried * and % in username but that did not help.
You can try like this:
GRANT ALL PRIVILEGES ON *.* TO ''#'localhost' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
The manual says:
In this case, any user who connects from the local host with the
correct password for the anonymous user will be permitted access, with
the privileges associated with the anonymous-user account.
This is working for all my MySQL users irrespective of their hostname and Ips.
GRANT ALL ON <yourdbname>.* TO ''#'%' IDENTIFIED BY 'yourdbpassword';
FLUSH PRIVILEGES;
This should work:
GRANT ALL PRIVILEGES ON 'database'.'table' TO '%'#'1.2.3.4' WITH GRANT OPTION;
Wild card does not work for user in mysql grant privilege command.
If you know list of users then yOU can try :-
GRANT ALL PRIVILEGES ON *.* TO 'user1'#'1.2.3.4',
'user2'#'1.2.3.4',
'user3'#'1.2.3.4',
'user4'#'1.2.3.4',
'user5'#'1.2.3.4',
'user6'#'1.2.3.4';
If you want to grant all privileges to all users and be able to use without a password, the command:
GRANT ALL PRIVILEGES ON *.* TO ''#'%' IDENTIFIED BY '' WITH GRANT OPTION;
If, for example you hava database named 'my_db' and you doing operations from localhost, the command can be the follow:
GRANT ALL PRIVILEGES ON my_db.* TO ''#'localhsot' IDENTIFIED BY '' WITH GRANT OPTION;
MySQL does not support wildcards in user names, so you cannot do that in a single grant.
see the official doc

mysql access denied eventhough grant permission exists

I have created user and grant all permissions, but still not able to connect to the sql using it. Could someone kindly help resolving this?
Steps followed
mysql --user=root mysql;
CREATE USER 'db_user'#'%' IDENTIFIED BY 'password';
CREATE USER 'db_user'#'localhost' IDENTIFIED BY 'password';
CREATE USER 'db_user'#'hostname' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'db_user'#'%' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'db_user'#'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'db_user'#'hostname' WITH GRANT OPTION;
When I try to connect like
mysql -u db_user -ppassword -h 'hostname'
I get this error
ERROR 1045 (28000): Access denied for user 'db_user'#'hostname' (using password: YES)
MariaDB [(none)]> show grants for db_user#'hostname';
GRANT ALL PRIVILEGES ON *.* TO 'db_user'#'hostname' IDENTIFIED BY PASSWORD '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19' WITH GRANT OPTION
1 row in set (0.00 sec)
What is it missing that it is not letting to connect?
Can you confirm again that the hostname is the hostname of the local host?
Can you try these.
DROP USER 'db_user'#'localhost';
DROP USER 'db_user'#'%';
DROP USER 'db_user'#'hostname';
GRANT ALL PRIVILEGES ON *.* TO 'db_user'#'localhost' IDENTIFIED BY PASSWORD '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'db_user'#'%' IDENTIFIED BY PASSWORD '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'db_user'#'hostname' IDENTIFIED BY PASSWORD '*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19' WITH GRANT OPTION;
mysql -u db_user -ppassword -h `hostname`

Is there a way to GRANT ALL PRIVILEGES to the same user from multiple LAN addresses in a single command?

I'm using the following command
GRANT ALL PRIVILEGES ON *.* TO 'user'#'ip'
IDENTIFIED BY 'password'
WITH GRANT OPTION;
To grant all privileges to a user. Is there a way I can make the ip a wildcard like 192.168.1.* so that I don't need to manually add each LAN ip I want give the user access to connect from?
Yes, use % in an address.
GRANT ALL PRIVILEGES ON *.* TO 'user'#'192.168.1.%'
IDENTIFIED BY 'password'
WITH GRANT OPTION;
Or you can use less restrictive host name and allow user to connect from everywhere.
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%'
IDENTIFIED BY 'password'
WITH GRANT OPTION;
According to your requirement,
Let's start by making a new user called "chaminda" within the MySQL shell:
CREATE USER 'chaminda'#'%' IDENTIFIED BY 'password';
The first thing to do is to provide the user with necessary permission and here I have given all permission to the particular user.
GRANT ALL PRIVILEGES ON * . * TO 'chaminda'#'%' WITH GRANT OPTION;
Reload all the privileges.
FLUSH PRIVILEGES;
Note: Here host Name = % and that means you can access this database server from any host. Giving all privileges to the user is a big risk and that's not a best practice.