MySQL user guide

Source: Internet
Author: User
Tags mysql commands mysql host mysql tutorial
Mysql user guide
Mysql does not have a configuration interface like mssql, so many people have installed mysql but do not know how to use it. In this article, we will learn some common MYSQL commands from the aspects of connecting to MYSQL, changing the password, and adding users.

1. Connect to MYSQL. Format: mysql-h host address-u user name-p User Password 1,
Example 1: connect to MYSQL on the local machine. Start --- run --- Enter cmd to enter the DOS window, enter mysqlbin In the mysql installation directory, and then enter the mysql-uroot-p command, after you press enter, you are prompted to enter the password. If you have just installed MYSQL, the Super User root does not have a password (or it may have been set during installation), so you can directly press enter to enter MYSQL, MYSQL prompt: mysql> 2,
Example 2: connect to MYSQL on the remote host. Assume that the IP address of the remote host is 110.110.110.110, the user name is root, and the password is abcd123. Run the following command: mysql-h210.210.210.210-uroot-pabcd123 (Note: you do not need to add spaces to the u and root, the same applies to others). 3. Run the MYSQL command: exit (Press ENTER)

2. Change the password. Format: mysqladmin-u username-p old password new password 1,

Example 1: Add a password ab12 to the root user. First, enter the directory mysqlbin in DOS, and then type the following command mysqladmin-uroot-password ab12. Note: because the root has no password at the beginning, the old-p password can be omitted. 2,

Example 2: Change the root password to djg345. Mysqladmin-uroot-pab12 password djg345 3. Add new users. (Note: Unlike the above, the following commands are in the MYSQL environment, so a semicolon is followed as the command Terminator) Format: grant select on database. * to username @ login host identified by "password" Example 1. Add a user test1 whose password is abc so that he can log on to any host, all databases are permitted to query, insert, modify, and delete databases. First, use the root user to connect to MYSQL, and then type the following command: grant select, insert, update, delete on *. * to test1 @ "%" Identified by "abc ";
However, the User Added in Example 1 is very dangerous. If someone knows the password of test1, then he can log on to your mysql database on any computer on the internet and do whatever he wants for your data. For the solution, see Example 2.

Example 2: Add a user named "test2" with the password "abc" so that the user can only log on to localhost, you can also query, insert, modify, and delete the database mydb (localhost refers to the local host, that is, the host where the MYSQL database is located), so that the user knows the password of test2, he cannot access the database directly from the internet, but can only access the database through the web pages on the MYSQL host. Grant select, insert, update, delete on mydb. * to test2 @ localhost identified by "abc"; if you do not want test2 to have a password, you can run another command to cancel the password. Grant select, insert, update, delete on mydb. * to test2 @ localhost identified "";

The above describes how to log on to MYSQL. The following describes the specific operations of MYSQL. Note: you must first log on to MYSQL. The following operations are performed at the MYSQL prompt and each command ends with a semicolon.

I. Operation Skills
1. If you forget the extra points after you press Enter when making the command, you don't have to repeat the command. You just need to press a semicolon to press Enter. That is to say, you can divide a complete command into several lines, and then use a semicolon as the end sign to complete the operation.

2. You can use the cursor to bring up or down the previous commands. However, an old MYSQL version I used earlier does not support this feature. I am using a mysql-4.1.14-win32

Ii. Display commands
1. display the Database List. Show databases; At the beginning, there were only two databases: MySQL and test. The MySQL database contains the MySQL system information. We change the password and add new users to use this database for operations.
2. display the data tables in the database: use MySQL; // open the database. If you have learned FOXBASE, show tables is a good choice;
3. display the structure of the data table: Describe table name; // you can view the specific table.
4. database creation: Create Database database name;
5. Create a table: Use Database Name; Create Table Name (field setting list );
6. Delete databases and tables: drop database database name; drop table name;
7. Clear the records in the Table: delete from table name;
8. Display records in the Table: Select * from table name;

3. An instance for creating a database, creating a table, and inserting data

Drop database if exists school; // Delete if SCHOOL exists
Create database school; // create a database SCHOOL
Use school; // open the SCHOOL library
Create table teacher // create table TEACHER
(Id int (3) auto_increment not null primary key, name char (10) not null, address varchar (50) default 'shenzhen ', year date ); // end table creation // insert fields as follows
Insert into teacher values ('', 'glengang ', 'xiamen Zhongyi', '2017-10-10 ');
Insert into teacher values ('', 'jack', 'xiamen Zhongyi ', '2017-12-23 ');
Note: Table Creation
(1) set the ID to a numeric field of 3: int (3) and let it automatically add one: auto_increment for each record. It cannot be blank: not null and set it to the primary key.
(2) Set NAME to a 10-character field
(3) set ADDRESS to a 50-character field, and the default value is Shenzhen. What is the difference between varchar and char? It will only be discussed later.
(4) set YEAR as the date field. If you type the preceding command at the mysql prompt, debugging is not convenient. You can write the above commands into a text file as they are. SQL, then copy it to the c: directory, and enter the directory mysqlbin in DOS status, and then type the following command: mysql-uroot-p password <c: school. if the SQL statement is successful, no display is displayed for a blank row. If an error occurs, a prompt is displayed. (The preceding command has been debugged. You only need to remove the // annotation to use it ).

4. convert text data to the database. 1. The text data must conform to the format: field data is separated by the tab key, and null values are replaced by n.

Example: 3 rose Xiamen No. 2 Middle School 1976-10-104 mike Xiamen No. 1 1975-12-232, data import command load data local infile "file name" into table Name; note: you 'd better copy the file to the mysqlbin directory and use the use command to create the database where the table is located.

V. Back up the database:
(Run the command in the mysqlbin directory of DOS) mysqldump -- opt school> school. bbb Note: Back up the database school to school. bbb file, school. bbb is a text file with any file name. Open it and you will find a new one. Note: In fact, MYSQL's database operations are similar to those of other SQL databases. You 'd better read this SQL book. I will only introduce some basic things here. In fact, I only understand these things,

Haha. The best MySQL tutorial is the MySQL Chinese reference manual translated by Yan Zi. It is not only free to download from every related website, but also the most authoritative. It is a pity that the chm format is not like the "PhP4 Chinese manual", and it is not convenient to search for function commands.

Appendix:
Create, authorize, delete, and change passwords for mysql users

Note: This article is included in phplamp.com phplamp.cn

Test environment: Win32 mysql5.0.45

First, declare that, in general, you need to have the root permission in MySQL to modify the MySQL password and grant permissions.

Note: This operation is performed at the win command prompt, and phpMyAdmin is also applicable.
User: phplamp user database: phplampdb

1. Create a user.

// 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.

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

2. Authorize the user.

// 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.
@> 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.
@> Mysql-u root-p
@> Password
Mysql> update mysql. user set password = password ('new password') where User = "phplamp" and Host = "localhost ";
Mysql> flush privileges;

Change the root password;
1. log in as root
2. mysql> set password for root = PASSWORD ('new _ password ');
// Note: set password for and later passwords must be in uppercase and lowercase. You can change the PASSWORD after you try it.

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.