zoukankan      html  css  js  c++  java
  • MySQL自带工具使用介绍


    在MySQL中,自带了许多功能比较强大的工具,如mysql、mysqladmin、mysqldump等。下面简单介绍这些工具!

    一、mysql

    Mysql命令是用的最多的一个命令工具了,为用户提供一个命令行接口来操作管理MySQL 服务器。可以通过mysql --help来查看其详细使用方法。

    选项 作用 说明
    -u 指定连接数据库时使用的用户
    -p 指定用户的密码 可以-p后面直接写密码,也可以不写,进行交互式输入密码,推荐后者
    -h 指定要登录的主机 可选,如果为空,则登录本机
    -P 指定要连接的端口 可选,默认是3306
    -e 可以通过-e命令直接执行SQL语句,而不用进入数据库 免交互登录数据库执行SQL语句,通常在脚本中使用
    -D 指定要登录到哪个库 默认不会登录到库,可以省略此选项,直接写库名

    | -E | 查询到的结果以行来显示 |类似于每条SQL语句后面加“G”
    | -f | 即使出现SQL错误,也强制继续 | 比如在不登陆数据库执行删除库的操作会有一个交互式的确认操作,可以使用此选项来避免交互式 |
    | -X | 将查询到的数据导出位xml文件 | 导出的文件在windows系统中可以使用excel表格打开 |
    | -H | 将查询到的数据导出位html文件 | 导出的文件在windows系统中可以使用浏览器打开 |
    | --prompt | 定制自己的MySQL提示符显示的内容| 默认登登录到MySQL后的提示符是“mysql >”,可以使用该选项定制提示符|
    |--tee| 将操作数据库所有输入和输出的内容都记录进文件中|在一些较大维护变更的时候,为了方便被查,可以将整个操作过程中的输出信息保存到某个文件中|

    ① -e、-u、-p、-h、-P、 等选项的使用语法

    [root@db01 ~]# mysql -uroot -p123 -h 192.168.1.1 -P 3306 -e 'show databases;';
    #使用密码为123的root用户,通过3306端口连接192.168.1.1的mysql库,查询mysql库中有哪些表
    mysql: [Warning] Using a password on the command line interface can be insecure.
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | test               |
    +--------------------+
    # -uroot:使用root用户
    # -p123.com:密码是123
    # -h:登录的主机地址是192.168.1.1
    # -P:登录的端口是3306
    # mysql:登录到库名为mysql的库中
    # -e:后面是SQL语句
    

    ② --prompt

    [root@db01 ~]# mysql -uroot -p123 --prompt="\u@\h: \d \r:\m:\s>"
    root@localhost: (none) 03:22:34>
    #u 表示用户名,h 表示主机名,d 表示当前数据库(none表示没有在任何库中)
    #R小时  24小时制 
    小时(12小时制),m分种,s秒,R小时(24小时制)
    

    上述方式每次连接都要写那些字符进行定制,非常麻烦,可以将其写入配置文件中的clinet字段下,之后再登录就可以省略了,如下:

    [root@db01 ~]# vim /etc/my.cnf     #编辑主配置文件
    [mysqld]
                 ..........#省略部分内容
    [client]      #注意写在client字段
    prompt="\u@\h: \d \R:\m:\s> "         #写入该行
    # 注:无需重启MySQL服务,每次客户端连接都会去重新读取该配置
    [root@db01 ~]# mysql -uroot -p123
    root@localhost: (none) 15:26:08>
    

    ③ --tee

    [root@db01 ~]# mysql -uroot -p123 --tee=/tmp/opt.log
    #将打印的信息保存到指定的文件中
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Logging to file '/tmp/opt.log'
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 5
    Server version: 5.7.29 Source distribution
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    root@localhost: (none) 15:27:46>show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | test               |
    +--------------------+
    
    
    [root@db01 ~]# cat /tmp/opt.log
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 5
    Server version: 5.7.29 Source distribution
    
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    root@localhost: (none) 15:27:46>show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | test               |
    +--------------------+
    5 rows in set (0.00 sec)
    
    root@localhost: (none) 15:28:08>exit
    

    同样,“--tee”这个配置项也可以写入my.cnf这个主配置文件中的client字段下,如下:

    [root@db01 ~]# vim /etc/my.cnf
    [client]
    prompt="\u@\h: \d \R:\m:\s>"
    tee=/tmp/opt.log
    #无需重启,即可使用
    

    ④ -H使用方式

    [root@db01 ~]# mysql -H -uroot -p123 -e 'select * from mysql.user;' > a.html
    #将查询的结果重定向输入到a.html文件中
    

    使用浏览器打开下载的文件:
    20200414153834

    二、mysqladmin

    mysqadmin,顾名思义,提供的功能都是与MySQL 管理相关的各种功能。如MySQL Server状态检查,各种统计信息的flush,创建/删除数据库,关闭MySQL Server 等等。mysqladmin所能做的事情,虽然大部分都可以通过mysql连接登录上MySQL Server 之后来完成,但是大部分通过mysqladmin来完成操作会更简单更方便。

    mysqladmin常用命令字:

    命令字 作用
    create databasename 创建一个库
    drop databasename 删除一个库
    status 查询MySQL的基本状态(显示的信息有限 )
    extended-status 查询服务器的详细状态信息(类似于在数据库中执行show status;)
    flush-hosts 刷新服务器缓存
    flush-logs 刷新二进制日志文件(如果二进制日志功能开启,那么执行这个操作会生成新的二进制日志文件)
    flush-status 刷新状态变量
    flush-tables 刷新所有表
    flush-threads 刷新所有线程缓存
    flush-privileges 重新加载授权表
    processlist 查看当前连接数据库的所有ID详细信息
    kill id 杀掉某个或多个连接ID(一般需要先使用processlist查看出ID列表,然后根据ID将其kill掉 )
    ping 检测某个MySQL服务是否处于启动状态
    password 修改用户密码
    shutdown 关闭MySQL服务
    start-slave 开启主从复制
    stop-slave 关闭主从复制
    variables 查询MySQL服务中的所有变量
    version 查询MySQL的版本详细信息

    ① status

    [root@db01 ~]# mysqladmin -uroot -p123 status
    Uptime: 8120  Threads: 1  Questions: 29  Slow queries: 0  Opens: 111  Flush tables: 1  Open tables: 104  Queries per second avg: 0.003
    

    ② ping

    [root@db01 ~]# mysqladmin -uroot -p123 ping         #服务开启时查看
    mysqld is alive
    [root@db01 ~]# systemctl stop mysqld             #停止服务
    [root@db01 ~]# mysqladmin -uroot -p123 ping         #再次查看
    mysqladmin: connect to server at 'localhost' failed
    error: 'Can't connect to local MySQL server through socket '/usr/local/mysql/mysql.sock' (2)'
    Check that mysqld is running and that the socket: '/usr/local/mysql/mysql.sock' exists!
    

    ③ processlist、kill

    [root@db01 ~]# mysqladmin -uroot -p123  processlist
    +----+------+-----------+----+---------+------+----------+------------------+
    | Id | User | Host      | db | Command | Time | State    | Info             |
    +----+------+-----------+----+---------+------+----------+------------------+
    | 5  | lv   | localhost |    | Sleep   | 20   |          |                  |
    | 8  | root | localhost |    | Query   | 0    | starting | show processlist |
    +----+------+-----------+----+---------+------+----------+------------------+
    [root@db01 ~]# mysqladmin -uroot -p123  kill 5
    [root@db01 ~]# mysqladmin -uroot -p123  processlist
    +----+------+-----------+----+---------+------+----------+------------------+
    | Id | User | Host      | db | Command | Time | State    | Info             |
    +----+------+-----------+----+---------+------+----------+------------------+
    | 10 | root | localhost |    | Query   | 0    | starting | show processlist |
    +----+------+-----------+----+---------+------+----------+------------------+
    

    三、mysqldump

    mysqldump这个工具其功能就是将MySQL Server中的数据以SQL 语句的形式从数据库中dump 成文本文件。mysqldump是做为MySQL 的一种逻辑备份工具。

    四、mysqlbinlog

    mysqlbinlog程序的主要功能就是分析MySQL Server 所产生的二进制日志(也就是binlog)。
    通过mysqlbinlog,我们可以解析出binlog中指定时间段或者指定日志起始和结束位置的内容解析成SQL 语句。

    [root@db01 data]# mysqlbinlog binary_log.000012   #指定二进制日志文件即可
    #如果没在日志存放的目录下,应指明绝对路径
    
    *************** 当你发现自己的才华撑不起野心时,就请安静下来学习吧!***************
  • 相关阅读:
    查看资源加载各环节具体耗时的利器
    WebStorm 格式化代码快捷键
    Android 如何使edittext默认失去焦点
    html语义化练习易牛课堂代码
    html网页练习豆瓣网
    HTML前期学习总结
    视频课阶段基础知识总结
    MQ、JMS 关系的理解
    Jvm参数配置
    Java泛型
  • 原文地址:https://www.cnblogs.com/lvzhenjiang/p/14197215.html
Copyright © 2011-2022 走看看