mysql official documentation - continually updated

Disclaimer: This article is a blogger original article, welcome to reprint. https://blog.csdn.net/guo_xl/article/details/86751402

Official Documents

installation

  • I am using a binary installation package, which is mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz, download address is https://dev.mysql.com/downloads/mysql/

  • See the installation steps , my steps

shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/share
shell> tar zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
shell> cp mysql-5.7.22-linux-glibc2.12-x86_64 mysql57
shell> cd mysql57
shell> mkdir data
shell> chown mysql:mysql data
shell> chmod 750 data
shell> vi /etc/profile
#输入
export PATH=/usr/share/mysql57/bin:$PATH
#保存退出
shell> source /etc/profile
shell> mysqld --user=root --basedir=/usr/share/mysql57 --datadir=/usr/share/mysql57/data --lc_messages_dir=/usr/share/mysql57/share --lc_messages=en_US &

Vi mysqld above is added to the linux $ PATH in
my mysql-5.7.22-linux-glibc2.12- x86_64.tar.gz placed under / usr / share path

Start the database

Reference Documents

In step installation's

bin/mysqld --user=root --basedir=/usr/share/mysql57 --datadir=/usr/share/mysql57/data --lc_messages_dir=/usr/share/mysql57/share --lc_messages=en_US &

It is launched, & represents the background

It can be used mysqld --verbose --help
to see which parameters are mysqld can take.

But every time you start had to take a bunch of parameters was too much trouble, and therefore provides us with a mysql cnf file can simplify this parameter.

cnf file

Establish a /usr/share/mysql57/my.cnf document reads as follows

[mysqld]
socket=/usr/share/mysql57/data/mysql.sock
user=mysql
basedir=/usr/share/mysql57
datadir=/usr/share/mysql57/data
lc_messages_dir=/usr/share/mysql57/share
lc_messages=en_US
  • [Mysqld] represents the mysqld command
  • The rest of those parameters, it is the key to the start.

With this file, you can use
mysqld --defaults-file=/usr/share/mysql57/my.cnf &directly started.

Omitted cnf file

The above is a cnf file specified by -defaults-file, but in fact cnf file also can be omitted. Because mysqld start time will find several default cnf file location, file locations but where is it? It can be mysqld --verbose --help|grep cnffound as follows

shell> mysqld --verbose --help|grep cnf

Print results are as follows


/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf 
                      my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default

My.cnf placed only at several locations of the above, such as

mv /usr/share/mysql57/my.cnf /etc/my.cnf

Then mysqld &start

Close the database

mysqladmin shutdown -uroot -pdevuser
-S/usr/share/mysql57/data/mysql.sock

Of course -u -p -S can be configured to cnf file

[mysqladmin]
socket=/usr/share/mysql57/data/mysql.sock

Connect to the database

mysql -uroot -proot

Find

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'

Here you need to specify a file sock, because of our my.cnf file to specify the socket mysqld

[mysqld]
socket=/usr/share/mysql57/data/mysql.sock

So mysql also need to specify the socket, as follows

mysql -uroot -pdevuser -S/usr/share/mysql57/data/mysql.sock

But so designated would be too much trouble, and the parameters of mysql actually can be configured in my.cnf, the following

[mysql]
socket=/usr/share/mysql57/data/mysql.sock

So do not specify -S.

How do you know which parameters are mysql can take? Like mysqld command, you can
mysql --helpview

Via a remote database connection

The above operations are present in the linux operating if the connection to the remote, the error will be reported

Product:  DbVisualizer Pro 9.0.2
Build:  #1912 (2012/12/18 10:21)
Java VM:  Java HotSpot(TM) 64-Bit Server VM
Java Version:  1.8.0_131
Java Vendor:  Oracle Corporation
OS Name:  Windows 10
OS Arch:  amd64
OS Version:  10.0

An error occurred while establishing the connection:

Long Message:
null, message from server: "Host '192.168.1.101' is not allowed to connect to this MySQL server"

Details:
   Type: java.sql.SQLException
   Error Code: 1130
   SQL State: HY000

Solution

//允许用户root从任何主机连接到mysql服务器的话。 
 
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'devuser' WITH GRANT OPTION; 
 
FLUSH   PRIVILEGES; 
 
//允许用户root从ip为192.168.1.88的主机连接到mysql服务器
 
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.88' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;     
 
FLUSH   PRIVILEGES; 

Baidu before encountered such problems are, in fact, is to look from above to baidu, but in fact this involves account management mysql.

Account Management

Reference Documents

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

Mysql-level system which is used to manage accounts,
mysql user will exist mysql.user this table. How to find the user's permission to do that?

About CRUD user reference documentation already very detailed. Compare concern is the need to give permission to the user.

Give permission

Do not ever give anyone (except MySQL root accounts) access to the user table in the mysql system database!

mysql> CREATE USER 'test' IDENTIFIED BY 'test';
mysql> GRANT ALL PRIVILEGES ON test.* TO 'test'@'%';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON test.* TO 'test'@'%';
Competence Explanation
SELECT Look-up table
INSERT Insert table
UPDATE Update table
DELETE Delete table
CREATE To build the table
DROP Delete library

More detailed instructions

Queries Account Privileges

eg:

SHOW GRANTS FOR 'joe'@'office.example.com';

SHOW GRANTS FOR 'joe'@'home.example.com';

Note that the account must be a user name @ host name to joint development authority, that is to say 'joe' from ' office.example.com login and from' on ' home.example.com ' is likely to have different permissions.

Journal

Log Type Information Written to Log
Error log Problems encountered starting, running, or stopping mysqld
General query log Established client connections and statements received from clients
Binary log Statements that change data (also used for replication)
Relay log Data changes received from a replication master server
Slow query log Queries that took more than long_query_time seconds to execute
DDL log (metadata log) Metadata operations performed by DDL statements
Configuration log-error
[mysqld]
log-error=/usr/share/mysql57/logs/log_err.log
mkdir usr/share/mysql57/logs/
chown -R usr/share/mysql57/logs/
Configuration slow queries

show vaiables like ‘slow%’
Here Insert Picture Description

set global slow_query_log=on; #开启慢查询日志
set global slow_query_log_file='d:/logs/mysql/query_slow_log.log'; #指定日志文件路径
set global long_query_time=3; # 单位是秒
set global log_queries_not_using_indexes=true;记录未使用索引的sql

Transaction submitted to the redo log, then refresh from the redo logs to disk.

Database Recovery

Setting configuration parameters mysql

  • Global Parameters
set global wait_timeout=3600;
set global interactive_timeout=3600;
  • Session parameters
set session wait_timeout=3600;

mysql optimization

Most MYSQL index (PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT) village become B-tree.

Using the index =,>,> =, < , <=, or BETWEEN
noted that like

The following statement is to use index of less than

SELECT * FROM tbl_name WHERE key_col LIKE '%Patrick%';
SELECT * FROM tbl_name WHERE key_col LIKE other_col;

The following statement is to use index

SELECT * FROM tbl_name WHERE key_col LIKE 'Patrick%';
SELECT * FROM tbl_name WHERE key_col LIKE 'Pat%_ck%';

Leftmost match

meaning explain output
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/guo_xl/article/details/86751402