MySQL CREATE database and independent Database account method introduction

Source: Internet
Author: User
Tags flush mysql create mysql create database create database mysql database phpmyadmin

phpMyAdmin create MySQL database and standalone Database account tutorial

On a server generally more than one site, more than a MySQL (and PHP collocation of the best combination) database.

In order to prevent security risks, we generally set up a separate database access account for each database, the account only has access to the database permissions. Let's take a concrete look at the following:

1, first we want to login phpMyAdmin (as the current mainstream development language), do not do demo.

2, create a database, the following figure, in phpMyAdmin (as the current mainstream development language) right window, fill in the database name, point creation can.

For example, here we create a database with the name: Cncmstest
Successful creation will have the following tips:

3, click on the top left corner of the homepage button, back to phpMyAdmin (as now the mainstream development language) main interface:

4. Click "Permissions" on the right side of the main screen to create the database account number.

5, in the Permissions page, we click "Add New User"

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

As shown above, we have filled in the user name: Cncmsuser, the database user only allows local access, host a selection of locally; the password we use automatically generated, the point below "Generate" will generate a random password, and then point "Copy" will automatically fill in the Password box.
The following boxes are not selected, directly pull to the bottom of the page to execute to create a new user.
The database user is successfully created and returns the following page:

7, the most important step, set the user's database access rights
You can set permissions directly in a page where the database user adds a successful return. Here we choose to specify permissions by database:

As shown above, select the Cncmstest we just created in the list of databases, which will automatically enter the permissions settings page for that database.

In the permission settings above, we select all the permissions for the "data" and "Structure" columns, and do not select administrative rights. Point execution.

Here, we have all set up, created a database: Cncmstest, and created the database user Cncmsuser, specifically specify the user only Cncmstest access rights. In this way, we have reached the point where we started by specifying separate user access rights for each database.

The following is a brief introduction to using the command to create a database

. New user.

The code is as follows Copy Code
Login to MySQL
@>mysql-u root-p
@> Password
Create a user
mysql> INSERT INTO Mysql.user (Host,user,password) VALUES ("localhost", "Phplamp", Password ("1234"));
Refresh System Permission Table
Mysql>flush privileges;

This creates a user named: phplamp Password: 1234.

Then log in.

The code is as follows Copy Code

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

2. Authorize the user.

The code is as follows Copy Code

Login to MySQL (with root privileges). I'm logged in as root.
@>mysql-u root-p
@> Password
First create a database for the user (phplampdb)
Mysql>create database phplampdb;
Authorize Phplamp users to have all the permissions of the Phplamp database.
>grant all privileges in phplampdb.* to phplamp@localhost identified by ' 1234 ';
Refresh System Permission Table
Mysql>flush privileges;
mysql> Other actions

/*
If you want to specify partial permissions to a user, you can write this:
Mysql>grant select,update on phplampdb.* to phplamp@localhost identified by ' 1234 ';
Refreshes the System permission table.
Mysql>flush privileges;
*/

3. Delete the user.

The code is as follows Copy Code
@>mysql-u root-p
@> Password
Mysql>delete from user WHERE user= "phplamp" and host= "localhost";
Mysql>flush privileges;
Delete a user's database
Mysql>drop database phplampdb;

4. Modify the specified user password.

The code is as follows Copy Code
@>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


Creating a Database
The CREATE DATABASE statement is used for creating databases 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 a MySQL connection.

Example
In the following example, we created a database called "my_db":

  code is as follows copy code

<?php
$con = mysql_connect ("localhost", "Peter", "abc123");
if (! $con)
  {
  die (' could not connect: '. Mysql_error ());
 }

If (mysql_query ("C reate database my_db ", $con)"
  {
  echo "database created";
 }
Else
  {
  ; echo "Error Creating Database:". Mysql_error ();
 }

Mysql_close ($con);
?

Related Article

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.