dongzhizhai4070
dongzhizhai4070
2015-03-07 12:34
采纳率: 0%
浏览 45
已采纳

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

I want to Create mysql user and database using php script

how to create mysql user using mysql_query()

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

this is not working..

图片转代码服务由CSDN问答提供 功能建议

我想使用 php 创建 mysql 用户和数据库 脚本

如何使用 mysql_query()

 &lt创建 mysql 用户 ;?php 
 mysql_query(“CREATE USER'demodemodemo'@'localhost'IDENTIFIED BY'jayul321';”); 
?&gt; 
   
 
 

这是 不工作..

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxian9010
    douxian9010 2015-03-07 12:47
    已采纳

    PHP script to create MySQL database, add user and grant privileges.

    ## 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/>';
    }
    
    点赞 评论

相关推荐