Cannot grant MySQL user access to information_schema database - mysql

I am creating a Laravel App and testing it in production mode on a fresh installation of Ubuntu 19.10.03. Fresh LAMP installation (PHP 7.3, MySQL 8.0.19-0).
I can create a new database, and a new user that has full access to the database, but Laravel requires that the user have the SELECT option from the 'information_schema' database as well. Here is the process I am using:
$ sudo mysql
mysql> CREATE DATABASE IF NOT EXISTS `my-laravel-database`;
mysql> CREATE USER IF NOT EXISTS 'laravelUser'#'localhost' IDENTIFIED WITH mysql_native_password BY 'myPass';
mysql> GRANT ALL PRIVILEGES ON `my-laravel-database`.* TO 'laravelUser'#'localhost' WITH GRANT OPTION;
mysql> GRANT SELECT ON `information_schema`.* TO 'laravelUser'#'localhost';
The last command always returns
ERROR 1044 (4200): Access denied for user 'root'#'localhost' to database 'information_schema'
I have tried running the mysql_secure_installation command to set the root password, this did not help. I can run the app as the root user, but I do not want to do this in production.
I have also tried a fresh installation of Ubuntu with just the LAMP services installed.

I wasted DAYS on something tangential to this problem. The answer was in the MySQL manual:
For most INFORMATION_SCHEMA tables, each MySQL user has the right to access them, but can see only the rows in the tables that correspond to objects for which the user has the proper access privileges.
So you can't grant permission to INFORMATION_SCHEMA directly, you have to grant permission to the tables on your own schemas, and as you do, those tables will start showing up in INFORMATION_SCHEMA queries.
Unfortunate, because I'd love have a user who's able to see INFORMATION_SCHEMA, but not the content of the actual tables, and there doesn't seem to be a way to do that.

Try the following:
USE mysql;
ANALYZE TABLE db;
In case table is broken run the following:
REPAIR TABLE db;
Then restart MySQL server.

Related

MariaDB - GRANT SELECT ON `DBX`.* TO `admin`#`172.22.%.%` WITH GRANT OPTION - from remote HOST fails

I've two ubuntu host, host01 is the DB Server in host02, and I need to install a PHP Application.
Both hosts are in the same network (172.22)
The installation process requires a user with administrative rights to:
create a database (DBX)
assign grants to the database, for a user.
I've added manually the user (admin) in MariaDB in this way:
CREATE USER 'admin'#'172.22.%.%' IDENTIFIED BY 'xxxxxxx';
I have no issue to create the database from host02, but when I try to run the following SQL
GRANT SELECT, UPDATE, DELETE, INSERT ON DBX.* TO 'admin'#'172.22.%.%';
I'm always getting:
Access denied for user 'admin'#'172.22.%.%' to database 'DBX'
The issue happens when the query is run from the PHP Application, and also if I use the mariadb client from host02, then it seems not related to PHP.
Any idea?

CREATE DATABASE using phpmyadmin in online server

I install phpMyAdmin on my subdomain for my clients to access their Databases, Because I can't give my Cpanel Login to them.
But my problem is when they need to create a new database, they can't do it from phpMyAdmin and they face this error
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY
'root';
And I try to create a DB using phpMyAdmin, login from cPanel but I had the same error.
No worry I can create and manage user and DB from cPanel, But the problem is I have to create a database myself when my client needs DBS.
So has I have an option for this, any type?
In your case, this error means the MySQL user does not have the required privilege to create a database. These are some options you could do:
Grant their user all privileges on all databases (not recommended):
GRANT ALL PRIVILEGES ON . TO 'sserver_sserver'#'localhost';
Create the database yourself and give their user all privileges on that specific database:
CREATE DATABASE my;
GRANT ALL PRIVILEGES ON my.* TO 'sserver_sserver'#'localhost';
Give their user all privileges to databases beginning with a certain prefix such as their business name which will allow them to create and work on as many databases as they need:
GRANT ALL PRIVILEGES ON 'businessname_%'.* TO 'sserver_sserver'#'localhost';

How to give all privileges to a new user that I created 'user#%'

I switch to MySQL 8 recently; earlier I was using MySQL 5.7 in GCP. I know questions like this have already been asked, but I didn't have any luck. My question is, I want to create a new user just say 'user1' and grant all privileges to a user account on all databases.
The query I am using for user creation:
CREATE USER 'user-1'#'%' IDENTIFIED BY 'user_password';
For privileges:
GRANT ALL PRIVILEGES ON *.* TO 'user-1'#'%';
This privileges query used to work on MySQL 5.7, but when I try to run this query in MySQL 8, I get this error (I logged in as root user and MySQL is in GCP):
SQL Error (1045): Access denied for user 'root'#'%' (using password: YES)
I also tried to run this query one after another like this:
CREATE USER 'user-1'#'%' IDENTIFIED BY 'user_password';
GRANT ALL PRIVILEGES ON *.* TO 'user-1'#'%';
But still I get the same error. I came to know that in MySQL 8 localhost root user have all the privileges, but my server is in Google Cloud, so how can I grant all the privileges to the user I created?
When I run this query in the mysql-8 (I am using HeidiSQL to connect the DB and run query)
SELECT * FROM mysql.user;
I got this output:
In this there are two root users:
For one host is localhost/127.0.0.1 (With all the privilege).
For other host is % (Not have any privilege).
I think I logged in as a user with host-% because my server is in GCP, that's why I cannot give any privilege to the user that I have created. So is there any way to give full permission to the
root#%
so that I can give full permission to the other users, Because I don't think there is any way to log in as a root#localhost
The problem here is that you are trying to create a super user, which is not something supported in cloud SQL, as you can see in this documentation:
Cloud SQL does not support SUPER privileges, which means that GRANT ALL PRIVILEGES statements will not work. As an alternative, you can use GRANT ALL ON %.*.
This alternative mentioned could be enough to grant the permissions you expected.

