Create a new database with Mysql username and password directly from PHP [duplicate]

1

Is it possible to create a new database with Mysql username and password directly from the PHP script? I ask why we are finalizing a registration system where each client will have its own database. If so, could you provide an example? I understand that to create a database, I can use:

mysqli_query($conexaoPrimaria,"CREATE DATABASE ".$banco." ");

But the user (with all permissions) and password?

=====================

ANSWER

I was able to resolve it as follows:

mysqli_query($conexao,"CREATE USER 'novo_usuario'@'localhost' IDENTIFIED WITH mysql_native_password AS 'senha_usuario'");
mysqli_query($conexao,"GRANT ALL PRIVILEGES ON *.* TO 'novo_usuario'@'localhost' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;");
mysqli_query($conexao,"GRANT ALL PRIVILEGES ON 'novo_usuario'.* TO 'novo_usuario'@'localhost';");
mysqli_query($conexao,"GRANT ALL PRIVILEGES ON 'novo_usuario\_localhost'.* TO 'novo_usuario'@'localhost';");

======================

    
asked by anonymous 17.01.2018 / 13:06

2 answers

2

This is possible, but one thing that is clear, do not log in as root to do these commands.

With this in mind, a method similar to this might work.

mysql_connect('localhost', 'user', password);
mysql_query("CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';");
mysql_query("GRANT ALL ON db1.* TO 'username'@'localhost'");
mysql_close();

If you have a dedicated database for these new users as quoted, you can assign it easily.

    
17.01.2018 / 13:23
-3

To create a user you do not need to put a field token, which would be the permission, it is only necessary to generate this token and put in the session ex:

$_SESSION['token'] = $tokenGerado;

the user table you normally create with your data

    
17.01.2018 / 14:03