How to create users in MySQL4 - mysql

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!

Related

Error when granting privilege - Mysql GRANT is deprecated, use ALTER USER instead

I'm using the latest version of mysql. When running the below command, its asking me to use the ALTER USER command instead of GRANT. I don't see the syntax to use the ALTER USER command to update privileges anywhere. https://dev.mysql.com/doc/refman/5.7/en/alter-user.html
GRANT ALL PRIVILEGES ON *.* to 'root'#'localhost' IDENTIFIED by ‘1234’;
Any pointers would be much appreciated.
The documentation of GRANT mentions:
Note
If an account named in a GRANT statement does not already exist, GRANT may create it under the conditions described later in the discussion of the NO_AUTO_CREATE_USER SQL mode. It is also possible to use GRANT to specify nonprivilege account characteristics such as whether it uses secure connections and limits on access to server resources.
However, use of GRANT to create accounts or define nonprivilege characteristics is deprecated as of MySQL 5.7.6. Instead, perform these tasks using CREATE USER or ALTER USER.
The IDENTIFIED by ‘1234’ part of your query is used to set/change the password of the user. The password is not a privilege, it should be changed using ALTER USER.
Use:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost'
to grant the desired privileges to the user and:
ALTER USER 'root'#'localhost' IDENTIFIED BY '1234'
to change its password, if needed.
Remark
It is worth mentioning that the ALTER USER statement was introduced in MySQL 5.6. For older versions, the GRANT statement is the only way to change the user's password.

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

MySql grant user permission

I want to create a new user in MySql. I do not want that new user to do much with my existing databases [I just want to grant Select privilege to him], but he can do anything and everything with a new database which he creates.
Firstly, is there a way to grant permission as per the database owner? If it is possible, then that is the ideal thing I am looking for. And if not, then how do I restrict a particular user from accessing [only Select privilege] some specific database only, allowing him to do anything he wants with the remaining ones?
From the MySQL grant documentation:
CREATE USER 'jeffrey'#'localhost' IDENTIFIED BY 'mypass';
GRANT SELECT ON *.* TO 'jeffrey'#'localhost';
GRANT ALL ON db1.* TO 'jeffrey'#'localhost';
The first command creates the user. The second grants select on all databases and tables. The third command grants all access to all tables in db1.
Is there anything else specific you are looking to do?
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’;
GRANT ALL PRIVILEGES ON * . * TO 'newuser'#'localhost';
The asterisks in this command refer to the database and table (respectively) that they can access—this specific command allows to the user to read, edit, execute and perform all tasks across all the databases and tables.
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;
For more about permission you can read this article
https://www.digitalocean.com/community/articles/how-to-create-a-new-user-and-grant-permissions-in-mysql
For the list of permissions, see the MySQL Manual page Privileges Provided by MySQL.
Open mysql command prompt.
To create a new user when host is localhost then use this command
CREATE user 'test_user'#'localhost' identified by 'some_password';
for any host use %, like this
CREATE user 'test_user'#'%' identified by 'some_password';
Once the user is created, you need to Grant some access. Use following command for this.
GRANT SELECT,INSERT,UPDATE
ON database_name.table_name
TO 'test_user'#'localhost';
After successful execution of above query, test_user can select, insert and update in table_name (name of table) of database_name (name of database).
grant privilege is given in data base like this
grant privilege on object to user
object is any data base table or relation and user might be the whom the privilege is provided to him.
Example
grant select,insert,update,on object name to user name
grant select on employee to john with grant option;
revoke delete on employee from john.

in mysql, set permission to connect mysql command line

creating users through mysql admin, but unable to login mysql command line
following on-line suggestions, as root issued
grant all on *.* to new_user;
it worked, but security-wise was a mess, so issued
revoke all on *.* from new_user;
now new_user can still connect, but security is sane as set in mysql admin
to me this is thoroughly hocus-pocus. what's really going on, and how do you really enable login?
this seems to be a MySQL Administrator problem (thanks #marco). if the same GRANT is issued in mysql command line, the user can log in; but it the grant is issued in Administrator, the user cannot log in.
as #marco pointed out, any access will grant mysql login access to the user, eg, SELECT privileges - but they need to be entered in msql command line.
That's because when you first use GRANT, user is created automatically; when you revoke privileges, user remains...
Check this link.
First you should give your user only the privileges he really needs.
Second: give the user access only to db or tables he should see/work on.
Example:
GRANT SELECT,INSERT,UPDATE ON mydb.* TO 'jeffrey'#'localhost';
or
GRANT SELECT ON db2.invoice TO 'jeffrey'#'localhost'
IDENTIFIED BY PASSWORD 'sdsd';
EDITED:
Just to prove what I'm saying:
enter mysql console (mysql -u root -p) and type
USE mysql;
SELECT * FROM user;
You'll see users MySql has inside.
Well, now use GRANT as you please on a user which does not exists yet; then repeat SELECT * FROM user;: you'll see new user created!!

Let MySQL users create databases, but allow access to only their own databases

I want to have multiple a MySQL users to be able to issue commands like
CREATE DATABASE dbTest;
But I also want each of these users to be able to see and access only their own databases.
All I could find was how to either create the databases by a DBA and grant the privileges on this database to the specific user:
GRANT ALL PRIVILEGES ON dbTest.* TO 'user';
or grant privileges on all databases to a user:
GRANT ALL PRIVILEGES ON *.* TO 'user';
But neither is what I want, because it needs to scale and be secure.
You can use
GRANT ALL PRIVILEGES ON `testuser\_%` . * TO 'testuser'#'%';
to grant the user testuser privileges on all databases with names beginning with testuser_.
This allows the testuser to create databases limited to names starting with testuser_
You can use
GRANT ALL PRIVILEGES ON `testuser_%` . * TO 'testuser'#'%';
to grant the user testuser privileges on all databases with names beginning with testuser_.
EDIT: I'm not sure if this user is now also allowed to create databases.
Yes, this allows the testuser to create databases limited to names starting with testuser_
Create a stored procedure that is defined by the admin user and invokes with the admin user privileges by using SQL SECURITY DEFINER. In the stored procedure,
Create the database.
Set the privileges on the database so only the current user has access.
Execute FLUSH PRIVILEGES to reload the privileges from the grant tables.
Use USER() to get the current user login details.
Find out more about SQL SECURITY DEFINER.
It is impossible to do this using permissions only .
The workaround as suggested in another answer:
GRANT ALL PRIVILEGES ONtestuser_%. * TO 'testuser'#'%';
has the problem that the users must then be very careful in naming their databases.
For example if user aaa creates database bbb_xyz, it can then be accessed exclusively by user bbb but not by user aaa.