Advanced management of mysql database(3)

One: mysql user management

For security, the root user of msyql is not allowed to log in remotely by default.

1.1: View all users

mysql> select user,host,password from mysql.user;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
| root | localhost             | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | localhost.localdomain |                                           |
| root | 127.0.0.1             |                                           |
| root | ::1                   |                                           |
|      | localhost             |                                           |
|      | localhost.localdomain |                                           |
+------+-----------------------+-------------------------------------------+
6 rows in set (0.00 sec)

1.2: New users

create user 用户名@'地址' identified by'密码';

For example:

mysql> create user huazai@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

1.3: Password modification

1.3.1: Method One

update user set password=password('新密码') where host='主地址' and user='用户名';
flush privileges;//刷新权限 

For example:

mysql> update user set password=password('123456') where user='huazai';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

刷新授权表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

1.3.2: Method Two

set password for 用户名@'主机地址' =password('新密码');             //注这种方法无需刷新权限

For example:

mysql> set password for huazai@'localhost'=password('123');
Query OK, 0 rows affected (0.00 sec)

1.4: Recovery after forgetting the root password

  1. Stop the mysql service first

    systemctl stop mariadb
    
  2. Skip the grant table and start the MySQL service.
    This step mainly uses the --skip-grant-tables option of mysqld to
    modify the my.cnf configuration and add skip_grant_tables=1 to start the setting:

    Open the /etc/my.cnf configuration file and
    add a line of skip_grant_tables, then save and exit

    [root@localhost mysql]# cat my.cnf |grep skip
    skip_grant_tables=1
    
  3. Reset root password

    mysql> update mysql.user set password=password('123') where user='root';
    Query OK, 3 rows affected (0.01 sec)
    Rows matched: 4  Changed: 3  Warnings: 0
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.01 sec)
    
    

1.5: Permission settings for database users

1.5.1: Authority level

The permissions can be divided into four levels:

  1. Global level (*.*)
  2. Database level (database name.*)
  3. Table level (database name. table name)
  4. Column level (authority (column) database name. table name).
    Global level permissions are stored in the mysql.user table.
    Database level permissions are stored in mysql.db or mysql.host.
    Table level permissions are stored in mysql.tables_priv.
    Column level permissions are stored in mysql.columns_priv.
1.5.2: Authorize users
grant  权限列表 on 数据库名.表名 to 用户@'地址';
1.5.3: Create users while authorizing
grant  权限列表 on 数据库名.表名 to 用户@'地址' identified by'密码';
mysql> grant select on mysql.* to 'huazai007'@'192.168.1.%' identified by '123';
Query OK, 0 rows affected (0.03 sec)

1.5.4: Withdraw permissions

View user permissions:
show grants for user name;

mysql> show grants for 'huazai007'@'192.168.1.%';
+--------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected].%                                                                                   |
+--------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'huazai007'@'192.168.1.%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT SELECT ON `mysql`.* TO 'huazai007'@'192.168.1.%'                                                             |
+--------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

To revoke the user's permissions:

revoke 权限列表 on 数据库名.表名 from 用户@'地址';
mysql> revoke select on mysql.* from 'huazai007'@'192.168.1.%';
Query OK, 0 rows affected (0.01 sec)

Two: backup and recovery of mysql database

2.1: Concept and classification of backup

2.1.1: Backup concept

In order to cope with possible unexpected situations such as file or data loss or damage, the data in the electronic computer storage device is copied to a large-capacity storage device such as a magnetic tape, so as to separate the program or file copy separately stored in the original text;

If the system's hardware or storage media fails, "backup" can help us protect data from accidental losses.

2.1.2: Backup classification

  1. Full backup: A
    full backup refers to a complete copy of all data or applications at a certain point in time
  2. Incremental backup:
    Incremental backup means that after a full backup or the last incremental backup, each subsequent backup only needs to back up the files that have been increased and modified compared to the previous one.
  3. Differential backup:
    Differential backup refers to the backup of those added or modified files during the period from a full backup to the differential backup

2.2: Backup tool

2.2.1:mysqldump

