Home > Backend Development > PHP Tutorial > Introduction to the method of creating database and independent database account in Mysql_PHP tutorial

Introduction to the method of creating database and independent database account in Mysql_PHP tutorial

WBOY
Release: 2016-07-13 17:06:24
Original
897 people have browsed it

This article will introduce you to phpMyadmin to create a Mysql database and establish an independent database account. If you don’t know the mysql command to create a database, we can use phpmyadmin to complete it.

phpMyadmin tutorial for creating Mysql database and independent database account

Generally speaking, there is more than one website on a server, and more than one MySQL (the best combination with PHP) database.

In order to prevent security risks, we generally set up an independent database access account for each database, which only has permission to access the database. Let us demonstrate it in detail below:

1. First, we need to log in to phpMyAdmin (as the current mainstream development language) without giving a demonstration.

2. Create a database, as shown below. In the right window of phpMyAdmin (as the current mainstream development language), fill in the database name and click Create.

For example, we create a database named: cncmstest
If the creation is successful, the following prompt will appear:

3. Click the home button in the upper left corner to return to the main interface of phpMyAdmin (as the current mainstream development language):

4. Click "Permissions" on the right side of the main interface to create a database account.

5. In the permissions page, we click "Add New User"

6. On this page, we fill in the database user name to be created, the user's access scope, and password.

As shown in the picture above, we filled in the user name: cncmsuser. The database user only allows local access. Select local for the host. We use the automatically generated password. Click "Generate" below to generate a random password, and then click "Copy" ” will be automatically filled in the password box.
Do not select any of the boxes below, just scroll to the bottom of the page and click Execute to create a new user.
If the database user is successfully created, the following page will be returned:

7. The most important step is to set the user’s database access permissions
Permissions can be set directly on the page returned after the database user is successfully added. Here we choose to specify permissions by database:

As shown in the picture above, select the cncmstest we just created in the database list, and you will automatically enter the permission setting page of the database.

In the permission settings in the picture above, we select all the permissions in the "Data" and "Structure" columns, and do not select the management permissions. Just click Execute.

At this point, we have completed all settings, created a database: cncmstest, and created the database user cncmsuser, specifically specifying that the user only has access rights to cncmstest. In this way, we achieve the purpose we talked about at the beginning: specifying independent user access rights for each database.

The following is a brief introduction to using commands to create a database

. Create a new user.

 代码如下 复制代码
//登录MYSQL
@>mysql -u root -p
@>密码
//创建用户
mysql> insert into mysql.user(Host,User,Password) values("localhost","phplamp",password("1234"));
//刷新系统权限表
mysql>flush privileges;

This creates a user named: phplamp with password: 1234.

Then log in.

The code is as follows Copy code
 代码如下 复制代码

mysql>exit;
@>mysql -u phplamp -p
@>输入密码
mysql>登录成功

mysql>exit;
@>mysql -u phplamp -p
@>Enter password
mysql>Login successful

2. Authorize users.
 代码如下 复制代码

//登录MYSQL(有ROOT权限)。我里我以ROOT身份登录.
@>mysql -u root -p
@>密码
//首先为用户创建一个数据库(phplampDB)
mysql>create database phplampDB;
//授权phplamp用户拥有phplamp数据库的所有权限。
>grant all privileges on phplampDB.* to phplamp@localhost identified by '1234';
//刷新系统权限表
mysql>flush privileges;
mysql>其它操作

/*
如果想指定部分权限给一用户,可以这样来写:
mysql>grant select,update on phplampDB.* to phplamp@localhost identified by '1234';
//刷新系统权限表。
mysql>flush privileges;
*/

The code is as follows Copy code

//Log in to MYSQL (with ROOT permissions). I log in as ROOT.
@>mysql -u root -p
@>Password
//First create a database (phplampDB) for the user
mysql>create database phplampDB;
//Authorize the phplamp user to have all permissions on the phplamp database.
>grant all privileges on phplampDB.* to phplamp@localhost identified by '1234';
//Refresh the system permission table
mysql>flush privileges;
mysql>Other operations

 代码如下 复制代码
@>mysql -u root -p
@>密码
mysql>DELETE FROM user WHERE User="phplamp" and Host="localhost";
mysql>flush privileges;
//删除用户的数据库
mysql>drop database phplampDB;
/*
If you want to specify some permissions to a user, you can write it like this:
mysql>grant select,update on phplampDB.* to phplamp@localhost identified by '1234';
//Refresh the system permission table.
mysql>flush privileges;
*/

3. Delete the user.
 代码如下 复制代码
@>mysql -u root -p
@>密码
mysql>update mysql.user set password=password('新密码') where User="phplamp" and Host="localhost";
mysql>flush privileges;
4. Modify the password of the specified user.

5. Create database


Create database
The CREATE DATABASE statement is used to create a database in MySQL.

Grammar
CREATE DATABASE database_name In order for PHP to execute the above statement, we must use the mysql_query() function. This function is used to send a query or command to the MySQL connection.

Example
In the example below, we create a database named "my_db":

The code is as follows
 代码如下 复制代码

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

mysql_close($con);
?>

Copy code
$con = mysql_connect("localhost","peter","abc123");

if (!$con)

{ } if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); }
mysql_close($con);

?>
http://www.bkjia.com/PHPjc/630695.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630695.htmlTechArticleThis article will introduce phpMyadmin to create a Mysql database and establish an independent database account. If you don’t know the mysql command, We can use phpmyadmin to create databases and the like...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template