How do I view user accounts when logged into MySQL? - 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

Related

Give user permission to drop and create specific databases

This question refers to MySQL and/or MariaDB specifically.
Is it possible to permit certain users to drop/create one (or more) specific databases, but not all databases? By this, I do not mean how to give users all privileges for certain specific databases with a query like:
GRANT ALL PRIVILEGES ON `example-db`.* TO 'example-user'#'localhost';
I've found some methods on how to do this for MSSQL here and a similar post for PostgreSQL.
grant privileges separately for a user account in MySQL.
When specifying the database name and table name, separate them with a. (period) and no spaces. This will give the root user fine-grain control over certain data.
Also, replace the PERMISSION_TYPE value with the kind of access you want to grant to your new user account.
Here are the most used commands in MySQL:
CREATE — enable users to create a database or table
SELECT — permit users to retrieve data
INSERT — let users add new entries in tables
UPDATE — allow users to modify existing entries in tables
DELETE — enable users to erase table entries
DROP — let users delete entire database tables
NOTE: Using the ALL PRIVILEGES permission type from before will allow
all of the permissions listed above.
To use any of these options, simply replace PERMISSION_TYPE with the appropriate keyword. To apply multiple privileges, separate them with a comma. For example, we can assign CREATE and SELECT to our non-root MySQL user account with this command:
GRANT CREATE, SELECT ON * . * TO 'user_name'#'localhost';
Sometimes, you might come across a situation where you need to revoke given privileges from a user. You can do so by entering:
REVOKE PERMISSION_TYPE ON database_name.table_name FROM ‘user_name’#‘localhost’;
For example, to withdraw all privileges for our non-root user we should use:
REVOKE ALL PRIVILEGES ON * . * FROM 'user_name'#'localhost';
Finally, you can entirely delete an existing user account by using the following command:
DROP USER ‘user_name’#‘localhost’;
Don't forget to FLUSH PRIVILEGES;
In order to find what privileges have already been granted to a MySQL user, you can use the SHOW GRANTS command:
SHOW GRANTS FOR 'user_name'#'localhost';
Let’s start by making a new user within the MySQL shell:
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
To provide a specific user with permission, you can use this framework:
GRANT type_of_permission ON database_name.table_name TO 'username'#'localhost'
Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.
FLUSH PRIVILEGES;

How to create a user with more privileges than the currently logged in user in MySQL/MariaDB?

I'm logged into a MariaDB instance as a user admin. The admin user has got the GRANT and CREATE privileges because it is supposed to add new users to the instance, with non-global privileges for particular databases. The admin user misses DROP and DELETE privileges on purpose because I don't want him to be able to delete data by accident. On the other hand, I want users created by the admin user to have DROP and DELETE privileges on particular tables.
By default, MariaDB doesn't allow me to create users that have a privilege that admin hasn't at all. I see the security consideration here, since users could easily exploit that right and create users with more "power". But is there any possibility to achieve my desired behaviour in MariaDB/MySQL?
Try this one on your linux console:
mysql -u root -p
Then your root password.
First we create the database ( yes database first)
CREATE DATABASE newdatabase;
Then we create the newuser with GRANT only for THIS newdatabase:
GRANT ALL PRIVILEGES ON newdatabase.* TO newuser#'%' IDENTIFIED BY 'any_password';
flush privileges;
exit

mysql - How to grant read only permissions to a user?

