如何使用php脚本创建mysql用户和数据库?

时间:2015-03-07 12:34:18

标签: php mysql

我想使用mysql脚本

创建php用户和数据库

如何使用mysql

创建mysql_query()用户
<?php
    mysql_query("CREATE USER 'demodemodemo'@'localhost' IDENTIFIED BY 'jayul321';");
?>

这不起作用..

1 个答案:

答案 0 :(得分:2)

用于创建MySQL数据库的PHP脚本,添加用户和授予权限。

## make sure you connect first to mysql with a super user (ex: root)
mysql_connect('localhost', 'root', 'password');

$dbName = 'database_name'; // new database name
$dbUser = 'db_user';       // new user name
$dbPass = 'db_pass';       // new user password

$queries = array(
    "CREATE DATABASE `$dbName` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci",
    "CREATE USER '$dbUser'@'localhost' IDENTIFIED BY '$dbPass'",
    "GRANT USAGE ON * . * TO '$dbUser'@'localhost' IDENTIFIED BY '$dbPass' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0",
    "GRANT SELECT , INSERT , UPDATE, DELETE ON `$dbName` . * TO '$dbUser'@'localhost'",
    "FLUSH PRIVILEGES"
);

foreach($queries as $query) {
    echo 'Executing query: "'.htmlentities($query).'" ... ';
    $rs = mysql_query($query);
    echo ($rs ? 'OK' : 'FAIL') . '<br/><br/>';
}