
1、首先打开软件清单面板中选择相应主机,在右边的选项卡里单击配置选项卡。
2、单击安全配置文件后在“服务”部分中,单击安全配置文件。
3、在d出的对话框选项卡列表中选择 ESXi Shell—单击选项——手动启动和停止。
4、最后选择启动以启用此服务——单击确定。
5、看到这个,标志进入Shell模式成功。
给你一个思路 把sql语句写在一个文件里, 比如叫sqlfilesql
然后mysql -uroot -p'密码' < sqlfilesql
这样可以把sql语句执行出来,然后过滤出你要的内容,再做判断
具体实施看你具体情况
1、将SQL语句直接嵌入到shell脚本文件中
复制代码 代码如下:
--演示环境
[root@SZDB ~]# more /etc/issue
CentOS release 59 (Final)
Kernel \r on an \m
root@localhost[(none)]> show variables like 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5612-log |
+---------------+------------+
[root@SZDB ~]# more shell_call_sql1sh
#!/bin/bash
# Define log
TIMESTAMP=`date +%Y%m%d%H%M%S`
LOG=call_sql_${TIMESTAMP}log
echo "Start execute sql statement at `date`" >>${LOG}
# execute sql stat
mysql -uroot -p123456 -e "
tee /tmp/templog
drop database if exists tempdb;
create database tempdb;
use tempdb
create table if not exists tb_tmp(id smallint,val varchar(20));
insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');
select from tb_tmp;
notee
quit"
echo -e "\n">>${LOG}
echo "below is output result">>${LOG}
cat /tmp/templog>>${LOG}
echo "script executed successful">>${LOG}
exit;
[root@SZDB ~]# /shell_call_sql1sh
Logging to file '/tmp/templog'
+------+-------+
| id | val |
+------+-------+
| 1 | jack |
| 2 | robin |
| 3 | mark |
+------+-------+
Outfile disabled
2、命令行调用单独的SQL文件
复制代码 代码如下:
[root@SZDB ~]# more tempsql
tee /tmp/templog
drop database if exists tempdb;
create database tempdb;
use tempdb
create table if not exists tb_tmp(id smallint,val varchar(20));
insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');
select from tb_tmp;
notee
[root@SZDB ~]# mysql -uroot -p123456 -e "source /root/tempsql"
Logging to file '/tmp/templog'
+------+-------+
| id | val |
+------+-------+
| 1 | jack |
| 2 | robin |
| 3 | mark |
+------+-------+
Outfile disabled
3、使用管道符调用SQL文件
复制代码 代码如下:
[root@SZDB ~]# mysql -uroot -p123456 </root/tempsql
Logging to file '/tmp/templog'
id val
1 jack
2 robin
3 mark
Outfile disabled
#使用管道符调用SQL文件以及输出日志
[root@SZDB ~]# mysql -uroot -p123456 </root/tempsql >/tmp/templog
[root@SZDB ~]# more /tmp/templog
Logging to file '/tmp/templog'
id val
1 jack
2 robin
3 mark
Outfile disabled
4、shell脚本中MySQL提示符下调用SQL
复制代码 代码如下:
[root@SZDB ~]# more shell_call_sql2sh
#!/bin/bash
mysql -uroot -p123456 <<EOF
source /root/tempsql;
select current_date();
delete from tempdbtb_tmp where id=3;
select from tempdbtb_tmp where id=2;
EOF
exit;
[root@SZDB ~]# /shell_call_sql2sh
Logging to file '/tmp/templog'
id val
1 jack
2 robin
3 mark
Outfile disabled
current_date()
2014-10-14
id val
2 robin
5、shell脚本中变量输入与输出
复制代码 代码如下:
[root@SZDB ~]# more shell_call_sql3sh
#!/bin/bash
cmd="select count() from tempdbtb_tmp"
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")
echo "Current count is : ${cnt}"
exit
[root@SZDB ~]# /shell_call_sql3sh
Warning: Using a password on the command line interface can be insecure
Current count is : 3
[root@SZDB ~]# echo "select count() from tempdbtb_tmp"|mysql -uroot -p123456 -s
3
[root@SZDB ~]# more shell_call_sql4sh
#!/bin/bash
id=1
cmd="select count() from tempdbtb_tmp where id=${id}"
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")
echo "Current count is : ${cnt}"
exit
[root@SZDB ~]# /shell_call_sql4sh
Current count is : 1
#以上脚本演示中,作抛砖引玉只用,对于输出的结果不是很规整友好,需要进一步改善和提高。
以上就是关于在mysql下怎样进入shell 命令行全部的内容,包括:在mysql下怎样进入shell 命令行、在shell中运行sql去 *** 作mysql的表,不生效,log如下、mysql是否有触发机制,可以执行shell脚本等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)