Home >Backend Development >PHP Tutorial >Introduction to the method of creating database and independent database account in Mysql_PHP tutorial
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 -u phplamp -p @>Enter password mysql>Login successful
|
代码如下 | 复制代码 |
//登录MYSQL(有ROOT权限)。我里我以ROOT身份登录. /* |
The code is as follows | Copy code | ||||
//Log in to MYSQL (with ROOT permissions). I log in as ROOT.
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; */
|
代码如下 | 复制代码 |
@>mysql -u root -p @>密码 mysql>update mysql.user set password=password('新密码') where User="phplamp" and Host="localhost"; mysql>flush privileges; |
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
|
Copy code | ||||
if (!$con)
mysql_close($con); ?>