Advertisement
Guest User

Untitled

a guest
May 11th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. <?php
  2.  
  3. $project = "test";
  4. $con=mysqli_connect("localhost","user","password");
  5.  
  6. function createUserAndDb($con, $proj, $type, $host = "localhost"){
  7. $password = bin2hex(openssl_random_pseudo_bytes(10));
  8.  
  9. $user = "{$proj}-{$type}";
  10. $userHost = "`{$user}`@`{$host}`";
  11.  
  12. $sql="CREATE USER {$userHost} identified by '{$password}'";
  13. if (mysqli_query($con,$sql)){
  14. echo "Created $user with $password".PHP_EOL;
  15. }else{
  16. echo mysqli_error($con).PHP_EOL;
  17. }
  18.  
  19. $sql="CREATE DATABASE `{$user}`";
  20. if (mysqli_query($con,$sql)){
  21. echo "Created database $user".PHP_EOL;
  22. }else{
  23. echo 'database create error '.mysqli_error($con).' '.$sql.PHP_EOL;
  24. }
  25.  
  26. mysqli_query($con,"use `{$user}`");
  27.  
  28. $sql= "GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, LOCK TABLES, REFERENCES, SELECT, SHOW VIEW, TRIGGER, UPDATE ON `{$user}`.* TO {$userHost}" ;
  29. if (mysqli_query($con,$sql)){
  30. echo "Grant for db {$user}.* to {$userHost}".PHP_EOL;
  31. mysqli_query($con,"FLUSH PRIVILEGES");
  32. }else{
  33. echo 'error grant premissions '.mysqli_error($con).PHP_EOL.$sql.PHP_EOL;
  34. }
  35. };
  36.  
  37. createUserAndDb($con, $project, 'dev');
  38. createUserAndDb($con, $project, 'prod');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement