mysql combat summary

Application-level database operations mainly need to know are:

Installation and start connecting to the database, modify permissions management password management database
backup and recovery databases, full backup, incremental backup and physical backup, logical backup of
high availability deployment database: clustered
database optimization: Optimizing database operating system, the hardware layer, database software configuration level of optimization, optimization sql statement level, optimize data index level.

Install the database:
you can download the official website has been compiled package, environmental CentOS
yum install: mysql-community
first download package npm
install npm package: rpm -Uvh mysql57-community-release -el7-9.noarch.rpm
install mysql: yum install mysql-community-server
start mysql: systemctl start mysqld
after the initial installation, change the root password
after starting the service, the root user creates a temporary password stored in /var/log/mysqld.log inside, you can
grep 'temporary password' / var / log / mysqld.log view
then mysql -uroot -p log
login password if there is a temporary special symbols can be used in single quotes' myPassword '
and
ALTER USER' root '@' localhost 'IDENTIFIED bY' MyNewPass4 ';!
refresh permissions: flush privileges;

A user authorized to access the database
grant all privileges on database tables to 'username' @ 'ip' identified by 'password'
grant all privileges on. To 'root' @ '%' IDENTIFIED by 'password' // percentile sign indicates any address

-

Backup and restore
using mysqldump backup:
Using sql format backup: mysqldump --all-Databases> my.sql
mysqldump --databases DB1 DB2> my.sql
by reloading the data file sql: mysql <my.sql or mysql> source my. sql
using backup data delimiter:
the mysqldump --tab = / tmp DB1 two formats will save two files A sql txt one kind of
parameters: - fields-terminated-by = str, - fields-enclosed- by = char, - fields-optionally -enclosed-by = char, - fields-escaped-by = char, - lines-terminated-by = str
recovery backup file delimiter:
MySQL DB1 <t1.sql
the mysqlimport db1 t1.txt
or
MySQL> DB1 use;
MySQL> Load Data INFILE 't1.txt' INTO Table T1;

How to copy a database
mysqldump db1> dump.sql
MySQL db2 <dump.sql
Do not use mysqldump --database, which can lead to use db1 statement exists dump file, overwriting the database name of db2.
How to copy a database server to another server:
mysqldump --databases db1> dump.sql
in two server: mysql> dump.sql
use --databases is because it will create database contains statements
How to back up stored procedures (stored procedures, functions, triggers, events)
plus parameters --events --routines (stored procedures and functions) --triggers
how to store data definitions and separated:
--no-the data does not copy table data, - no-create-info:
upgrade compatibility testing:
the mysqldump --all --no-Data-Databases --routines --events> the dump-defs.sql
MySQL <defs.sql the dump-in upgraded server

Using the binary log point in time recovery (incremental recovery):
View the binary log list: show binary logs
to see the name of the binary log: show master status
restore the binary log: mysqlbinlog mylog | mysql-uroot -p
view the binary log information: mysqlbinlog mylog | more
practice if you want to handle multiple binary log each file separately when a statement is a dangerous, should be performed in a single statement
mysqlbinlog log1 log2 log3 | mysql -uroot -p
or the use of additional ways:
mysqlbinlog log1> my.sql
mysqlbinlog log2 >> my.sql
then use: mysql -uroot -p -e "source my.sql "
You can use --skip-gtids ignore global transaction
recovery time point:
use --start-datetime and to --stop-datetime specify the recovery time:
mysqlbinlog --stop-datetime = "2017-04-20 9:59:59" MyLog | -p-uroot-MySQL
mysqlbinlog --start-datetime = "2017-04-20 9:59:59" mylog | mysql -uroot -p
use --start-position and --stop-position to specify the recovery position:
mysqlbinlog --stop-position=154 mylog | mysql -uroot -p
mysqlbinlog --start-position=209 mylog | mysql -uroot -p

Master-slave configuration
principle is to use binary log, two from a master, the master server before data is written to a storage binlog record, which contains the data recording operation, the record will be synchronized to the server, then the server starts from sql thread these records into the database synchronization
operation:
1: my.cnf are arranged from the master file server, add the server-id = 1, server- id = 2, server-id = 3 ....
Add the primary node log -bin = mylog, open binaries
2: adding a user copy with the master node on
grant replication slave on to 'repl' @ '%' identified by 'myPassword';.
See master node status: show master status; appears log file name and position
. 3: 3306 to open the respective port server:
Firewall-cmd = --zone public --add-port = 3306 / TCP --permanent
Firewall-cmd --reload
. 4: in each input node from the
change master to
master_host = 'master node ip', // there must be a comma
MASTER_USER = 'the repl',
master_password = 'as myPassword',
MASTER_LOG_FILE = 'mylog.000001', // file name here is the view from the master node to the file name
master_log_ pos = 154;
After the successful use of start slave; to boot
and then view from node information: show slave status \ G, if
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
both as it indicates normal, if it is otherwise possible there is something wrong configuration, viewing port, log_pos location.
From the master copy of the specific configuration of the my.cnf
Server-ID. 1 =
// binary log log-bin = / var / log / mysql / mysql-bin storage position
binlog-do-db = test // database to be synchronized
binlog- ignore-db = mysql // not logging library, library i.e. does not require synchronization of
log-slave-updates // open from logging on node
sync_binlog = 1 // 1 through log writes to a log file written to disk once, the safest but the least efficient, the default is 0
#auto_increment // control the behavior of auto-increment

