MySql command instance aggregation _ MySQL

Source: Internet
Author: User
This article mainly introduces MySql commands, analyzes the usage methods and precautions of various common operations such as MySQL database creation, connection, addition, deletion, modification, and query based on instances, and is of great practical value, for more information about MySQL commands, see the following examples. We will share this with you for your reference. The details are as follows:

Mysql installation directory

Database Directory

/Var/lib/mysql/

Configuration file

/Usr/share/mysql (mysql. server command and configuration file)

Related commands

/Usr/bin (commands such as mysqladmin mysqldump)

Start script

/Etc/init. d/mysql (directory for starting the script file mysql)

System Management

Connect to MySQL

Format: mysql-h host address-u user name-p user password

Example 1: Connect to MySQL on the local machine.

The code is as follows:

Hadoop @ ubuntu :~ $ Mysql-uroot-pmysql;

Example 2: Connect to MYSQL on the remote host.

The code is as follows:

Hadoop @ ubuntu :~ $ Mysql-h 127.0.0.1-uroot-pmysql;

Change New Password

Enter mysql-u username-p password on the terminal, and press enter to enter Mysql.

> Use mysql;> update user set password = PASSWORD ('New password') where user = 'username';> flush privileges; # update permission> quit; # exit

Add new users

The code is as follows:

Mysql> grant select, insert, update, delete on *. * to root @ localhost identified by 'mysql ';


Or

The code is as follows:

Grant all privileges on *. * to root @ localhost identified by 'mysql ';


Then refresh the permission settings.

The code is as follows:

Flush privileges;

Example 2: If you do not want the root user to have a password to operate the data table in the database "mydb", you can run another command to remove the password.

The code is as follows:

Grant select, insert, update, delete on mydb. * to root @ localhost identified '';

Delete a user

Hadoop @ ubuntu :~ $ Mysql-u username-p password mysql> delete from user where user = 'username' and host = 'localhost'; mysql> flush privileges; // delete the user's database mysql> drop database dbname;

Database operations

Show all databases

The code is as follows:

Mysql> show databases;

(Note: There is a last s)

Create a database

The code is as follows:

Mysql> create database test;

Connect to database

The code is as follows:

Mysql> use test;

View the currently used database

The code is as follows:

Mysql> select database ();

Table information contained in the current database

The code is as follows:

Mysql> show tables;

(Note: There is a last s)

Delete database

The code is as follows:

Mysql> drop database test;

Table operations

The code is as follows:

Mysql> drop table MyClass;

Insert data

The code is as follows:

Mysql> insert into MyClass values (1, 'Tom ', 96.45), (2, 'job', 82.99), (2, 'Wang', 96.59 );

Query table data

Query all rows

The code is as follows:

Mysql> select * from MyClass;

Query the first few rows of data

For example, view the first two rows of data in the MyClass table.

The code is as follows:

Mysql> select * from MyClass order by id limit 0, 2;


Or

The code is as follows:

Mysql> select * from MyClass limit 0, 2;

Delete table data

The code is as follows:

Mysql> delete from MyClass where id = 1;

Modify table data

The code is as follows:

Mysql> update MyClass set name = 'Mary 'where id = 1;

Add fields to the table

The code is as follows:

Mysql> alter table MyClass add passtest int (4) default '0'

Change Table name

The code is as follows:

Mysql> rename table MyClass to YouClass;

Update field content

The code is as follows:

Update article set content = concat ('', content );

Import and Export databases

Export database files from a database

The code is as follows:

C: \> mysqldump-h localhost-u root-p mydb> e: \ MySQL \ mydb. SQL


Enter the password and wait for a moment until the export is successful. you can check whether the export is successful in the target file.

Example 2: export mytable in mydb to the e: \ MySQL \ mytable. SQL file.

The code is as follows:

C: \> mysqldump-h localhost-u root-p mydb mytable> e: \ MySQL \ mytable. SQL

Example 3: export the database mydb structure to the e: \ MySQL \ mydb_stru. SQL file.

The code is as follows:

C: \> mysqldump-h localhost-u root-p mydb -- add-drop-table> e: \ MySQL \ mydb_stru. SQL


Note:-h localhost can be omitted, which is generally used on a VM.

3) only export data without exporting the data structure

