How to create a database and an independent database account in Mysql _ PHP Tutorial

Source: Internet
Author: User
Tags mysql commands
How to create a database and an independent database account in Mysql. This article introduces phpMyadmin's creation of Mysql databases and independent database accounts, if you do not use the mysql command to create a database, we can use phpmyadmin to complete this article to introduce you to phpMyadmin to create a Mysql database and create an independent database account, you can use phpmyadmin to create databases without using mysql commands.

How to create a Mysql database and an independent database account using phpMyadmin

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

To prevent security risks, we generally set an independent database access account for each Database. This account has only the permission to access the database. Here is a detailed demonstration:

1. first, log on to phpMyAdmin (as the mainstream development language) without demonstration.

2. create a database. for example, in the right window of phpMyAdmin (as the current mainstream development language), enter the database name and click create.

For example, here we create a database named cncmstest
If the creation is successful, the following message is displayed:

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

4. click "permission" on the right of the main interface to create a database account.

5. on the permission page, click "add new user"

6. on this page, enter the username, access range, and password of the database to be created.

For example, if the user name is cncmsuser, the database user only allows access from the local computer, and the host is selected as the local computer, the password is automatically generated, click "Generate" below to Generate a random password, and then click "Copy" to automatically fill in the password box.
If none of the following boxes is selected, you can directly pull them to the bottom of the page to create a new user.
If the database user is created successfully, the following page is returned:

7. the most important step is to set the user's database access permissions.
You can directly set permissions on the page returned when the database user is successfully added. Here we choose to specify permissions by database:

For example, if you select the cncmstest created in the database list, the permission settings page of the database is automatically displayed.

In the permission settings, we select all the permissions in the "data" and "structure" columns, and do not select any management permissions. Click to execute.

By now, we have all set up, created a database: cncmstest, and created the database user cncmsuser, specifically specifying that the user only has access to cncmstest. In this way, we can achieve our goal at the beginning: to specify independent user access permissions for each database.

The following describes how to use commands to create a database.

. Create a user.

The code is as follows:
// Log on to MYSQL
@> Mysql-u root-p
@> Password
// Create a user
Mysql> insert into mysql. user (Host, User, Password) values ("localhost", "phplamp", password ("1234 "));
// Refresh the system permission list
Mysql> flush privileges;

In this way, a user named: phplamp password: 1234 is created.

Then log on.

The code is as follows:

Mysql> exit;
@> Mysql-u phplamp-p
@> Enter the password
Mysql> logon successful

2. authorize the user.

The code is as follows:

// Log on to MYSQL (with ROOT permission ). I log on as ROOT.
@> Mysql-u root-p
@> Password
// Create a database for the user (phplampDB)
Mysql> create database phplampDB;
// Authorize the phplamp user to have all the permissions of the phplamp database.
> Grant all privileges on phplampDB. * to phplamp @ localhost identified by '123 ';
// Refresh the system permission list
Mysql> flush privileges;
Mysql> Other operations

/*
If you want to assign some permissions to a user, you can write as follows:
Mysql> grant select, update on phplampDB. * to phplamp @ localhost identified by '123 ';
// Refresh the system permission table.
Mysql> flush privileges;
*/

3. delete a user.

The code is as follows:
@> Mysql-u root-p
@> Password
Mysql> delete from user WHERE User = "phplamp" and Host = "localhost ";
Mysql> flush privileges;
// Delete the user's database
Mysql> drop database phplampDB;

4. modify the password of the specified user.

The code is as follows:
@> Mysql-u root-p
@> Password
Mysql> update mysql. user set password = password ('New password') where User = "phplamp" and Host = "localhost ";
Mysql> flush privileges;

5. create a database


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

Syntax
Create database database_name to allow PHP to execute the preceding statement, we must use the mysql_query () function. This function is used to send queries or commands to MySQL connections.

Example
In the following example, a database named "my_db" is created:

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 );
?>

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.