当前位置:首页 » 编程语言 » linux下sql

linux下sql

发布时间: 2024-10-12 17:19:34

linux下如何运行sql脚本

Linux运行sql脚本的具体操作步骤如下:

1、使用shell工具登陆到安装postgresql的服务器,切换到postgres用户,postgresql默认的操作用户,命令是:su - postgres,查看当前路径是/var/lib/psql,创建一个test.sql脚本文件,命令是:vim test.sql。

⑵ linux下执行sql文件

linux里登录数据库后直接执行特定的命令就可以,参数是文件所在位置。比如说mysql:首先登录数据库mysql -u用户名 -p,之后执行source sql文件位置 就可以。

⑶ linux上mysql怎样导入sql数据库文件

首先通过xshell连接数据库服务器,执行命令mysql -u root -p 命令,按照提示输入密码。连接上数据库。x0dx0ax0dx0a在连接终端上执行命令create database JD_Model;x0dx0a执行完成后,验证数据库是否创建成功。执行命帆旦令show database;查看是否有JD_Model数据库。x0dx0ax0dx0a将准备好的数据库文件20151010.sql文件通过xftp工具,上传至/root目录下,并等待上传完毕。x0dx0ax0dx0a在连接数据库的终端执行命令use JD_Model。x0dx0a使用JD_Model数据库。具体操作如下图所示。x0dx0ax0dx0a执行命令source /root/20151010.sql。执行数据库导入命令。x0dx0a待导入完毕,执行下一步操作。x0dx0ax0dx0a确定数据表蔽或是否创建成功,即数据文件是否导入成功。x0dx0a执行命令 show tables;查看数据库下的态并扰表。

⑷ linux系统下怎么在终端运行sql语句

主要有以下几种方法:
1、将SQL语句直接嵌入到shell脚本文件中
代码如下:

--演示环境
[root@SZDB ~]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m
root@localhost[(none)]> show variables like 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.6.12-log |
+---------------+------------+

[root@SZDB ~]# more shell_call_sql1.sh
#!/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/temp.log
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/temp.log>>${LOG}
echo "script executed successful.">>${LOG}
exit;

[root@SZDB ~]# ./shell_call_sql1.sh
Logging to file '/tmp/temp.log'
+------+-------+
| id | val |
+------+-------+
| 1 | jack |
| 2 | robin |
| 3 | mark |
+------+-------+
Outfile disabled.

2、命令行调用单独的SQL文件

代码如下:

[root@SZDB ~]# more temp.sql
tee /tmp/temp.log
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/temp.sql"
Logging to file '/tmp/temp.log'
+------+-------+
| id | val |
+------+-------+
| 1 | jack |
| 2 | robin |
| 3 | mark |
+------+-------+
Outfile disabled.

3、使用管道符调用SQL文件
代码如下:

[root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql
Logging to file '/tmp/temp.log'
id val
1 jack
2 robin
3 mark
Outfile disabled.
#使用管道符调用SQL文件以及输出日志
[root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql >/tmp/temp.log
[root@SZDB ~]# more /tmp/temp.log
Logging to file '/tmp/temp.log'
id val
1 jack
2 robin
3 mark
Outfile disabled.

4、shell脚本中MySQL提示符下调用SQL

代码如下:

[root@SZDB ~]# more shell_call_sql2.sh
#!/bin/bash
mysql -uroot -p123456 <<EOF
source /root/temp.sql;
select current_date();
delete from tempdb.tb_tmp where id=3;
select * from tempdb.tb_tmp where id=2;
EOF
exit;
[root@SZDB ~]# ./shell_call_sql2.sh
Logging to file '/tmp/temp.log'
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_sql3.sh
#!/bin/bash
cmd="select count(*) from tempdb.tb_tmp"
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")
echo "Current count is : ${cnt}"
exit
[root@SZDB ~]# ./shell_call_sql3.sh
Warning: Using a password on the command line interface can be insecure.
Current count is : 3

[root@SZDB ~]# echo "select count(*) from tempdb.tb_tmp"|mysql -uroot -p123456 -s
3

[root@SZDB ~]# more shell_call_sql4.sh
#!/bin/bash
id=1
cmd="select count(*) from tempdb.tb_tmp where id=${id}"
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")
echo "Current count is : ${cnt}"
exit

[root@SZDB ~]# ./shell_call_sql4.sh
Current count is : 1

热点内容
电脑硬件配置是什么 发布:2024-10-12 18:33:58 浏览:257
菏泽科二预约密码是多少 发布:2024-10-12 18:33:55 浏览:67
找零点C语言 发布:2024-10-12 18:33:42 浏览:190
快手怎么上传gif 发布:2024-10-12 18:15:02 浏览:513
ctr算法 发布:2024-10-12 18:13:32 浏览:246
如何创建服务器账号 发布:2024-10-12 18:13:19 浏览:724
物理存储是指闪存吗 发布:2024-10-12 18:00:21 浏览:542
怎么看bcg是否配置到vs里 发布:2024-10-12 17:53:54 浏览:732
linux下sql 发布:2024-10-12 17:19:34 浏览:115
搭建sql服务器 发布:2024-10-12 17:11:25 浏览:823