Codeigniter, create tables and users for MySQL

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

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

and

 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'@'%'; 

Can they be converted to Active Record, or does CI provide its own way to create users / tables? I was looking for documents and found nothing ...

EDIT : I am doing this as an intranet application, so don't worry about permissions

+6
source share
2 answers

CodeIgniter provides a class called Database Forge , it contains some functions that can be used to perform basic maintenance of your database.

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

and regarding adding users, itโ€™s possible, but I donโ€™t know if this is a good practice or not.

and also make sure the mysql user executing the queries has permissions to create db.

 $data = array( 'Host' => 'localhost' , 'User' => 'krish' , 'Password' => 'somepass', 'Select_priv' => 'Y', 'Insert_priv' => 'Y' ); $this->db->insert('mysql.user', $data); 
+4
source

IF I understand you can try

 $config['database'] = 'mysql'; // other config details eg username. port, pass $this->load->database($config); $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv) VALUES ('localhost', 'user', PASSWORD('mypass'), 'Y', 'Y', 'Y')"; $this->db->query($query); 

My syntax may be a bit off.

Essentially - use the mysql database, insert directly into the user table.

However, some hosts may restrict access to this directive.

+2
source

Source: https://habr.com/ru/post/891113/


All Articles