mysqldump is a backup tool that comes with mysql, which completes a full backup

Example: backup all databases

mysqldump -uroot --p password --all-databases> drive letter:\path\file name.sql

-A is equivalent to --all-databases

 /usr/local/mysql/bin/mysqldump -uroot -p123 -A -S /var/lib/mysql/mysql.sock > /tmp/all_db.sql

Example: backup a (some) specified database (test+mysql)

mysqldump -uroot -p password -databases database name>disk letter:\path\file name.sql

-B is equivalent to -databases

[root@localhost mysql]# /usr/local/mysql/bin/mysqldump -uroot -p123 --databases test mysql -S /var/lib/mysql/mysql.sock > /tmp/test_mysql.sql
Warning: Using a password on the command line interface can be insecure.

Example: Back up a table in a specified database (mysql.user)

mysqldump -uroot -p password database name table name>disk letter:\path\file name.sql

[root@localhost mysql]# /usr/local/mysql/bin/mysqldump -uroot -p123  mysql user -S /var/lib/mysql/mysql.sock > /tmp/mysql_user.sql
Warning: Using a password on the command line interface can be insecure.

2.2.2:mydumper

Mydumper is a high-performance multi-threaded backup and recovery tool for MySQL.

2.2.2.1: Main features of Mydumper:
  • Written in lightweight C language
  • The execution speed is 10 times faster than mysqldump
  • Fast file compression
  • Support export binlog
  • Multi-threaded recovery (applicable to version 0.2.1 and above)
  • Working as a daemon, timed snapshots and continuous binary logs (applicable to versions above 0.5.0)
  • Open source (GNU GPLv3)
2.2.2.2: Mydumper installation:

download:

wget https://github.com/maxbube/mydumper/releases/download/v0.9.5/mydumper-0.9.5-2.el7.x86_64.rpm
yum -y install  glib2-devel zlib-devel pcre-devel
yum -y install mydumper-0.9.5-2.el7.x86_64.rpm

Version view:

[root@localhost mysql]# mydumper  -V
mydumper 0.9.5, built against MySQL 5.7.21-21

2.2.2.3: Mydumper parameter description

-B,
--database The library to be backed up -T, --tables-list The tables to be backed up, multiple tables, separated by commas
-o, --outputdir The directory of the output file
-s, --statement-size Generate insert statements The number of bytes, the default is 1000000
-r, --rows split into many rows of blocks. Table
-c, --compress Compress output files
-e, --build-empty-files Even if the table has no data, an empty file is generated
-x , --regex Regular expression:'db.table'
-i, --ignore-engines Ignored storage engines, separated by commas
-m, --no-schemas do not export table structure
-k, --no-locks no Execute shared read lock warning: this will cause inconsistent backups
-l, --long-query-guard set long query time, default 60 seconds
-kill-long-queries kill long-executed queries
-b, --binlogs export binlog
-D, --daemon enable daemon mode
-I, --snapshot-interval dump snapshot interval time, default 60s, need to be in daemon mode
-L, --logfile log file
-h, --host The host to connect to
-u, --user Username with privileges to run the dump
-p, --password User password
-P, --port TCP/IP port to connect to
-S, --socket UNIX domain socket file to use for connection
-t, --threads The number of threads used, the default is 4
-C , --compress-protocol Use compression protocol on mysql connection
-V, --version Show the program version and exit
-v, --verbose More output, 0 = silent, 1 = errors, 2 = warnings, 3 = info , default 2

2.2.2.4: Example of mydumper

By default, 4 thread derivatives are used. Generally, enterprise servers recommend 8 or more. Non-transaction engines will lock the table. It is recommended to export using slave and use compression.
Note: There is a space after the parameter -u xx to
backup the entire database
mydumper -u root -p'xxx'- h xxx -o /xxx/gw
(The backup directory will be created automatically!)

Back up the table without backing up the table structure
mydumper -u root -p'xxx' -h xxx -B gw -T testA -m -o /xxx/gw

Back up the specified database
mydumper -u root -p'xxx' -h xxx -B gw -t 6 -o /xxx/gw

