Various commands about mysql database under Linux operating system (full)!

1. Commands related to mysql database under Linux

1. The command to start mysql under linux:

mysqladmin start
/ect/init.d/mysql start (The previous is the installation path of mysql)

2. The command to restart mysql under linux:

mysqladmin restart
/ect/init.d/mysql restart (The previous is the installation path of mysql)

3. The command to close mysql under linux:

mysqladmin shutdown
/ect/init.d/mysql shutdown (The previous is the installation path of mysql)

4. Connect to mysql on this machine:

Enter the directory mysql\bin, and then type the command mysql -uroot -p, press Enter and prompt for the password.
Exit the mysql command: exit (Enter)

5. Modify the mysql password:

mysqladmin -u username -p old password password new password
or enter the mysql command line SET PASSWORD FOR root=PASSWORD("root");

6. Add new users. (Note: The commands in the mysql environment are followed by a semicolon as the command terminator)

grant select on database.* to Username@Login host identified by "Password"
If you add a user test password to 123, so that he can log in on any host, and has query, insert, modify, and delete permissions for all databases. First connect to mysql as the root user, and then type the following command:
grant select,insert,update,delete on . To "Identified by "123";

Two, operations related to mysql database

You must log in to mysql first, and the relevant operations are performed at the mysql prompt, and each command ends with a semicolon

1. Display the database list.

show databases;

2. Display the data table in the library:

use mysql; //Open the library
show tables;

3. Display the structure of the data table:

describe table name;

4. Build a library:

create database 库名;

GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
UTF8: CREATE DATABASE test2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

5. Build a table:

use library name;
create table table name (field setting list);

6. Delete database and delete table:

drop database library name;
drop table table name;

7. Clear the records in the table:

delete from 表名;

truncate table table name;

8. Display the records in the table:

select * from 表名;

9. Modification of coding

If you want to change the encoding format of the entire mysql:
when starting mysql, add
–default-character-set=gbk to the mysqld_safe command line

If you want to change the encoding format of a library: enter the command
alter database db_name default character set gbk at the mysql prompt ;

10. Rename the table

alter table t1 rename t2;

11. View the efficiency of sql statement

explain < table_name >

例如:explain select * from t3 where id=3952602;

12. Load the data into the database table in text mode (for example, D:/mysql.txt)

mysql> LOAD DATA LOCAL INFILE “D:/mysql.txt” INTO TABLE MYTABLE;

Three, data import and export

1. The text data is transferred to the database

The format that the text data should conform to: the field data is separated by the tab key, and the null value is used instead. Example:
1 name duty 2006-11-23
data transfer command load data local infile "file name" into table table name;

2. Export databases and tables

mysqldump --opt news> news.sql (Back up all the tables in the database news to the news.sql file, news.sql is a text file, the file name is
arbitrary. ) mysqldump --opt news author article> author.article. sql (Back up the author table and article table in the news database to the author.article.sql file, author.article.sql is a text file, and the file name is whatever you want.)
mysqldump --databases db1 db2> news.sql (the database dbl and db2 are backed up to the news.sql file, news.sql is a text file, the file name is
whatever you want .) mysqldump -h host -u user -p pass --databases dbname> file.dump
is the name user on the host , Import the database dbname of the password pass into the file file.dump
mysqldump --all-databases> all-databases.sql (back up all databases to the all-databases.sql file, all-databases.sql is a text file, the file name Whatever you want.)

3. Import data

mysql <all-databases.sql (import database)
mysql>source news.sql; (executed under mysql command, can import tables)

One, connect to MySQL

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

1. Example 1: Connect to MYSQL on this machine.

First open the DOS window, then enter the directory mysqlbin, and then type the command mysql -uroot -p, and then press Enter to prompt you to enter the password. If MYSQL has just been installed, the super user root has no password, so press Enter to enter MYSQL is in, the prompt of MYSQL is: mysql>.

2. Example 2: Connect to MYSQL on the remote host. Assume that the IP of the remote host is 110.110.110.110, the user name is root, and the password is abcd123. Then type the following command:

mysql -h110.110.110.110 -uroot -pabcd123

(Note: u and root do not need to add spaces, the other is the same)

3. Exit MYSQL command: exit (Enter).

Two, change the password

Format: mysqladmin -u username -p old password password new password

