Learn the problems and solutions for using mysql. bitsCN.com
Learn the problems and solutions for using mysql
The installation fails at the end and no response is returned. it doesn't matter. The previous installation is successful, but it is not configured.
After the service is disabled, run the following command to enable the service.
Enable the service first
Net start mysql5 -- service name specified during installation
The initial password for connecting to localhost with navicat lite software should be blank and the username is root
In the user after navicat lite login, you can change the root password and click save.
-- It is best to disable mysql5 and enable it again.
In the command:
Mysql-u root-p
Enter the password modified in navicat lite.
-- To add a user pkm with a password of 123456
Grant all on *. * to pkm @ localhost identified by "123456 ";
Use the ip address and user name pkm to remotely connect to the computer mysql. the following error occurs:
1130-host '**-pc' is not allowed to connect to this mysql server
Solution: authorize the pkm user to allow remote connection:
Grant all privileges on *. * TO 'pkm' @ '%' identified by '000000' with grant option;
This is enough.
Some basic mysql commands are as follows:
Start: net start mySql;
Enter: mysql-u root-p/mysql-h localhost-u root-p databaseName;
List databases: show databases;
Select database: use databaseName;
List tables: show tables;
Show columns from tableName;
Create a database: source fileName.txt;
Matching character: Wildcard _ can be used to represent any character, and % 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 to multiple rows. no
Press enter to store the data;
Add an administrator account: grant all on *. * to user @ localhost identified by "password ";
After each statement is entered, enter the plus sign ';' at the end, or add '/g;
Query Time: select now ();
Query the current user: select user ();
Query the 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 to 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 table structure from an existing table
Create table table2 select * from table1 where 1 <> 1;
6. copy a table
Create table table2 select * from table1;
7. rename the table
Alter table table1 rename as table2;
8. modify the column type
Alter table table1 modify id int unsigned; // modify the column id type to int unsigned
Alter table table1 change id sid int unsigned; // modify the column id name to sid and modify the attribute
Int unsigned
9. create an 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. delete an index
Drop index idx_id on table1;
Alter table table1 drop index ind_id;
11. combine characters or multiple columns (link the column id with ":" And column name and "=)
Select concat (id, ':', name, '=') from students;
12. limit (10 to 20 items are selected) <第一个记录集的编号是0>
Select * from students order by id limit 9, 10;
13. functions not supported by MySQL
Transaction, view, foreign key and reference integrity, stored procedure and trigger
14. MySQL uses the index operator symbol
<, <=, >=, >,=, Between, in, like without the start of % or _
15. disadvantages of using indexes
1) slowing down the speed of adding, deleting, and modifying data;
2) occupied disk space;
3) increase the burden on the query optimizer;
When the query optimizer generates an execution plan, it will consider indexes. too many indexes will increase the workload for the query optimizer, resulting in the inability to select the optimal query solution;
16. analyze index efficiency
Method: Add explain before a general SQL statement;
Meaning of analysis results:
1) table: The table name;
2) type: connection type (ALL/Range/Ref ). Among them, ref is the most ideal;
3) possible_keys: query the available index names;
4) key: actually used index;
5) key_len: the length (in bytes) of the used part in the index );
6) ref: displays the column name or "const" (I don't understand what it means );
7) rows: displays the number of rows that MySQL considers to be scanned before finding the correct results;
8) extra: MySQL recommendations;
17. Use a short fixed-length column
1) try to use a shorter data type;
2) use the fixed-length data type as much as possible;
A) replace varchar with char, and process data with a fixed length faster than that with a variable length;
B) for tables that are frequently modified, disks are easy to form fragments, which affects the overall performance of the database;
C) in case of a data table crash, it is easier to reconstruct a table with a fixed length of data rows. Use a fixed length of data rows, each
The starting position of the record is a multiple of the fixed record length, which can be easily detected, but the use of variable-length data rows is not necessarily;
D) for MyISAM data tables, although converting to a fixed-length data column can improve performance, it occupies a large space;
18. use not null and enum
Define the column as not null as much as possible, so that data can be generated faster and less space is required. MySQL does not need to query the data.
Check whether there are special cases, that is, the null value, to optimize the query;
If a column contains only a limited number of specific values, such as gender, validity, or enrollment year, you should consider converting it to the value of the enum column, mySQL processes faster, because all enum values are represented by an identifier value in the system;
19. use optimize table
For tables that are frequently modified, fragments are easily generated, so that more disk blocks must be read during database query to reduce query performance. All tables with variable lengths have disk fragmentation issues. this problem is more prominent for blob data types because of its large size changes. You can use optimize table to organize fragments to ensure that the database performance is not degraded and optimize the data tables affected by fragments.
Optimize table can be used for MyISAM and BDB data tables. In fact, any fragment method uses mysqldump to store data tables, and then creates a new data table using the converted files;
20. use procedure analyze ()
We recommend that you use procedure analyze () to display the optimal type. it is easy to use. add
Procedure analyze (). For example:
Select * from students procedure analyze ();
Select * from students procedure analyze (16,256 );
The second statement requires that procedure analyze () should not contain more than 16 values or enum types containing more than 256 bytes. if there is no limit, the output may be very long;
21. use query cache
1) How to query the cache:
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.
This statement directly returns results from the cache. when a data table is updated, any cache query of the data table becomes invalid,
And is discarded.
2) configure cache parameters:
Variable: query_cache _ type, the operation mode of the query cache. There are 3 moderate modes, 0: No cache; 1: cache query, unless it matches
Select SQL _no_cache; 2: Only queries starting with select SQL _cache are cached as needed;
Query_cache_size: sets the maximum size of the result set cached by the query, which is greater than this value and will not be cached.
22. adjust hardware
1) add more memory to the machine;
2) increase the speed of the hard disk to reduce the I/O wait time;
Track-seeking time is the main factor that determines performance. the speed of moving the head is the slowest word by word. once the head is located, it is fast to read from the track;
3) re-allocate disk activities on different physical hard disk devices;
If possible, the busiest data inventory should be placed on different physical devices, which is different from using different partitions of the same physical device.
Same because they will compete for the same physical resources (head ).
BitsCN.com