I have a mysql user, whom I want to grant all the READ permission on a db schema.
One way is this :
GRANT SELECT, SHOW_VIEW ON test.* TO 'readuser'#'%';
Is there a way to group all read operations in grant ?
If there is any single privilege that stands for ALL READ operations on database.
It depends on how you define "all read."
"Reading" from tables and views is the SELECT privilege. If that's what you mean by "all read" then yes:
GRANT SELECT ON *.* TO 'username'#'host_or_wildcard' IDENTIFIED BY 'password';
However, it sounds like you mean an ability to "see" everything, to "look but not touch." So, here are the other kinds of reading that come to mind:
"Reading" the definition of views is the SHOW VIEW privilege.
"Reading" the list of currently-executing queries by other users is the PROCESS privilege.
"Reading" the current replication state is the REPLICATION CLIENT privilege.
Note that any or all of these might expose more information than you intend to expose, depending on the nature of the user in question.
If that's the reading you want to do, you can combine any of those (or any other of the available privileges) in a single GRANT statement.
GRANT SELECT, SHOW VIEW, PROCESS, REPLICATION CLIENT ON *.* TO ...
However, there is no single privilege that grants some subset of other privileges, which is what it sounds like you are asking.
If you are doing things manually and looking for an easier way to go about this without needing to remember the exact grant you typically make for a certain class of user, you can look up the statement to regenerate a comparable user's grants, and change it around to create a new user with similar privileges:
mysql> SHOW GRANTS FOR 'not_leet'#'localhost';
+------------------------------------------------------------------------------------------------------------------------------------+
| Grants for not_leet#localhost |
+------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, REPLICATION CLIENT ON *.* TO 'not_leet'#'localhost' IDENTIFIED BY PASSWORD '*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' |
+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Changing 'not_leet' and 'localhost' to match the new user you want to add, along with the password, will result in a reusable GRANT statement to create a new user.
Of, if you want a single operation to set up and grant the limited set of privileges to users, and perhaps remove any unmerited privileges, that can be done by creating a stored procedure that encapsulates everything that you want to do. Within the body of the procedure, you'd build the GRANT statement with dynamic SQL and/or directly manipulate the grant tables themselves.
In this recent question on Database Administrators, the poster wanted the ability for an unprivileged user to modify other users, which of course is not something that can normally be done -- a user that can modify other users is, pretty much by definition, not an unprivileged user -- however -- stored procedures provided a good solution in that case, because they run with the security context of their DEFINER user, allowing anybody with EXECUTE privilege on the procedure to temporarily assume escalated privileges to allow them to do the specific things the procedure accomplishes.
GRANT SELECT ON *.* TO 'user'#'localhost' IDENTIFIED BY 'password';
This will create a user with SELECT privilege for all database including Views.
Note for MySQL 8 it's different
You need to do it in two steps:
CREATE USER 'readonly_user'#'localhost' IDENTIFIED BY 'some_strong_password';
GRANT SELECT, SHOW VIEW ON *.* TO 'readonly_user'#'localhost';
flush privileges;
Various permissions that you can grant to a user are
ALL PRIVILEGES- This would allow a MySQL user all access to a designated database (or if no database is selected, across the system)
CREATE- allows them to create new tables or databases
DROP- allows them to them to delete tables or databases
DELETE- allows them to delete rows from tables
INSERT- allows them to insert rows into tables
SELECT- allows them to use the Select command to read through databases
UPDATE- allow them to update table rows
GRANT OPTION- allows them to grant or remove other users' privileges
To provide a specific user with a permission, you can use this framework:
GRANT [type of permission] ON [database name].[table name] TO ‘[username]’#'localhost’;
I found this article very helpful
A step by step guide I found here.
To create a read-only database user account for MySQL
At a UNIX prompt, run the MySQL command-line program, and log in as an administrator by typing the following command:
mysql -u root -p
Type the password for the root account.
At the mysql prompt, do one of the following steps:
To give the user access to the database from any host, type the following command:
grant select on database_name.* to 'read-only_user_name'#'%' identified by 'password';
If the collector will be installed on the same host as the database, type the following command:
grant select on database_name.* to 'read-only_user_name' identified by 'password';
This command gives the user read-only access to the database from the local host only.
If you know the host name or IP address of the host that the collector is will be installed on, type the following command:
grant select on database_name.* to 'read-only_user_name'#'host_name or IP_address' identified by 'password';
The host name must be resolvable by DNS or by the local hosts file.
At the mysql prompt, type the following command:
flush privileges;
Type quit.
The following is a list of example commands and confirmation messages:
mysql> grant select on dbname.* to 'readonlyuser'#'%' identified
by 'pogo$23';
Query OK, 0 rows affected (0.11 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Even user has got answer and #Michael - sqlbot has covered mostly points very well in his post but one point is missing, so just trying to cover it.
If you want to provide read permission to a simple user (Not admin kind of)-
GRANT SELECT, EXECUTE ON DB_NAME.* TO 'user'#'localhost' IDENTIFIED BY 'PASSWORD';
Note: EXECUTE is required here, so that user can read data if there is a stored procedure which produce a report (have few select statements).
Replace localhost with specific IP from which user will connect to DB.
Additional Read Permissions are-
SHOW VIEW : If you want to show view schema.
REPLICATION CLIENT : If user need to check replication/slave status.
But need to give permission on all DB.
PROCESS : If user need to check running process. Will work with all
DB only.
If you want the view to be read only after granting the read permission you can use the ALGORITHM = TEMPTABLE in you view DDL definition.
solution: here's some useful cookbook for creating a readonly user on mysql.
# 1. connect as an admin on database / cluster
mysql -u root -h mydb.123456789012.us-east-1.rds.amazonaws.com
# 2. create user protected with strong password with global access ('%') or local access ('localhost')
mysql> CREATE USER 'ro_user'#'%' IDENTIFIED BY 'abcd1234%^&#';
# 3. grant SELECT privileges for relevant user
mysql> GRANT SELECT ON *.* TO 'ro_user'#'%' WITH GRANT OPTION;
# 4. reload grant tables on database
mysql> FLUSH PRIVILEGES;
# 5. verify grant are placed as expected
mysql> show grants for 'ro_user'#'%';
// output:
// +------------------------------------------------------+
// | Grants for ro_user#% |
// +------------------------------------------------------+
// | GRANT SELECT ON *.* TO 'ro_user'#'%' WITH GRANT OPTION |
// +------------------------------------------------------+
// 1 row in set (0.00 sec)
mysql> exit

delete MySQL entire database. Database not showing in MySQL Databases

Every time I create a database using a custom joomla template quick install the database is created but does not show up in MySQL Database management despite the fact that it most definitely does exist and MySQL database management knows it does because it wont let me create a database with that name due to error "Database already exists".
I want to delete joomlasall database.
Full size image
http://img99.imageshack.us/img99/4995/tempkh.png
If you can not see database but you are sure that it exists, this is definitely permissions issue.
Do
SHOW GRANTS
More info here
You will see that you does not hold global SELECT privilege.
You need to explicitly GRANT permissions with similar command like:
GRANT ALL PRIVILEGES ON DBNAME.* TO 'username'#'localhost';
Instead of ALL you can specify SELECT, INSERT, UPDATE, DELETE , EXECUTE, etc... check this
replace DBNAME with your DB name, username with user for whom you want to grant access and localhost with hostname if DB is used remotly.
To do this, you need GRANT privilege or to be root user.
use same mysql user credentials for Joomla DB connectivity also the one you are using in phpmyadmin.

How to create users in MySQL4

I tried using the "create user" command in a MySQL4 database (something similar to what is available in the MySQL5 docs), but it failed. Can someone provide me the right syntax?
Users are created the first time you GRANT them a privilege.
From http://dev.mysql.com/doc/refman/4.1/en/grant.html :
The GRANT statement creates MySQL user accounts and grants rights to accounts.
So, let's say you have a database "mydb", with a table "mytable". If you want to create a user "jason", with the password "pwd123!" who has SELECT privileges on this table, you can do this:
grant select on mydb.mytable to 'jason'#'hostname' identified by 'pwd123!';
The usual caveats about hostname apply.
If you want to give jason full permissions on mydb:
grant all on mydb.* to 'jason'#'hostname' identified by 'pwd123!';
Important note: every time you use identified by, you're changing the password for that user/hostname, so you you will typically only use this syntax when creating a user!