How do I view user accounts when logged into MySQL?

I want to see the names of all the users for MySQL. When I try to google this question most results are all concerned with users in a database as opposed to MySQL users. I checked out the MySQL documentation but it is very verbose and does not really help much. How do I view the account names of MySQL users, change permissions, and create new users?
Normally very simple... Log in as root, and do:
select * from mysql.user;
And if you googled on mysql show users you would have gotten this as first hit.
Furthermore, rights to specific databases are held in mysql.db and host limitations are in mysql.host
Display only User and Host
SELECT User, Host FROM mysql.user
Display users and privileges (pretty way)
SELECT * FROM mysql.user\G;
Create user
CREATE USER 'jeffrey'#'localhost' IDENTIFIED BY 'mypass';
Grant Privileges (Set)
-- Grant all privileges - Specific Database, all tables
GRANT ALL ON db1.* TO 'jeffrey'#'localhost';
-- Grant only SELECT privilege - Specific Database, specific table
GRANT SELECT ON db2.invoice TO 'jeffrey'#'localhost';
-- Grant USAGE privilege All databases, all tables
GRANT USAGE ON *.* TO 'jeffrey'#'localhost' WITH MAX_QUERIES_PER_HOUR 90;
List Of Privileges:
CREATE
DROP
GRANT OPTION
LOCK TABLES
REFERENCES
EVENT
ALTER
DELETE
INDEX
INSERT
SELECT
UPDATE
CREATE TEMPORARY TABLES
TRIGGER
CREATE VIEW
SHOW VIEW
ALTER ROUTINE
CREATE ROUTINE
EXECUTE
FILE
CREATE USER
PROCESS
RELOAD
REPLICATION CLIENT
REPLICATION SLAVE
SHOW DATABASES
SHUTDOWN
SUPER
ALL [PRIVILEGES]
USAGE
Performing the following query will provide all your MySQL users:
SELECT user FROM mysql.user;
You may need to login as admin to perform the above query. If that is the case login as admin from terminal by using the following command:
sudo mysql -p
Additionally, you can also create new users as follows:
create user '<user name>'#'<host name>';
Example
create user 'tony'#'localhost';
Depending on the scope of your project you will need to provide this new user with various permissions. To do that use the following syntax:
grant <permission type> on <database name> to '<user name>'#'<host name>';
Example
grant all on uncovery.* to 'tony'#'localhost';
If you are looking to do this with a GUI just to get yourself started with MySQL download MySQL workbench.
https://www.mysql.com/products/workbench/
Once this is completed launch the application and add your new connection by clicking the little + sign and filling out the parameters.
Defaults:
Hostname: 127.0.0.1 (if hosted on the machine you are running Workbench)
Username: root
Password: <blank> (if you set the root password enter that here)
Click test connection and make changes until the connection is successful and connect.
You can then click the server drop-down on the top and select Users and Privileges. This will give you a nice easy GUI to play around with things and get familiar with the available options.
Some additional documentation can be found here:
https://dev.mysql.com/doc/workbench/en/wb-mysql-connections-navigator-management-users-and-privileges.html

Can not login as newly created MySQL user

I am working on a project for a client, and instead of using my usual 'root' SQL password, I needed to create a new User/Password with specific access rights. I looke up how to do this and found that I should use the following:
CREATE USER 'AppSrv'#'%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON some_db.* TO 'AppSrv'#'%' WITH GRANT OPTION;
When I run select * from mysql.user; I can see that the user has been added to the table, however when using PDO or PHPMyAdmin I get an error stating: Access denied for user 'AppSrv'#'localhost'
I have created users in the past without a problem, and am not sure why it is now not working. I have attempted rebooting the SQL server as well with no avail.
Running:
Linux version 3.2.0-24-virtual (buildd#crested) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) )
MySQL v5.5.28-0ubuntu0.12.04.3 - (Ubuntu)
What could be a possible fix to create new users for my SQL Server?
EDIT
I have also tried using PHPMyAdmin to create the user, and even that fails when trying to login.
Create the account with #'localhost':
CREATE USER 'AppSrv'#'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON some_db.* TO 'AppSrv'#'localhost' WITH GRANT OPTION;
Otherwise the anonymous-user account for localhost that is created by mysql_install_db would take precedence - see mysql doc adding users or access denied