Unable to create new databases in mysql - mysql

I am able to create new databases through the root account. but I need to create new databases using a new user that I have created.
Here is error I'm getting when trying to create a database using the new user
ERROR 1044 (42000): Access denied for user 'user'#'localhost' to database 'new_db'
How can I grant permissions to "user" so it create any number of new databases?
Update:
This is also what I tried:
mysql> grant all privileges on db.* to 'user'#'%' with create;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create option' at line 1
mysql>

PHP example:
$con = new mysqli("...","...","..."); // CONNECT TO DB WITH ROOT USER
mysqli_query($con, "GRANT ALL PRIVILEGES ON newdb.* TO 'user'#'%' WITH GRANT OPTION");
Command line example:
login with root user, and use this:
GRANT ALL PRIVILEGES ON newdb.* TO 'user'#'%' WITH GRANT OPTION;
This will create a user how is permitted to create new databases (but will give him also all the permissions to do bad stuff, so handle it wise...).
This is for create only:
GRANT CREATE ON *.* TO 'newuser'#'%' IDENTIFIED BY 'password'

Related

How Can I Reliably Drop and Create a Database and a User in MySQL

I am currently running two scripts.
01_INIT_DATABASE.SQL
CREATE DATABASE foobar;
USE foobar;
CREATE USER 'foo'#'localhost' IDENTIFIED BY 'bar';
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'#'localhost' WITH GRANT OPTION;
CREATE USER 'foo'#'%' IDENTIFIED BY 'bar';
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'#'%' WITH GRANT OPTION;
and
01_DROP_DATABASE.SQL
USE foobar
DROP USER 'foo'#'localhost';
DROP USER 'foo'#'%';
DROP DATABASE IF EXISTS foobar;
They each selectively fail if one or the other has not run properly.
How can I get them to run reliably (or fail gracefully, for example, to only drop a user if it exists)
I am running them through an ant task (if that affects things, but it is really just a Java Exec), in the form of
<exec executable="mysql" input="./src/main/ddl/create/01_init_database.sql">
<arg value="-uroot"/>
<arg value="-ptoor"/>
</exec>
My MySQL Version is
mysql Ver 14.14 Distrib 5.5.52, for debian-linux-gnu (x86_64) using readline 6.2
Update
IF EXISTS and IF NOT EXISTS does not work for USER. It works fine for DATABASE.
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS 'foo'#'localhost' IDENTIFIED BY 'bar'' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS 'foo'#'%'' at line 1
You can add IF NOT EXISTS to your databasechema and user creation: like:
CREATE DATABASE IF NOT EXISTS foobar;
CREATE USER IF NOT EXISTS 'foo'#'localhost' IDENTIFIED BY 'bar';
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'#'localhost' WITH GRANT OPTION;
CREATE USER IF NOT EXISTS 'foo'#'%' IDENTIFIED BY 'bar';
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'#'%' WITH GRANT OPTION;
and for the drop:
DROP USER IF EXISTS 'foo'#'localhost';
DROP USER IF EXISTS 'foo'#'%';
DROP DATABASE IF EXISTS foobar;
As mentioned below: the user if not exists only works on mysql 5.7 and higher.
Do not use the create user syntax below 5.7, but change the grant statement to:
GRANT ALL PRIVILEGES ON foobar.* TO 'foo'#'localhost' identified by 'password' WITH GRANT OPTION;

ERROR 1064 (42000) at line 1

I doing a Batch File and Got the error please help me.
The Error --
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GRANT OPTION' at line 1
MySQL Script
CREATE USER 'a4db'#'%' IDENTIFIED BY '0987654321';
GRANT ALL PRIVILEGES ON *.* TO 'a4db'#'%' IDENTIFIED BY '0987654321' WITH GRANT OPTION;
FLUSH PRIVILEGES;
I'm want to create the new user for remote the Sql Databases from client PC. If I copy the script to the workbench or to cmd it will run correctly and no error.
Please help me solve this error.
Add *.* between ON and TO ,the first * is your database name,and the second * is the table name. *.* means all database.all tables. For example, if you want this user just have the right to operate table 'test' in the database 'db',you may write like this db.test
GRANT ALL PRIVILEGES ON *.* TO 'a4db'#'%' IDENTIFIED BY '0987654321' WITH GRANT OPTION;

How can I give CREATE USER and GRANT privileges to an user in MySQL?

I created a new user called Hamza in localhost then I tried to give it these 2 privileges:
CREATE USER
GRANT
For the creation everything is OK, but when it comes to granting the privileges I tried this query :
GRANT CREATE USER TO 'Hamza'#'localhost' WITH GRANT OPTION;
And I got this error :
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TO 'Hamza'#'localhost' WITH GRANT OPTION' at line 1
Could someone tell me how can I correct this syntax error ?
You are missing the ON object_name here in your GRANT statement like
GRANT CREATE USER ON *.* TO 'Hamza'#'localhost' WITH GRANT OPTION;

ERROR 1046 (3D000): No database selected while creating user

When I create a admin user with:
GRANT ALL ON * TO my_name#localhost IDENTIFIED BY 'my_passwd' WITH GRANT OPTION
I received the error message "ERROR 1046 (3D000): No database selected".
You should be saying it like below by qualifying it with database name. Check MySQL Documentation for more information.
GRANT ALL ON db_name.*
So for your case,
GRANT ALL ON db1.* TO my_name#localhost IDENTIFIED BY 'my_passwd'
WITH GRANT OPTION;

Allow GRANT permission to MySQL User

I am creating a user app_root identified by a password with limited permission. This user should only be able to create a database with prefix, create a new user, and grant permission to new user to access the database. So this is what I am doing.
CREATE USER app_root#localhost IDENTIFIED BY 'password';
GRANT CREATE USER ON *.* TO app_root#localhost WITH GRANT OPTION;
GRANT CREATE ON `myapp\_%`.* TO app_root#localhost WITH GRANT OPTION;
FLUSH PRIVILEGES;
The user is duly created. Now I log in to mysql with app_root user and execute following:
CREATE DATABASE test;
It fails as expected because name of database does not contain myapp_ prefix.
CREATE DATABASE myapp_test;
CREATE USER myapp_test#localhost IDENTIFIED BY 'ABC';
Both database and user are successfully created. But if I execute grant statement, error occurs.
GRANT ALL PRIVILEGES ON myapp_test.* TO myapp_test#localhost;
It generates ERROR 1044 (42000): Access denied for user 'app_root'#'localhost' to database 'myapp_test'. Could anyone please tell me what is wrong with my approach?
MySQL Version - Server version: 5.5.37-0ubuntu0.14.04.1 (Ubuntu)
Edit - As suggested, I granted all privileges to app_root user. But it still does not change anything.
GRANT ALL ON `myapp\_%`.* TO app_root#localhost WITH GRANT OPTION;
FLUSH PRIVILEGES;