为MySQL创建表和用户

为MySQL创建表和用户

本文介绍了Codeigniter,为MySQL创建表和用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CI以编程方式创建数据库和用户。到目前为止我有这两个简单的MySQL语句。

I want to create databases and users with CI programmatically. So far i have these 2 simple MySQL statements.

CREATE DATABASE `testdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE USER 'test_user'@'%' IDENTIFIED BY '***';

GRANT ALL PRIVILEGES ON * . * TO 'test_user'@'%' IDENTIFIED BY '***' WITH GRANT
OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0
MAX_USER_CONNECTIONS 0 ;

GRANT ALL PRIVILEGES ON `testdb` . * TO 'test_user'@'%';

可以将这些转换为Active Record,还是CI提供自己创建用户/表的方式?我搜索文档,没有找到任何东西...

Can those be converted to Active Record, or does CI provides its own way of creating users/tables? i search the docs and couldn't find anything...

编辑:我这样做内部网应用程序,所以不用担心权限

EDIT: I do this as an intranet application so no worries about permissions

推荐答案

CodeIgniter提供了一个名为,它包含一些可用于对数据库执行基本维护的函数。

CodeIgniter provides a class called as Database Forge , it contains some functions which can be used to perform basic maintenance on your DB.

$this->load->dbforge()

if ($this->dbforge->create_database('my_db'))
{
    echo 'Database created!';
}

并且关于添加用户,它可能,但我不知道一个好的做法。

and regarding adding users, its possible, but I don't know if its a good practice or not.

,并且确保运行查询的mysql用户有权限创建db。

and also make sure that the mysql user who runs the queries has the permissions to create db.

$data = array(
   'Host' => 'localhost' ,
   'User' => 'krish' ,
   'Password' => 'somepass',
   'Select_priv' => 'Y',
   'Insert_priv' => 'Y'
);

$this->db->insert('mysql.user', $data);

这篇关于Codeigniter,为MySQL创建表和用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 18:34