MySql 8 unable to connect via user - mysql

I have a strange issue. Im using Msql 8 via Docker and Im able to connect via IDE like DataGrip to connect to my db with root user. But only with root. Im trying to establish a connection with a non-root user but It is not working even with all privileges.
I thought I did something wrong when creating a new user, but in my local environment Im able to connect to my db with a non-root user.
This works:
mysql --user=test mysql -p
Priviligeses:
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `test`#`localhost` WITH GRANT OPTION |
| GRANT BACKUP_ADMIN,BINLOG_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SET_USER_ID,SYSTEM_VARIABLES_ADMIN,XA_RECOVER_ADMIN ON *.* TO `test`#`localhost` WITH GRANT OPTION
I know there are several similiar question and I tried all of them. But none of them worked or changed something.
Btw. I created my user this way:
CREATE USER 'test'#'localhost' IDENTITY BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'test'#'localhost';

Related

Granting privilege to existing user gives "you are not allowed to create a user with grant" even though the user already exists

I am logged in as root user and i just reinstalled the mysql server. login_manager isn't created as is shown when i execute SELECT * FROM mysql.user;. I now execute
CREATE USER 'login_manager'#'localhost' IDENTIFIED BY 'login_manager'; and when i check if the user is created, i can find it. Executing
GRANT INSERT
ON clients
TO login_manager;
where client is a table gives me the error i put in the title. Executing FLUSH PRIVILEGES; after creating the user didn't help either. I flushed privileges before creating the user. I looked that up, and normally this error comes when you try to create the user in the grant statement, which i am not doing.
I know this is long, but here are the Grants that root has:
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`#`localhost` WITH GRANT OPTION'
and
'GRANT APPLICATION_PASSWORD_ADMIN,AUDIT_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,INNODB_REDO_LOG_ARCHIVE,INNODB_REDO_LOG_ENABLE,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SHOW_ROUTINE,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `root`#`localhost` WITH GRANT OPTION'
and
'GRANT PROXY ON ''#'' TO 'root'#'localhost' WITH GRANT OPTION'
Your login_manager user has a Host entry, likely localhost or %.
Because of this, your GRANT technically isn't for that user, it's for a login_manager with no Host entry - so mysql knows it would have to create the user with no Host first.
Asuming you used localhost when you created the user, use this to do the grants:
GRANT INSERT
ON clients
TO 'login_manager'#'localhost';

Grant all privileges to my localhost MySQL account from a Google Cloud SQL MySQL so that I can migrate my local DB to the cloud one

I'm currently trying to grant access to my locally hosted MySQL user to my Google Cloud SQL MySQL instance so that I can migrate a database over.
Currently, I've done the following:
Authorized my IP (it's from my ISP so it will change eventually) on my Cloud SQL connections.
On my laptop I've signed into my instance with:
mysql --host=12.345.678.90 --user=root --password
Then I created myself a user so that I can use mysqladmin and mysqldump to migrate the DB after.:
mysql> CREATE USER 'andrew'#'98.76.543.210' IDENTIFIED BY 'andrew';
Then:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'andrew'#'98.76.543.210 WITH GRANT OPTION;
But I get the error:
ERROR 1045 (28000): Access denied for user 'root'#'%' (using password: YES)
Users created using Cloud SQL have all privileges except FILE and SUPER.
The set of privileges you can set are as below
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE ON mydb.* TO 'admin'#'%' WITH GRANT OPTION
So that is to say you cannot do GRANT ALL PRIVILEGES since it requires SUPERROLE.

Want to create a mysql user who can create new databases users

I'm creating a SAAS website, and every new account creation should have :
a new database created for it dynamically from my php code ,
a new MySQL user created dynamically from my PHP code and granted with privileges over the new database.
My question is: How can I create a MySQL user who have privileges to do these actions , ( create db, create users, grant privileges).
But It's important to note: I want this MySQL user to not be able to show or manipulate any other database not created by him.
Note: I have a server with WHM access.
To enable the new user called userguy to create other users on database db
create the user
CREATE USER 'userguy'#'%' IDENTIFIED BY 'password';
he needs reload on global rights
GRANT CREATE USER, RELOAD ON *.* TO 'userguy'#'%';
then whatever rights you want to give him on db
GRANT EXECUTE, SELECT, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, INDEX, INSERT, REFERENCES, TRIGGER, UPDATE, LOCK TABLES ON `db`.* TO 'userguy'#'%' WITH GRANT OPTION;
and flush at the end
FLUSH PRIVILEGES;
this should do the job.
UPDATE:
I am sorry, I have not read attentive the question (blame the coffee). If the user should also create new databases he also needs the global CREATE right, and for creating user for the DBs created by him he also needs CREATE USER
GRANT CREATE, CREATE USER, SELECT, RELOAD ON *.* TO 'userguy'#'%';
In my opinion there is no need for global GRANT privileges
I have found the main answer to my problem in the following link:
https://stackoverflow.com/a/13041542/9419598
By using :
GRANT ALL PRIVILEGES ON testuser_% . * TO 'testuser'#'%';
this will allow the testuser to create databases starting with testuser_ and will have al priviledges over them
And he will not have privileges over other databases.
And I can grant him the create user privilege
This does the stuff.

MySQL permissions to see all databases

I have a few users on my server who have access to every MySQL database. I'm trying to revoke privileges but I'm not sure how to go about it.
For example: I have a user bob who has access to every database on my server. I run the following to see which privileges bob has:
mysql -e "select * from information_schema.user_privileges;" | grep bob
'bob'#'%' def SELECT NO
'bob'#'%' def INSERT NO
'bob'#'%' def UPDATE NO
'bob'#'%' def DELETE NO
'bob'#'%' def CREATE NO
'bob'#'%' def FILE NO
'bob'#'%' def CREATE USER NO
Nothing jumps out like GRANT, ALL or SUPER. I create a new database sometest, switch to bob's account and see that bob has access to sometest. Not sure what I am missing here.
Edit: I ran SHOW GRANTS FOR 'bob'#'%'; and see:
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN,
PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES,
LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW,
CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE
ON *.* TO 'bob'#'%'
1) How does one drop this permission? I assume if I run:
REVOKE SELECT, INSERT,... on *.* to 'bob'#'%';
it will remove permissions like
GRANT ALL ON bobsdb.* TO 'bob'#'%';
which I don't want to happen. But maybe it's best to just remove all privileges and build them back up from scratch.
2) How does one identify all permissions like this? Do I have to look over every user?
You should be able to view the users' grants using the command:
SHOW GRANTS FOR 'bob'#'localhost';
From the MySQL manual:
The SHOW DATABASES privilege enables the account to see database names by issuing the SHOW DATABASE statement. Accounts that do not have this privilege see only databases for which they have some privileges, and cannot use the statement at all if the server was started with the --skip-show-database option. Note that any global privilege is a privilege for the database.
If SHOW DATABASES is enabled, revoke that permission.
REVOKE SHOW DATABASES ON *.* FROM 'bob'#'localhost';
Lastly, reload all the privileges using the command:
FLUSH PRIVILEGES;
(or restart MySQL - if that's an option - often it's not).
P.S: You might need to replace 'localhost' with your db hostname.
Some references:
List of MySQL privileges
Revoke syntax
Flushing privileges
Edit:
To answer your questions:
1) How does one drop this permission? I assume if I run: REVOKE SELECT, INSERT,... on *.* to 'bob'#'%';
You could simply run REVOKE ALL ON *.* TO 'bob'#'%';
2) How does one identify all permissions like this? Do I have to look over every user?
See this blog post. Disclamer: I am not associated with this blog.

MySQL permissions -- can't create functions even with the 'CREATE ROUTINE' grant

When connecting to my server (from a different machine) I get
Error Code: 1044 Access denied for user 'username'#'%' to database 'dbname'
when I try to create a function. But when I look at my permissions
SHOW GRANTS FOR CURRENT_USER;
I get
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE ROUTINE ON *.* TO ''username''#''%'' IDENTIFIED BY PASSWORD ''--stripped--'' WITH GRANT OPTION'
In particular, this includes CREATE ROUTINE. Why can't I make a function? How can I change it so I can?
I think there is a CREATE FUNCTION that is separate from CREATE ROUTINE. But either way, since it looks like your user has 100% full access anyway you could do:
GRANT ALL PRIVILEGES ON *.* TO user#'%' INDENTIFIED BY 'password' WITH GRANT OPTION
However I would note it would be much better to set the '%' to 'localhost' and only access the database in this manner from a local machine (or at least a trusted IP). The lack of security with this could cause you trouble.
Definitely don't use this user/password to connect to the database from a web script!
Edit
I forgot: routines and functions have to be granted globally. Adding . tries to add the grant to the tables themselves which is why it doesn't work. Try:
GRANT ALTER ROUTINE,CREATE ROUTINE, EXECUTE ON * TO user#'%' IDENTIFIED BY 'password'
There's a longer description of it here: http://dev.mysql.com/doc/refman/5.0/en/grant.html