1. Example 1: Add a password ab12 to root. First enter the directory mysqlbin under DOS, and then type the following command:

mysqladmin -uroot -password ab12

Note: Because root has no password at the beginning, the -p old 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 is a command in the MySQL environment, so it is followed by a semicolon as the command terminator)

Format: grant select on database.* to username@login host identified by "password"

Example 1. Add the password of a user test1 to abc, so that he can log in on any host and have query, insert, modify, and delete permissions for all databases. First connect to MySQL as the root user, and then type the following command:

grant select,insert,update,
delete on . to test1@"%" Identified by “abc”;

But the added user in Example 1 is very dangerous. If someone knows the password of test1, then he can log in to your MySQL database on any computer on the Internet and do whatever you want with your data. Solution See example 2.

Example 2. Add a user test2 with a password of abc, so that he can only log in on localhost, and can query, insert, modify, and delete the database mydb (localhost refers to the local host, that is, the host where the MySQL database is located) In this way, even if the user knows the password of test2, he cannot directly access the database from the Internet, and can only access it through the web page on the MySQL host.

grant select,insert,update,
delete on mydb.* to test2@localhost identified by “abc”;

If you don't want test2 to have a password, you can delete the password by typing another command.

grant select,insert,update,delete on mydb
.* to test2@localhost identified by “”;

Start: net start mySql;
enter: mysql -u root -p/mysql -h localhost -u root -p databaseName;
list databases: show databases;
select databases: use databaseName;
list tables: show tables;
display table columns Attribute: show columns from tableName;
build database: source fileName.txt;
matching characters: wildcard _ can be used to represent any character,% represents any string;
add a field: alter table tabelName add column fieldName dateType;
add multiple fields: alter table tabelName add column fieldName1 dateType, add columns fieldName2 dateType;
multi-line command input: note that the word cannot be broken; when inserting or changing data, the field string cannot be expanded into multiple lines, otherwise the hard return will be Save to the data;
add an administrator account: grant all on . To user@localhost identified by “password”;
after each sentence is entered, a semicolon';' should be added at the end, or'\g' should also be added Yes;
query time: select now();
query current user: select user();
query database version: select version();
Query the currently used database: select database();

1. Delete the students data table in the student_course database:
rm -f student_course/students.*

2. Back up the database: (Back up the database test)
mysqldump -u root -p test>c:\test.txt
Backup table: (Back up the mytable table under the test database)
mysqldump -u root -p test mytable>c:\test .txt
Import the backup data into the database: (import back to the test database)
mysql -u root -p test

3. Create a temporary table: (create a temporary table zengchao)
create temporary table zengchao(name varchar(10));

4. To create a table, first determine whether the table exists
create table if not exists students(……);

5. Copy the structure of the
table from the existing table create table table2 select * from table1 where 1<>1;

6、复制表
create table table2 select * from table1;

7. Rename the
table alter table table1 rename as table2;

8. Modify the type of column
alter table table1 modify id int unsigned;//Modify the type of column id to int unsigned
alter table table1 change id sid int unsigned;//Modify the name of column id to sid, and modify the attribute to int unsigned

9. Create index
alter table table1 add index ind_id (id);
create index ind_id on table1 (id);
create unique index ind_id on table1 (id);//Create a unique index

10、删除索引
drop index idx_id on table1;
alter table table1 drop index ind_id;

11. Combining characters or multiple columns (connect column id with ":" and column name with "=")
select concat(id,':',name,'=') from students;

12. limit (select 10 to 20) <The number of the first record set is 0>
select * from students order by id limit 9,10;

13. Functions not supported by MySQL:
transactions, views, foreign keys and referential integrity, stored procedures and triggers

14. MySQL will use index operation symbols
<,<=,>=,>,=,between,in, like without% or _

15. Disadvantages of using indexes
1) slow down the speed of adding, deleting and modifying data;
2) occupying disk space;
3) increasing the burden on the
query optimizer ; when the query optimizer generates an execution plan, it will consider the index, too many indexes will give The query optimizer increases the workload, resulting in the inability to select the optimal query plan;

