3种自动登录MySQL命令行模式的方法

  作者:zhanhailiang 日期:2013-01-15

第1种(主要是为了说明expect的使用场景,没什么实用意义

一般情况下,我们想要进入MySQL命令模式总是需要按如下交互输入密码确认,才能进入命令行模式:

zhanhailiang@linux-06bq:~> mysql -u sl -p
Enter password: 

其实我们可以使用expect编写脚本,来通过expect与shell交互通信来实现自动登录:

zhanhailiang@linux-06bq:~> cat mysql.sh 
#!/usr/local/bin/expect

spawn /usr/local/services/mysql/bin/mysql -u sl -p
expect {
    "assword" { send "sl\r" }
}
interact

其实这段脚本的意思就是说“执行/usr/local/services/mysql/bin/mysql -u sl -p,等待响应,若匹配assword,则发送sl(即MySQL用户登录密码),回车,模拟用户手工操作”。

编写完mysql.sh脚本后,赋予执行权限,然后就可以通过执行mysql.sh来自动登录MySQL命令行模式。

zhanhailiang@linux-06bq:~> chmod +x mysql.sh 
zhanhailiang@linux-06bq:~> ./mysql.sh        
spawn /usr/local/services/mysql/bin/mysql -u sl -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12912
Server version: 5.5.14-log MySQL Community Server (GPL)

Copyright (c) 2000, 2010, 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.

mysql> 

注:expect拥有众多的参数及丰富的应用场景,有兴趣的同学请自行查看man手册或其它资料。

第2种

通过编辑/home/xxx/.bashrc文件,添加如下代码:

alias mysql="/usr/local/services/mysql/bin/mysql -u sl -psl"

接下来重新加载.bashrc文件

~$ source .bashrc
~$ mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.20-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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.

mysql> 

第3种

通过修改/home/xxx/.my.cnf,添加登录所需的默认参数值:

  1 [client]
  2 host=localhost
  3 user=sl
  4 password=sl

这样也可实现自动登录MySQL:

~$ mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.20-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, 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.

mysql> 

参考资料:linux expect, spawn用法小记

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值