mysql命令行工具
mysql包相关命令行工具 [root@manage ~]# rpm -qa|grep mysql
mysql-server-5.1.73-5.el6_7.1.x86_64
mysql-5.1.73-5.el6_7.1.x86_64
mysql-connector-java-5.1.17-6.el6.noarch
mysql-libs-5.1.73-5.el6_7.1.x86_64
[root@manage ~]# rpm -ql mysql
/usr/bin/msql2mysql 没多大用
/usr/bin/my_print_defaults 没多大用
/usr/bin/mysql 有用
/usr/bin/mysql_config
/usr/bin/mysql_find_rows 比grep能更近一步,将有关系的块组织在一起,显示出来,而grep只是将匹配行过滤出来而已
/usr/bin/mysql_waitpid 没多大用
/usr/bin/mysqlaccess 没多大用
/usr/bin/mysqladmin 非常有用
/usr/bin/mysqlbinlog 有用
/usr/bin/mysqlcheck 没多大用
/usr/bin/mysqldump 有用
/usr/bin/mysqlimport 有用
/usr/bin/mysqlshow 不太用
/usr/bin/mysqlslap 不太用
连接方式与客户端的不一样,在相关输出中会有明确的表示,此时深刻理解了客服模型。
服务器唯一,但不同版本也可多样。客户端多种多样。连接方式socket,tcp/ip,tunnel,请看下面的输出,可以看的更清楚一点。
centos7 远程连接tcp/ip [root@kvm1 ~]# rpm -qa|grep mari
mariadb-libs-5.5.47-1.el7_2.x86_64
marisa-0.2.4-3.el7.x86_64
mariadb-5.5.47-1.el7_2.x86_64
[root@kvm1 ~]# rpm -ql mariadb
[root@kvm1 ~]# mysql -p -h 192.168.10.110
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.1.73 Source distribution Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> status
--------------
mysql Ver 15.1 Distrib 5.5.47-MariaDB, for Linux (x86_64) using readline 5.1 Connection id: 20
Current database:
Current user: root@192.168.10.101 centos6.5,本地连接unix socket连接
[root@vm1 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.1.73 Source distribution
mysql> status
--------------
mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1 Connection id: 24
Current database:
Current user: root@localhost 如下所示,说明有三个数据库连接,其中一个本地连接unix socket,两个远程tcp/ip连接,time以秒计算
mysql> show processlist;
+----+------+----------------------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+----------------------+------+---------+------+-------+------------------+
| 28 | root | 192.168.10.101:58498 | NULL | Sleep | 88 | | NULL |
| 33 | root | 192.168.10.108:50105 | NULL | Sleep | 68 | | NULL |
| 34 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+----------------------+------+---------+------+-------+------------------+
3 rows in set (0.00 sec) 如下所示,有一个tcp/ip,两个本地unix socket,其中36510是通过ssh tunnel转发过来的
MySQL [test]> show processlist;
+----+------+----------------------+-------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+----------------------+-------+---------+------+-------+------------------+
| 28 | root | 192.168.10.101:58498 | test | Query | 0 | NULL | show processlist |
| 34 | root | localhost | mysql | Sleep | 112 | | NULL |
| 35 | root | localhost:36510 | NULL | Sleep | 4 | | NULL |
+----+------+----------------------+-------+---------+------+-------+------------------+ 这个id(即connection id)的数量是一直在增加的
[root@10-9-71-105 ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17135
Server version: 5.7.10-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial) mysql> status
--------------
mysql Ver 14.14 Distrib 5.7.10, for Linux (x86_64) using EditLine wrapper Connection id: 17130
Current database:
Current user: root@localhost [root@10-9-71-105 ~]# mysqladmin status -p
Enter password:
Uptime: 3129296 Threads: 50 Questions: 58828358 Slow queries: 0 Opens: 77917 Flush tables: 1 Open tables: 400 Queries per second avg: 18.799 这一个字段代表当前共有50条连接
Threads: 50
其实就是通过下面两条命令显示的详细,二者是等价的
[root@10-9-71-105 ~]# mysqladmin proc stat -p
mysql> show processlist; win7 heidisql 远程连接,通过 ssh tunnel连接 /* 连接到 127.0.0.1 (经由 MySQL (SSH tunnel)),用户名 root,密码:Yes ... */
/* 尝试创建plink.exe进程,响应等待 4 秒... */
/* D:\program\PuTTY\plink.exe -ssh root@192.168.10.110 -pw "******" -P 22 -N -L 3307:127.0.0.1:3306 */
SELECT CONNECTION_ID();
/* 已连接。线程ID:26 */
/* Unknown character set: 'utf8mb4' */
/* 字符集: utf8 */
mysql与mysqlimport工具配合使用
[root@manage ~]# mysql -e 'CREATE TABLE imptest(id INT, n VARCHAR(30))' fgy -p
Enter password:
在fgy库中创建表imptest,两列 [root@manage ~]# cat imptest.txt
100 hello world
200 ni hao [root@manage ~]# mysqlimport --local fgy imptest.txt -p
Enter password:
fgy.imptest: Records: 2 Deleted: 0 Skipped: 0 Warnings: 4
[root@manage ~]# mysql -e 'select * from imptest' fgy -p
Enter password:
+------+------+
| id | n |
+------+------+
| 100 | NULL |
| 200 | NULL |
+------+------+
之所以产生警告是因为imptest.txt里分隔符不是tab,而是空格,改成tab就可以了
#man mysql中的解释
When used interactively, query results are presented in an ASCII-table format.
When used noninteractively (for example, as a filter), the result is presented in tab-separated format.
[root@manage ~]# mysqlimport --local fgy imptest.txt -p
Enter password:
fgy.imptest: Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
[root@manage ~]# mysql -e 'select * from imptest' fgy -p
Enter password:
+------+-------------+
| id | n |
+------+-------------+
| 200 | ni hao |
| 100 | hello world |
+------+-------------+ The following example demonstrates tabular versus nontabular output and the use of raw mode to disable escaping:
mysql
mysql -s -r
上面两条命令是不一样的 Then type an SQL statement, end it with “;”, \g, or \G and press Enter.
ego, \G
Send the current statement to the server to be executed and display the result using vertical format.
my_print_defaults groups(groups代表多个/etc/my.cnf中的[]),非常方便,不用去看文件,但是好像用处也不是太大,如果配置项不多的话,直接vi /etc/my.cnf一眼就看完了,也就不需要在命令行敲入那么多字母了。
[root@manage ~]# my_print_defaults mysqld
--datadir=/var/lib/mysql
--socket=/var/lib/mysql/mysql.sock
--user=mysql
--symbolic-links=0
--innodb_rollback_on_timeout=1
--innodb_lock_wait_timeout=600
--max_connections=350
--log-bin=mysql-bin
--binlog-format=ROW
--character_set_server=utf8
mysqldump
这一段代码太不优化了,多余-重复
不需要sed来添加行,只需要加入 --add-drop-database就可以了
有--database或-A,添加--add-drop-database才有效。
还是要多看看 #man mysqldump手册页。了解具体需求,才有针对性。 drop database if exists spauth;
create database spauth;
use spauth; mysqldump -u root -d -R --add-drop-table basedata >basedata.sql
sed -i '1i\use basedata;' basedata.sql
sed -i '1i\create database basedata;' basedata.sql
sed -i '1i\drop database if exists basedata;' basedata.sql
mysqldump -u root --add-drop-table basedata industry>>basedata.sql
mysqldump -u root --add-drop-table basedata data_dictionary>>basedata.sql
mysqldump -u root --add-drop-table --extended-insert=false basedata tb_sequence>>basedata.sql
-e, --extended-insert,长INSERT,多row在一起批量INSERT,提高导入效率,和没有开启 -e 的备份导入耗时至少相差3、4倍,默认开启;用--extended-insert=false关闭。强烈建议开启,通过下面的测试比较就会明白为什么了。
(1)默认方式导出,也即--extended-insert=true -d只有表结构即列信息,无内容即行信息 · --no-data, -d Do not write any table row information (that is, do not dump table contents). This is
useful if you want to dump only the CREATE TABLE statement for the table (for example,
to create an empty copy of the table by loading the dump file). · --routines, -R Include stored routines (procedures and functions) for the dumped databases in the
output. Use of this option requires the SELECT privilege for the mysql.proc table. The
output generated by using --routines contains CREATE PROCEDURE and CREATE FUNCTION
statements to re-create the routines. However, these statements do not include
attributes such as the routine creation and modification timestamps. This means that
when the routines are reloaded, they will be created with the timestamps equal to the
reload time.
mysqlbinlog
因为每次操作的时间和“位置”都会被记录下来。所以要想还原数据有两种途径通过“时间”或“位置”。 [root@manage mysql]# pwd
/var/lib/mysql
[root@manage mysql]# ls
cloud ibdata1 mysql-bin.000001 mysql-bin.000005 mysql-bin.000009 mysql-bin.000013
cloudbridge ib_logfile0 mysql-bin.000002 mysql-bin.000006 mysql-bin.000010 mysql-bin.index
cloud_usage ib_logfile1 mysql-bin.000003 mysql-bin.000007 mysql-bin.000011 mysql.sock
fss mysql mysql-bin.000004 mysql-bin.000008 mysql-bin.000012 spauth
[root@manage mysql]# mysqlbinlog mysql-bin.000013 |more
mysqlshow
mysqlshow - display database, table, and column information mysqlshow supports the following options, which can be specified on the command line or in
the [mysqlshow] and [client] groups of an option file. [root@manage ~]# mysqlshow -p123456 cloud vpc display
Database: cloud Table: vpc Wildcard: display
+---------+------------+-----------+------+-----+---------+-------+---------------------------------+--------------------------------------------------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+---------+------------+-----------+------+-----+---------+-------+---------------------------------+--------------------------------------------------+
| display | tinyint(1) | | NO | | 1 | | select,insert,update,references | True if the vpc can be displayed to the end user |
+---------+------------+-----------+------+-----+---------+-------+---------------------------------+--------------------------------------------------+
mysqlcheck
[root@manage ~]# mysqlcheck -p cloud
Enter password:
cloud.account OK
cloud.account_details OK
cloud.account_network_ref OK
cloud.op_lock
note : The storage engine for the table doesn't support check
cloud.op_networks OK
cloud.op_nwgrp_work
note : The storage engine for the table doesn't support check
cloud.op_pod_vlan_alloc OK mysqlcheck is similar in function to myisamchk, but works differently. The main operational
difference is that mysqlcheck must be used when the mysqld server is running, whereas
myisamchk should be used when it is not.
[root@manage ~]# mysqladmin extended-status|more
+-----------------------------------+----------+
| Variable_name | Value |
+-----------------------------------+----------+
| Aborted_clients | 0 |
| Aborted_connects | 1 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 7 |
| Bytes_received | 17954 |
| Bytes_sent | 83686 |
| Com_admin_commands | 2 |
fgy3是库名
[root@manage ~]# mysqladmin create fgy3 create fgy4
[root@manage ~]# mysqlshow
[root@manage ~]# mysqladmin drop fgy4
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed. Do you really want to drop the 'fgy4' database [y/N] y
Database "fgy4" dropped
[root@manage ~]# mysqlshow [root@manage ~]# mysqladmin proc stat
+----+-------+-----------------+-------------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------+-----------------+-------------+---------+------+-------+------------------+
| 4 | cloud | localhost:46162 | cloud | Sleep | 24 | | |
| 5 | cloud | localhost:46163 | cloud_usage | Sleep | 32 | | |
| 30 | root | localhost | | Query | 0 | | show processlist |
+----+-------+-----------------+-------------+---------+------+-------+------------------+
Uptime: 16972 Threads: 3 Questions: 8410 Slow queries: 0 Opens: 17 Flush tables: 1 Open tables: 10 Queries per second avg: 0.495 [root@manage ~]# mysqladmin version
[root@manage ~]# mysqladmin variables|more
[root@manage ~]# mysqladmin extended-status|more 远程连接经由tcp/ip
[root@kvm1 ~]# mysqladmin version -h 192.168.10.110 -p
Enter password:
mysqladmin Ver 9.0 Distrib 5.5.47-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Server version 5.1.73
Protocol version 10
Connection 192.168.10.110 via TCP/IP
TCP port 3306
Uptime: 51 min 31 sec Threads: 1 Questions: 59 Slow queries: 0 Opens: 30 Flush tables: 1 Open tables: 23 Queries per second avg: 0.19 本地连接经由unix socket
[root@vm1 ~]# mysqladmin version -p
Enter password:
mysqladmin Ver 8.42 Distrib 5.1.73, for redhat-linux-gnu on x86_64
Copyright (c) 2000, 2013, 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. Server version 5.1.73
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 47 min 31 sec Threads: 1 Questions: 57 Slow queries: 0 Opens: 30 Flush tables: 1 Open tables: 23 Queries per second avg: 0.19
mysql命令行工具的更多相关文章
- 用批处理启动MySQL命令行工具
最近在看MySQL,安装好之后,每次在开始菜单去启动MySQL命令行工具的时候,都是直接用root用户连接我本地的数据库 输入密码开始工作,但是要连接服务器上的MySQL的话,就要去CMD下运行 : ...
- [MySQL]命令行工具和基本操作
[MySQL]命令行工具和基本操作 一 MySQL命令行工具 (查看帮助 ---help,或 -?) 1)MySQL MySQL是一个简单的SQL外壳(有GNU readline功能).它支持交互式 ...
- Mysql 命令行工具
1.Mysql命令行工具分为两类:服务端命令行工具和客户端命令行工具. 2.服务端工具 mysql_install_db:建库工具 mysqld_safe:Mysql服务的启动工具,mysqld_sa ...
- MySQL命令行工具各功能说明(转)
MySQL 服务器端使用工具程序 mysqld - SQL 后台程序(即 MySQL 服务器进程).该程序必须启动运行,才能连接服务器来访问数据库. mysqld_safe - 服务器启动脚本,可以通 ...
- MYSQL 命令行工具自动登录的方法
MYSQL 命令行工具自动登录的方法 1. 需求提出 由于在linux 环境下,经常需要使用mysql(command-line tool) 终端连接到MYSQL DB服务. 其中大致的语法如下: m ...
- MySQL 命令行工具之 mysqldump 深入研究
mysqldump 是MySQL的一个命令行工具,用于逻辑备份.可以将数据库和表的结构,以及表中的数据分别导出成:create database, create table, insert into的 ...
- MySQL 命令行工具不能向表中插入中文的解决方法
1.报错图示 解释:sname这个字段 解析出错. 2.解决方法 打开MySQL的安装目录,找到my.ini文件,把57和81行的utf8改成gbk后 保存,最后,重启MySQL的服务 即可. 3.测 ...
- MySQL命令行登录的例子
环境:MySQL Sever 5.1 + MySQL命令行工具 问题:MySQL命令行登录 解决: 命令 行登录语法: mysql –u用户名 [–h主机名或者IP地址] –p密码 说明:用户名是你登 ...
- MySql命令行命令和SQL语句
一.常用mysql命令行命令 1.启动MYSQL服务 net start mysql 停止MYSQL服务 net stop mysql 2.netstat -na|findstr 3306 查看被监听 ...
随机推荐
- 解决JSP路径问题的方法(jsp文件开头path, basePath作用)
原文:http://blog.csdn.net/mingxunzh/article/details/4627185 在JSP中的如果使用 "相对路径" 则有可能会出现问题. 因为 ...
- Android开发之各个语言
Android开发之各个语言 1.进行源码开发遇到一个最基础的问题就是各个语言下的字串翻译,所以我们必须得清楚res文件夹下各个资源文件夹 2.如图:
- python——初识socket
什么是socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链 ...
- C语言->实验室->指针数组
一 分析 讨论指针数组要从三个层面来考虑: 1)指针数组本身是什么 2)指针数组作为参数时的表现 3)指针数组作为返回值时的表现 二 指针数组是什么 1)指针数组--指针的集合 数组是若干元素的集合, ...
- Yii批量添加的问题
使用Yii进行批量添加的时候,执行后会发现表中只插入了foreach循环的最后一条数据,而其它数据没有添加成功,那是因为内存地址中循环时新一条数据会覆盖前一条数据,解决办法如下: 第一种方法: < ...
- jquery实现百度类似搜索提示功能(AJAX应用)
有时候觉得百度那个输入内容就有提示的工具很神奇,它究竟是怎么做到的呢?以前在一个进销存系统中也做过这么个功能,但是远远不及百度的功能强大,百度可以输入首字母,关键字拼音,或关键字都可以匹配,有时在想, ...
- JavaWeb chapter 4 Servlet处理HTTP请求
1. GET/POST提交方法: 用户在网页上点击一个超链接:(get) 用户提交在网页上提交表单:(post或者get) 用户在浏览器地址栏输入URL地址并回车(get) 2. 默认情况下都是使 ...
- LG1268树的重量
#include<bits/stdc++.h> using namespace std; #define N 35 #define INF 1e9 int dis[N][N],n,len, ...
- QB资料学习.01
1.多数据集的读取 A.取数SQL的配置,借用TStringList进行存储多个不同的取数SQL B.DBA取数: DBA.ReadMultipleDataSet(TStringList) C.结果 ...
- Python OpenCV —— geometric
用OpenCV画几何图形. import numpy as np import cv2 # Create a black image img = np.zeros((521,512,3), np.ui ...