mysql command 工具 ----Mysql命令

mysql命令
mysql 命令是最基本的命令,通过该命令登录到数据库进行操作。

1 连接

[root@localhost ~]# mysql --help
[root@localhost ~]# mysql -u root -p
Enter password:
[root@localhost ~]# mysql -S /disk1/mysql/mysql.sock

当配置文件中配置了

[client]
user=****
password=******

可直接mysql登录,省略用户密码选项

[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12

2 mysql下交互式命令

help (\h) (?) (\?)          ----列出帮助
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.

如:
edit (\e) —–编辑当前sql语句,并执行,用于方便修改很长的sql语句时
select Host,User,Select_priv,Insert_priv,Update_priv,Delete_priv from mysql.user \e;

ego (\G) —–查询结果垂直显示,便于观看
select Host,User,Select_priv,Insert_priv,Update_priv,Delete_priv from mysql.user \G;

exit (\q) quit (\q) —-退出
rehash (#) —开启tab自动补全命令,必须在数据库【db_name】下使用,如果需永久开启则可在配置文件中[mysql]下添加 auto_rehash
source (.) —执行sql脚本
status (\s) —显示状态信息,包含数据版本,会话ID,user,字符集等
system (!) —执行外部系统命令
tee (\T) —记录屏幕输出到文件,如 \T ./123.txt
charset (\C) —修改当前客户端连接的字符集,仅仅只是当前连接有效。

3 mysql 登录后的日志

默认情况下linux下利用 mysql 登录数据库后,会将后续mysql> 的操作命令都记录在历史文件中.
如root 用户,则历史文件就是 /root/.mysql_history

–histignore 忽悠记录特定字符串(不区分大小写)的命令
如:mysql –histignore=”UPDATE:DELETE” 忽悠掉了 delete update 之类的语句

禁止记录方法:
如针对root用户

[root@localhost ~]# rm /root/.mysql_history
[root@localhost ~]# ln -s /dev/null /root/.mysql_history

-j, –syslog 将命令历史 补充上其他信息记录到系统日志中 /var/log/messages
如:

[root@localhost ~]# mysql --syslog                            ----用户及命令已写入到配置文件[client]
[root@localhost ~]# cat /var/log/messages
pr  8 23:10:21 localhost MysqlClient: SYSTEM_USER:'root', MYSQL_USER:'root', CONNECTION_ID:22, DB_SERVER:'--', DB:'--', QUERY:'show databases;'
Apr  8 23:10:26 localhost MysqlClient: SYSTEM_USER:'root', MYSQL_USER:'root', CONNECTION_ID:22, DB_SERVER:'--', DB:'--', QUERY:'use breath;'

4 服务器端命令帮助

mysql> help search_string
mysql服务器支持 help 命令 检索语句命令,只有给一个有效的字符,进行检索匹配。
原理:调用初始化生成的几个帮助表,如 mysql.help_topic mysql.help_category等。这几张表都是由脚本$MYSQL_HOME/share/fill_help_tables.sql初始化.
如果help 执行结果不对或者错误,可用脚本进行初始化数据

如果你的数据库利用的二进制升级的情况下,默认不会自动升级帮助表,手动运行下脚本初始化
[root@localhost ~]# mysql -u root mysql < fill_help_tables.sql

具体使用

mysql> help contents
mysql> help logs
mysql> help show binary logs
mysql> HELP rep%
mysql> HELP %index%

5 从文本文件执行sql语句(SQL脚本)

两种方式:
shell> mysql db_name < text_file —-登录前
或者
mysql> source file_name —登录后
mysql> . file_name

注:默认执行脚本只显示结果,不显示sql语句
mysql –verbose 则连语句一起显示

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值