[MySQL | Advanced] 09. MySQL management and the use of common tools (mysqladmin, mysqlbinlog, mysqldump, etc.)

Table of contents

1. System database

2. Commonly used tools 

2.1 mysql

example

2.2 mysqladmin 

example 

2.3 mysqlbinlog

example

2.4 mysqlshow

example

2.5 mysqldump (data backup)

example

2.6 mysqlimport/source (data recovery)

2.6.1 mysqlimport

2.6.2 source


1. System database

After the Mysql database is installed, it comes with the following four databases, the specific functions are as follows:

2. Commonly used tools 

2.1 mysql

The mysql does not refer to the mysqld service, but refers to the client tool of mysql. 

语法 :
    mysql [options] [database]
选项 :
    -u, --user=name           # 指定用户名
    -p, --password[=name]     # 指定密码
    -h, --host=name           # 指定服务器 IP 或域名(不写则默认本地 ip)
    -P, --port=port           # 指定连接端口(不写则默认端口 3306)
    -e, --execute=name        # 执行 SQL 语句并退出

        The -e option can execute SQL statements on the Mysql client without connecting to the MySQL database, which is especially convenient for some batch scripts.

example

mysql -uroot -p1234 itcast -e "select * from stu"

2.2 mysqladmin 

        mysqladmin is a client program that performs administrative operations. You can use it to check the server's configuration and current state, create and drop databases, and more.

# 通过帮助文档查看选项:
[root@mysql01 ~]# mysqladmin --help

语法:
    mysqladmin [options] command ...
选项:
    -u, --user=name             # 指定用户名
    -p, --password[=name]       # 指定密码
    -h, --host=name             # 指定服务器 IP 或域名
    -P, --port=port             # 指定连接端口

example 

# 删除指定的数据库
mysqladmin -uroot –p1234 drop 'test01'

# 查看数据库版本信息
mysqladmin -uroot –p1234 version

2.3 mysqlbinlog

        Since the binary log files generated by the server are saved in binary format, if you want to check the text format of these texts, you will use the mysqlbinlog log management tool. 

语法 :
    mysqlbinlog [options] log-files1 log-files2 ...
选项 :
    -d, --database=name         # 指定数据库名称,只列出指定的数据库相关操作。
    -o, --offset=               # 忽略掉日志中的前 n 行命令。
    -r, --result-file=name      # 将输出的文本格式日志输出到指定文件。
    -s, --short-form            # 显示简单格式, 省略掉一些信息。
    --start-datatime=date1 --stop-datetime=date2 # 指定日期间隔内的所有日志。
    --start-position=pos1 --stop-position=pos2   # 指定位置间隔内的所有日志。

example

A. View the data information in the binary file binlog.000008

        The amount of data in the binary log file viewed above is too large to be inconvenient to query. We can add a parameter -s to display the simple format. 

2.4 mysqlshow

        mysqlshow Client object lookup tool, used to quickly find out which databases, tables in databases, columns or indexes in tables exist. 

语法 :
    mysqlshow [options] [db_name [table_name [col_name]]]

选项 :
    --count     # 显示数据库及表的统计信息(数据库,表 均可以不指定)
    -i          # 显示指定数据库或者指定表的状态信息

示例:
    # 查询 test 库中每个表中的字段书,及行数
    mysqlshow -uroot -p2143 test --count

    # 查询 test 库中 book 表的详细情况
    mysqlshow -uroot -p2143 test book --count

example

A. Query the number of tables in each database and the number of records in the table

mysqlshow -uroot -p1234 --count

B. View the statistics of the database itcast 

mysqlshow -uroot -p1234 itcast --count

C. View the information of the stu table in the database itcast 

mysqlshow -uroot -p1234 itcast stu --count

D. View the information of the id field of the stu table in the database itcast 

mysqlshow -uroot -p1234 itcast stu id --count

2.5 mysqldump (data backup)

        The mysqldump client tool is used to back up databases or perform data migration between different databases. The backup content includes SQL statements for creating and inserting tables. 

语法 :
    mysqldump [options] db_name [tables]
    mysqldump [options] --database/-B db1 [db2 db3...]
    mysqldump [options] --all-databases/-A

连接选项 :
    -u, --user=name               # 指定用户名
    -p, --password[=name]         # 指定密码
    -h, --host=name               # 指定服务器ip或域名
    -P, --port=                   # 指定连接端口

输出选项:
    --add-drop-database           # 在每个数据库创建语句前加上 drop database 语句
    --add-drop-table              # 在每个表创建语句前加上 drop table 语句 , 默认开启; 不开启 (--skip-add-drop-table)
    -n, --no-create-db            # 不包含数据库的创建语句
    -t, --no-create-info          # 不包含数据表的创建语句
    -d, --no-data                 # 不包含数据
    -T, --tab=name                # 自动生成两个文件:一个.sql文件,创建表结构的语句;一个.txt文件,数据文件

example

A. Backup itcast database

# 备份文件在当前目录(也可以写绝对路径到指定目录)
mysqldump -uroot -p1234 itcast > itcast_20230421.sql

You can directly open cat itcast_20230421.sql to see what the backup data looks like. 

The backed up data includes:

  • statement to drop a table

  • statement to create a table

  • data insert statement

        If we do not need to create a table, or do not need to back up data, but only need to back up the table structure during data backup, it can be realized through the corresponding parameters. 

B. Back up the table data in the itcast database without backing up the table structure (-t)

mysqldump -uroot -p1234 -t itcast > itcast02_20230421.sql

Open itcast02_20230421.sql to view the backup data, there is only insert statement, no backup table structure.

C. Backup the table structure and data of the itcast database table separately (-T)

mysqldump -uroot -p1234 -T /root itcast stu

        Executing the above command will cause an error, and the data cannot be backed up. The reason is that the data storage directory /root we specified is considered unsafe by MySQL and needs to be stored in a directory trusted by MySQL. So, which directory is the directory trusted by MySQL? You can check the system variable secure_file_priv. The execution results are as follows: 

        The above two files stu.sql record the table structure file, and stu.txt is the table data file, but you need to pay attention to the table data file, not to record the insert statements one by one, but to record the table structure in a certain format data in . as follows: 

2.6 mysqlimport/source (data recovery)

2.6.1 mysqlimport

mysqlimport is a client-side data import tool, used to import the text files exported by mysqldump with the -T parameter.

Note: You need to delete the original table before importing to succeed. 

语法 :
    mysqlimport [options] db_name textfile1 [textfile2...]

示例 :
    mysqlimport -uroot -p2143 test /tmp/city.txt

2.6.2 source

If you need to import sql files, you can use the source command in mysql (you need to enter the mysql command line to execute): 

语法 :
    source /root/xxxxx.sql

Previous article: [MySQL | Advanced] 08, InnoDB engine architecture, transaction principles and MVCC explanation_Stars.Sky's Blog-CSDN Blog

Next article: [MySQL | Operation and Maintenance] 01. Log_Database Log Management Operation and Maintenance_Stars.Sky's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_46560589/article/details/130269363