Format:

The code is as follows:

Mysql> show databases;


2. create a database named MYSQLDATA

The code is as follows:

Mysql> create database mysqldata;


3. select the database you created

The code is as follows:

Mysql> use mysqldata;

(When you press the Enter key to see Database changed, the operation is successful !)
4. view the tables in the current database

The code is as follows:

Mysql> show tables;


5. create a database table

The code is as follows:

Mysql> create table mytable (name VARCHAR (20), sex CHAR (1 ));


6. display the table structure:

The code is as follows:

Mysql> describe mytable;


7. add records to the table

The code is as follows:

Mysql> insert into MYTABLE values ("hyq", "M ");


8. load data into database tables in text mode (for example, D:/mysql.txt)

The code is as follows:

Mysql> load data local infile "D:/mysql.txt" into table mytable;


9. import the. SQL file command (for example, D:/mysql. SQL)

The code is as follows:

Mysql> use database;
Mysql> source d:/mysql. SQL;


10. delete a table

The code is as follows:

Mysql> drop table mytable;


11. clear the table

The code is as follows:

Mysql> delete from MYTABLE;


12. update table data

The code is as follows:

Mysql> update MYTABLE set sex = "f" where name = 'hyq ';

The following are the management experiences of using MySql on the network:

In windows, MySql exists as a service. before using mysql, make sure that the service has been started and the net start MySql command is not enabled. In Linux, the "/etc/rc. d/init. d/mysqld start" command is available. Note that the initiator must have administrator privileges.

The newly installed MySql contains a root account with a blank password and an anonymous account, which poses a major security risk. for some important applications, we should improve the security as much as possible, here, you should delete anonymous accounts and set passwords for root accounts. you can run the following command:

use mysql;delete from User where User="";update User set Password=PASSWORD('newpassword') where User='root';

If you want to restrict the logon terminal used by the User, you can update the Host field of the corresponding User in the User table. after making the above changes, restart the database service, at this time, the following commands can be used for logon:

mysql -uroot -p;mysql -uroot -pnewpassword;mysql mydb -uroot -p;mysql mydb -uroot -pnewpassword;

The preceding command parameters are part of common parameters. for details, refer to the documentation. Here, mydb is the name of the database you want to log on.

In development and practical applications, users should not only use root users to connect to the database. although it is convenient to use root users for testing, it will bring significant security risks to the system, it is not conducive to the improvement of management techniques. We grant the most appropriate Database permissions to the users used in an application. For example, a user who only inserts data should not be granted the permission to delete data. MySql User management is implemented through the User table. There are two common methods to add new users. one is to insert the corresponding data rows in the User table and set the corresponding permissions; the second is to use the GRANT command to create a user with certain permissions. The common usage of GRANT is as follows:

grant all on mydb.* to NewUserName@HostName identified by "password" ;grant usage on *.* to NewUserName@HostName identified by "password";grant select,insert,update on mydb.* to NewUserName@HostName identified by "password";grant update,delete on mydb.TestTable to NewUserName@HostName identified by "password";

To GRANT the user the ability to manage permissions on the corresponding object, you can add the with grant option after GRANT. For users inserted into the User table, use the Password function to update and encrypt the PASSWORD field to prevent unauthorized users from stealing the Password. Users who do not need permissions should be cleared, and those who pass the permissions should be revoked in a timely manner. to REVOKE permissions, you can update the corresponding fields in the User table or use the REVOKE operation.

The following is my interpretation of common permissions from other information (www.cn-java.com:

Global management permissions:

FILE: read and write files on the MySQL server.
PROCESS: displays or kills service threads of other users.
RELOAD: RELOAD access control tables and refresh logs.
SHUTDOWN: shut down the MySQL service.
Database/data table/data column permissions:
ALTER: modify existing data tables (such as adding/deleting columns) and indexes.
CREATE: CREATE a new database or data table.
DELETE: DELETE table records.
DROP: Delete a data table or database.
INDEX: create or delete an INDEX.
INSERT: Add table records.
SELECT: Displays/searches for table records.
UPDATE: modify existing records in the table.
Special permissions:
ALL: Allow anything (same as root ).
USAGE: only logon allowed-nothing else is allowed.

I hope this article will help you design your MySQL database.

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.