Back up multiple tables (tableA, tableB)
mydumper -u root -p'xxx' -h xxx -B gw -T testA,testB -t 6 -o /xxx/gw

Back up the data in tableA and compress it
mydumper -u root -p'xxx' -h xxx -B gw -T testA -t 6 -c -o /xxx/gw

Mydumper data recovery:
myloader -u root -p'xxxxx ' -B test -d /home/mysql/

2.2.3: [xtrabackup] (understand)

xtrabackup official website
Insert picture description here

2.2.4: Data recovery

method one:

mysql -u root -p password library or table <backup file

Method Two:

Or log in to mysql
source backup drive letter:\path\filename.sql

Method three:

mydumper: export data
myloader: restore data
myloader -u root -p'xxxxx ' -B test -d /home/mysql/

Three: mysql log management

3.1: Log type

3.1.1: Error log:

Record problems when starting, running, or stopping mysqld.

3.1.2: Binary log:

The log file records various operations of the database in binary form, but does not record query statements.
The master-slave replication of the database is also based on binlog to synchronize data.

3.1.2.1: Check whether the binary log is enabled:
show variables like 'log%';

Insert picture description here

3.1.2.2: Get the current binary log list:
mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |     69459 |
| mysql-bin.000002 |   1377550 |
| mysql-bin.000003 |       143 |
| mysql-bin.000004 |      9796 |
| mysql-bin.000005 |       916 |
+------------------+-----------+
5 rows in set (0.00 sec)

3.1.2.3: Check which log is being used:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 |      916 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3.1.2.4: View the contents of the binary file: (mysqlbinlog tool)

Common options for the mysqlbinlog command:

--Start-postion start position
--stop-postion end position --start
-datetime'yyyy-mm-dd hh:mm:ss' start time
--stop-datetime'yyyy-mm-dd hh:mm:ss' end time

mysqlbinlog --no-defaults --start-position=106 --stop-position=272  /var/lib/mysql/mysql-bin.000001 
[root@localhost ~]# mysqlbinlog  --start-datetime='2020-10-08 21:10:38' --stop-datetime='2020-10-08 21:20:01' /data/mysql/mysql-bin.000005

3.1.2.5: Use the binary log to restore data:

When backing up the mysql database, backing up the related binary log backup is an incremental backup of mysql , and the binary log can be incrementally restored when restoring.

3.1.2.5.1: Restore by time

Experiment process:
1: Insert a piece of data first
2: Delete this piece of data
3: Find the pos start and end position parameters of the input data, and restore the data!

mysqlbinlog --start-datetime=“时间” 日志文件路径 | mysql -u用户 -p             //指定开始恢复的时间节点

mysqlbinlog --stop-datetime=“时间” 日志文件路径 | mysql -u用户 -p 					//指定结束恢复的时间节点

mysqlbinlog --start-datetime=“时间” --stop-datetime=“时间” 日志文件路径 | mysql -u用户 -p   //从规定的起始时间还原到规定的结束时间
3.1.2.5.2: Restore by location
mysqlbinlog --start-position=“位置” 日志文件路径 | mysql -u	用户 -p   //从规定的起始位置还原到现在

mysqlbinlog --stop-position=“位置” 日志文件路径 | mysql -u	用户 -p   //从最开始还原到规定的结束位置

mysqlbinlog --start-position=“位置” --stop-position=“位置” 日	志文件路径 | mysql -u用户 -p   //从规定的起始位置还原到规定的结束位置

3.1.3: Slow query log (understand)

slow_log
MySQL's slow query log is a log record provided by MySQL. It is used to record statements in MySQL whose response time exceeds the threshold. Specifically, SQL with a running time exceeding the long_query_time value will be recorded in the slow query log.

The default value of long_query_time is 10, which means to run the statement more than 10S.

By default, Mysql database does not start slow query log, we need to manually set this parameter.

Of course, if it is not required for tuning, it is generally not recommended to enable this parameter, because turning on the slow query log will more or less bring about a certain performance impact.

The slow query log supports writing log records to files and also supports writing log records to database tables.

Guess you like

Origin blog.csdn.net/zhangshaohuas/article/details/108976646