16. Analyze index efficiency
Method: add explain before the general SQL statement;
the meaning of the analysis result:
1) table: table name;
2) type: connection type, (ALL/Range/Ref). Among them ref is the most ideal;
3) possible_keys: query can use the index name;
4) key: actually used index;
5) key_len: the length of the used part of the index (bytes);
6) ref: display column name Or "const" (don't understand what it means);
7) rows: Shows the number of rows that MySQL thinks must be scanned before finding the correct result;
8) extra: MySQL's suggestion;

17. Use shorter fixed-length columns
1) Use shorter data types
as much as possible ; 2) Use fixed-length data types as much as possible;
a) Use char instead of varchar, fixed-length data processing is faster than variable-length data;
b ) For frequently modified tables, disks are prone to fragmentation, which affects the overall performance of the database;
c) In case of data table crashes, tables with fixed-length data rows are easier to reconstruct. Using fixed-length data rows, the starting position of each record is a multiple of the fixed record length, which can be easily detected, but using variable-length data rows is not necessarily;
d) For MyISAM type data tables, Although converting to a fixed-length data column can improve performance, it takes up a lot of space;

18. Use not null and enum
to define the column as not null as much as possible, which can make the data come out faster and require less space, and when querying, MySQL does not need to check whether there is a special case, that is, the null value, so as to optimize Query;
if a column contains only a limited number of specific values, such as gender, whether it is valid or the year of enrollment, etc., in this case you should consider converting it to the value of the enum column. MySQL processes faster because all enum values ​​are in The system is all expressed by the identification value;

19. The use of optimize table
for frequently modified tables is prone to fragmentation, so that more disk blocks must be read when querying the database, which reduces query performance. Tables with variable lengths all have the problem of disk fragmentation. This problem is more prominent for blob data types, because their size changes greatly. You can use optimize table to defragment to ensure that the performance of the database does not decrease, and optimize the data tables affected by fragmentation. The optimize table can be used for MyISAM and BDB data tables. In fact, any defragmentation method is to use mysqldump to dump the data table, and then use the dumped file and rebuild the data table;

20. Using procedure analyse(),
you can use procedure analyse() to show the best type of suggestions. It is very simple to use, just add procedure analyse() after the select statement; for example:
select * from students procedure analyse();
select * from students procedure analyse(16,256); The
second statement requires procedure analyse() not to suggest that it contains more than 16 values ​​or enum types that contain more than 256 bytes. If there is no limit, the output may be very long;

21. Use query cache
1) How the query cache works:
when a select statement is executed for the first time, the server remembers the text content and query results of the query, and stores them in the cache. The result is returned in the cache; when the data table is updated, any cached queries of the data table become invalid and will be discarded.
2) Configure cache parameters:
Variable: query_cache _type, query cache operation mode. There are 3 modes, 0: no cache; 1: cache query, unless it starts with select sql_no_cache; 2: cache only those queries beginning with select sql_cache as needed; query_cache_size: set the size of the maximum result set of the query cache, which is greater than this value Large ones will not be cached.

22. Adjust the hardware
1) Install more memory on the machine;
2) Increase the faster hard disk to reduce the I/O waiting time;
seek time is the main factor determining performance, moving the head literally is the slowest, once Head positioning, read from the track is fast;
3) Redistribute disk activity on different physical hard disk devices;
if possible, put the busiest data inventory on different physical devices, which is the same as using different partitions of the same physical device Are different because they will compete for the same physical resources (heads)

Create and delete MySQL database

1. Create a database

mysql> create database drop_database;
Query OK, 1 row affected (0.00 sec)
2. Delete a database that has been determined to exist

mysql> drop database drop_database;
Query OK, 0 rows affected (0.00 sec)
3. Delete a database that does not exist

复制代码
mysql> drop database drop_database;
ERROR 1008 (HY000): Can’t drop database ‘drop_database’; database doesn’t exist

//An error occurred, the'drop_database' database cannot be deleted, the database does not exist.

mysql> drop database if exists drop_database;
Query OK, 0 rows affected, 1 warning (0.00 sec)//A warning is generated indicating that this database does not exist

mysql> create database drop_database;
Query OK, 1 row affected (0.00 sec)

mysql> drop database if exists drop_database; // if exists to determine whether the database exists, there is no error does not occur
Query OK, 0 rows affected (0.00 sec)
copying the code

Guess you like

Origin blog.csdn.net/AntdonYu/article/details/102894406