Optimization
single table optimization:
Field:
1: make use tinyint, smallint, medium_int integer type and not as INT, plus a non-negative UNSIGNED
2: the length of the space allocated only required varchar
3: Instead of using the integer string or an enumeration type
4 : use timestamp, without using a datetime
. 5: single field suggestions under 20
6: integer to keep ip
index:
1: consider indexing and where in order by column
2: the value of the field distribution is not suitable for building sparse index
3: primary key is not a character field
4: no foreign keys, by the program to ensure that the constraint
5: not UNIQUE, constrained by the program to ensure
optimization of the efficiency sql statement:
1: to select a smaller column
2: with where the more frequent the field index
3: * avoid using the select clause
4: avoiding the use of calculation, not, in columns in the index and <> operation
5: data line only when needed. 1 using the limit
. 6: guaranteed form data no more than 200W, timely separate table

Do not do the column operation, any operation on the column were led to a table scan, the query operation to the right of the equal sign. sql statement as simple as possible, a sql can only run on a cpu, big statement dismantle small statement. or change in, or efficiency level is n, in the efficiency of log (n) level, in the number of recommendations 200

  • Row subqueries: a plurality of columns may be used as the determination condition
    select * from t1 where (col1, col2) in (select col3, col4 from t2)

exist: If the sub-query has returned, the data is returned, if there is no sub-query does not return content

Optimization subquery:
1: Use can affect the subquery row number or sequence query, is to let the subquery filter Multicast data
2: using a subquery to replace join connection
3: the outer statement into the internal subquery
SELECT FROM t1
WHERE s1 IN (SELECT s1 FROM t1 UNION ALL SELECT s1 FROM t2);
alternatively:
the SELECT the FROM T1
the WHERE S1 the IN (the SELECT S1 the FROM T1) the IN OR S1 (the FROM T2 the SELECT S1);
Another example:
the SELECT (the FROM. 5 the SELECT column1 + t1) FROM t2;
alternatively
the SELECT (the SELECT column1 the FROM T1) +. 5 the FROM T2;
. 4: use a row query instead of a correlated subquery:
the SELECT the FROM T1
the WHERE (column1, Column2) the IN (the SELECT column1, Column2 the FROM T2);
alternatively :
the SELECT the FROM T1
the WHERE EXISTS (the WHERE t2.column1 the SELECT * T2 = the FROM t1.column1
the AND t2.column2 = t1.column2);
. 5: use not (a = any ()) than a <> all () good
6: x = any (table containing (1,2 )) ratio of x = 1 or x = 2 Good
7: Use = any better than EXISTS
. 8: If returns only one record, then the use of better than in =

explain Query Analyzer
explain the contents:
the above mentioned id SELECT_TYPE the Table of the type possible_keys Key key_len ref rows Extra

id: execution number, or if no nested sub-queries, there is only select, then each line displayed 1
SELECT_TYPE: Bank display query types:
the Simple: simple subquery does not contain sub-queries and union
Primary: contain or sub-union query
subquery: subquery
derived: derived table
of Union:
of Union the Result: anonymous temporary table
dependent of Union:
subquery:
dependent subquery:
the table: corresponding row relief access table name or alias
type: access type:
All: full table scan
index: scanning table Xun this time by index scan instead of a progressive
range: range scan, key column shows which index is used. Use =, <>,>, "=", <=, can be used when BETWEEN or the I Range
REF: index access, returns OK All individual values of all matched, only the use of non-unique index or a unique index non-unique It will occur when sex prefix.
eq_ref: occurs when the query uses a unique index or primary key
const: only up to determine the time line matching, mysql optimizer will read and read only once before the query
system: one kind of special columns const connected, only one row table to meet conditions
Null: performing access node even less than the table or index (efficient)
possible_keys: show what the use of the index
key: key (index) actually decide to use, if not selected index was Null
key_len: use the key length, the length as short as possible
ref: Which column or virtuous Changshu, together with the key to select rows from a table using
the number of rows to execute the query must be checked, which is a value estimate: rows
Extra: Important supplementary information:
using filesort: use the index to sort
using temporary: the temporary table to hold intermediate results were
not exists: once a match is no longer a left join search
using index: use covering indexes do not need to read the data file, at the same time if there is a using where, showed that
using conditon index:
a using the WHERE:
a using the Join Buffer:
Impossible the WHERE:
the SELECT Optimized the Tables Away:
DISTINCT:

Guess you like

Origin blog.51cto.com/14462428/2423514