怎样在linux的shell脚本中连接mongodb插入

怎样在linux的shell脚本中连接mongodb插入,第1张

想在shell中连接数据库,首先要在连接数据的机器上安装mongodb的客户端才可以。客户端的安装在这里不再重复,自己百度或者google一下吧。连接mongodb的命令如下:

/home/test/mongodb/mongodb-223/bin/mongo 127001:8888

这个是我的数据库配置,没有设置用户名密码。所以直接通过该命令就可以连接。

连结后会有一个默认连接的数据库。

mongodb常用命令:

查看数据库命令:

show dbs;

查看集合命令:

show collections;

切换数据库:

use databaseName;

查询数据:

db集合名find()

插入数据:

db集合名insert({name:'test',age:1});

删除:

dbtestremove();

sql="dbtestinsert({name:'test',age:1});"//定义执行的sqlecho "$sql"|/home/test/mongodb/mongodb-223/bin/mongo 127001:8888/test --shell

注意,echo命令中的格式必须这样写,管线命令后面的是是数据库安装地址 然后是ip:端口号,斜线后是数据库名称,--shell表示通过shell交互!

一 备份数据库

1 选择某一个数据库,右键依次选择:任务==》生成脚本

2 选择要编写脚本的数据库对象,注意此处可以选择特定的数据库对象,我们可以选择我们需要备份的数据表。

3 在当前页面下面第一步选择高级选项,然后再选择输出的sql脚本的保存位置。

4 高级选项中最重要的一步就是在”要编写的脚本的数据类型“中选择第一项“架构和数据”,这个默认是选择“仅限架构”,这个选项只会复制数据库的表结构而不会复制数据库的具体数据。

5 我们可以查看具体的导出的脚本和数据。

二 还原数据库

1 在一个新的数据库实例中创建一个一模一样的空的数据库,名称也为IGIS。

2 通过:文件==》打开文件,打开刚开始导出的脚本文件。

3 选择执行或者是按下F5来执行相应的 *** 作,就可以将相应的数据库里面的数据表结构和数据导入到新的数据库中,最终完成数据库的备份工作。

//创建数据库连接对象

var conn = new ActiveXObject("ADODBConnection");

//创建数据集对象

var rs = new ActiveXObject("ADODBRecordset");

try{

//如果不知道如何配置连接串,可以通过配置UDL文件后用文本编辑器打开获得

var connectionstring = "Provider=OraOLEDBOracle1;Password=pwd;Persist Security Info=True;User ID=username;Data Source=ORA";

//打开连接

connopen(connectionstring);

//查询语句

var sql = " select from tb_col ";

//打开数据集(即执行查询语句)

rsopen(sql,conn);

//遍历所有记录

while(!rseof){

//WScript是Windows 的脚本宿主对象,详细情况请在windows帮助里查找。

//WScriptEcho输出记录的内容

WScriptEcho(rsFields("id") + "\t" + rsFields("name") + "\n");

//下一条记录

rsmoveNext(); }

//关闭记录集

rsclose();

//关闭数据库连接

connclose();} catch(e){

//异常报告

WScriptEcho(emessage);} finally{

}

javascript 在本地安全域内是可以直接 *** 作数据库的,但也仅仅局限于本地访问。由于脚本的跨域安全限制,要远程访问是不可能的。

如果使用服务端语言,如asp,是可以使用javascript 作为 动态语言来编写数据库访问代码的。

<script language="javascript" runat="server">

var conn = new ActiveXObject("ADODBConnection");

connOpen("Provider=SQLOLEDB1; Data Source=localhost; User ID=sa; Password=changemenow; Initial Catalog=pub");

var rs = new ActiveXObject("ADODBRecordset");

var sql="select id from authors";

rsopen(sql, conn);

var word="";

while(!rsEOF)

{

word+=rs("id")

}

documentwrite(word);

rsclose();

rs = null;

connclose();

conn = null;

</script>

语法上遵循javascript,就可以进行任何 *** 作了。

Private Sub 历史报表Btn_Click()

'注释: 1。该程序需要安装ADO 20目标库并在本机注册

' 2。Microsoft ActiveX Data Objects 21 Library 必须被引用 (Office 2000)

' 3。Microsoft Excel 90 object libraries 必须被引用 (Office 2000)

' 4。划===处可根据具体报表修改

Dim strQuery As String

Dim c, i As Integer

Dim r As Integer

Dim Rpt_xls As ExcelApplication

Dim dnm(4) As String

Dim MyDate, MyMonth, MyDay, MyHour, MyMinute, MySecond

Dim stDate, stMonth, stDay, stHour, stinute, stSecond, sttime As String

Dim StartTime, EndTime, Duration, DisplayDay, DisplayMonth As String

'++===================================================================

'报表中的 TAG

Dim Items As Integer

dnm(1) = "AI1"

dnm(2) = "AI2"

dnm(3) = "AI3"

dnm(4) = "AI4"

'从历史库中取得域项, 2 - DATATIME, VALUE, TAG 共三项

'--====================================================================

stDate = Date$

sttime = Time$

StartTime = stDate & " 00:00:00"

EndTime = stDate + " " + sttime

Set Rpt_xls = New ExcelApplication

Rpt_xlsVisible = True

'++============================================================================

'打开的报表文件名

Dim OutReportFile As String

Dim Rpt_f2 As String

Rpt_f2 = "d:\Dynamics\App\HIST"

Rpt_xlsWorkbooksOpen Rpt_f2 & "XLS"

Rpt_xlsSheets("Sheet1")Select

Range("e1")Select

SelectionNumberFormatLocal = "yyyy-mm-dd hh:mm:ss"

Cells(1, 4)Value = stDate & "-" & sttime

' Rpt_xlsActiveWorkbookSaveAs (Rpt_f2 & stDate)

'++==========================================================================

Dim cnADO As New ADODBConnection

Dim rsADO As Recordset

Set cnADO = New ADODBConnection

cnADOConnectionString = "DSN = FIX Dynamics Historical Data; UID =; PWD = ;"

cnADOOpen "FIX Dynamics Historical Data", "", ""

'============================================================================

'查询,根据报表修改。注意,在“Select VALUE FROM mynd“中,mynd是当前SCADA节点名。

For i = 1 To 4

r = 3

strQuery = "Select VALUE FROM mynd " & _

"WHERE (TAG='" & dnm(i) & "'and " & _

"INTERVAL = '00:30:00' and " & _

"DATETIME >= {ts '" & StartTime & "'} and " & _

"DATETIME <= {ts '" & EndTime & "'})"

'--===========================================================================

MsgBox (strQuery)

Set rsADO = New ADODBRecordset

rsADOOpen strQuery, cnADO, adOpenForwardOnly, adLockBatchOptimistic

rsADOMoveFirst

Rpt_xlsColumns("a")Select

SelectionNumberFormatLocal = "yyyy-mm-dd hh:mm:ss"

While rsADOEOF <> True

' MsgBox ("有记录")

Cells(r, i + 1)Value = rsADOFields(0)

Cells(r, i + 1)NumberFormatLocal = "000"

r = r + 1

rsADOMoveNext

Wend

MsgBox (r)

Next i

Set cnADO = Nothing

' Rpt_xlsActiveSheetPageSetupOrientation = xlPortrait 'xlLandscape

' Rpt_xlsActiveSheetPageSetupPaperSize = xlPaperA4

' Rpt_xlsActiveSheetPrintOut

' Rpt_xlsDisplayAlerts = False

Rpt_xlsActiveWorkbookSave

' OutReportFile = Rpt_f2 & "_00" & MyMonth & MyDay

' Rpt_xlsActiveWorkbookSaveAs OutReportFile

Rpt_xlsActiveWorkbookSaveAs (Rpt_f2 & stDate)

Rpt_xlsQuit

Rpt_xlsDisplayAlerts = True

Set Rpt_xls = Nothing

End Sub

Private Sub 历史数据库Btn_Click()

'注释: 1。该程序需要安装ADO 20目标库并在本机注册

' 2。Microsoft ActiveX Data Objects 21 Library 必须被引用 (Office 2000)

' 3。Microsoft Excel 90 object libraries 必须被引用 (Office 2000)

' 4。划===处可根据具体报表修改

Dim strQuery As String

Dim c As Integer

Dim r As Integer

Dim Intyexcel As ExcelApplication

Dim MyDate, MyMonth, MyDay, MyTime, MyHour, MyMinute, MySecond

Dim StartTime, EndTime, Duration, DisplayDay, DisplayMonth As String

'从历史库中取得域项, 2 - DATATIME, VALUE, TAG 共三项

Items = 2

'--====================================================================

MyDate = Format(Now(), "yyyy-mm-dd")

MyTime = Format(Now(), "h:m:s")

StartTime = MyDate & " " & "00:00:00"

EndTime = Now()

'++==========================================================================

' strQuery = "Select value from Fix " + _

' " Where DATETIME >={ts '" + StrStarTime + "'} AND " + _

' "DATETIME <={ts '" + StrEndTime + "'} AND " + _

' "Tag = " & " '" & HisDate5(i, 0) & "'"

'查询,根据报表修改

strQuery = "Select From MYND " + _

"WHERE (DATETIME >= {ts '" & StartTime & "'} and " + _

"DATETIME <= {ts '" & EndTime & "'}) and " + _

"(tag = 'AI1')" + _

"and INTERVAL = '00:30:00' "

'--===========================================================================

Dim cnADO As New ADODBConnection

Dim rsADO As Recordset

Set cnADO = New ADODBConnection

cnADOConnectionString = "DSN = FIX Dynamics Historical Data; UID = ; PWD = ;"

cnADOOpen "FIX Dynamics Historical Data", "", ""

Set rsADO = New ADODBRecordset

' RsOpen strQuery, Cn, adOpenStatic, 3, adCmdText

rsADOOpen strQuery, cnADO, adOpenForwardOnly, adLockBatchOptimistic

' rsADOMoveFirst

'++============================================================================

'打开的报表文件名

Dim Rpt_xls As ExcelApplication

Dim OutReportFile As String

Dim Rpt_f1 As String

Set Rpt_xls = New ExcelApplication

Rpt_xlsVisible = True

Rpt_f1 = "d:\Dynamics\App\rt1"

Rpt_xlsWorkbooksOpen Rpt_f1 & "XLS"

' Rpt_xlsActiveWorkbookSaveAs (Rpt_f1 & stDate)

'===============================================================================

Rpt_xlsSheets("Sheet2")Select

Rpt_xlsRange("E1")Select

r = 3

SelectionNumberFormatLocal = "yyyy-mm-dd hh:mm:ss"

Cells(1, 5)Value = EndTime

While rsADOEOF <> True

If rsADO(c) <> "" Then

Cells(r, 1) = rsADOFields(0)

Cells(r, 2) = rsADOFields(1)

Cells(r, 3) = rsADOFields(2)

Cells(r, 4) = rsADOFields(3)

Cells(r, 5) = rsADOFields(4)

Cells(r, 6) = rsADOFields(5)

Cells(r, 7) = rsADOFields(6)

Cells(r, 8) = rsADOFields(7)

Cells(r, 9) = rsADOFields(8)

End If

r = r + 1

rsADOMoveNext

Wend

' IntyexcelSheets("Sheet1")Select

' IntyexcelActiveSheetPageSetupOrientation = xlPortrait 'xlLandscape

' IntyexcelActiveSheetPageSetupPaperSize = xlPaperA4

' IntyexcelActiveSheetPrintOut

' IntyexcelDisplayAlerts = False

' IntyexcelActiveWorkbookSave

' OutReportFile = InReportFile & "_00" & MyMonth & MyDay

' IntyexcelActiveWorkbookSaveAs OutReportFile

' IntyexcelQuit

' IntyexcelDisplayAlerts = True

' Set Intyexcel = Nothing

Set cnADO = Nothing

End Sub

Private Sub 实时报表Btn_Click()

'注释: 1。该程序需要安装ADO 20目标库并在本机注册

' 2。Microsoft ActiveX Data Objects 21 Library 必须被引用 (Office 2000)

' 3。Microsoft Excel 90 object libraries 必须被引用 (Office 2000)

' 4。划===处可根据具体报表修改

Dim c As Integer

Dim r As Integer

Dim Rpt_xls As ExcelApplication

Dim MyDate, MyMonth, MyDay, MyHour, MyMinute, MySecond

Dim stDate, stMonth, stDay, stHour, stinute, stSecond, sttime As String

Dim StartTime, EndTime, Duration, DisplayDay, DisplayMonth As String

'--====================================================================

stDate = Date$

sttime = Time$

StartTime = stDate & " 09:40:00"

EndTime = stDate + " " + sttime

r = 1

Items = 8

Set Rpt_xls = New ExcelApplication

Rpt_xlsVisible = True

'++============================================================================

'打开的报表文件名

Dim OutReportFile As String

Dim Rpt_f1 As String

Rpt_f1 = "d:\Dynamics\App\rt1"

Rpt_xlsWorkbooksOpen Rpt_f1 & "XLS"

Rpt_xlsActiveWorkbookSaveAs (Rpt_f1 & stDate)

'===============================================================================

Rpt_xlsSheets("Sheet1")Select

Rpt_xlsRange("E1")Select

SelectionNumberFormatLocal = "yyyy-mm-dd hh:mm:ss"

Cells(1, 5)Value = stDate & sttime

'================================================================================

Cells(3, 2)Value = Fix32myndai1f_cv

Cells(3, 3)Value = Fix32myndai2f_cv

Cells(3, 4)Value = Fix32myndai3f_cv

Cells(3, 5)Value = Fix32myndai4f_cv

Range("B3:E3")Select

SelectionNumberFormatLocal = "000_ "

'================================================================================

Rpt_xlsActiveSheetPageSetupOrientation = xlPortrait 'xlLandscape

Rpt_xlsActiveSheetPageSetupPaperSize = xlPaperA4

'Rpt_xlsActiveSheetPrintOut

Rpt_xlsDisplayAlerts = False

Rpt_xlsActiveWorkbookSave

OutReportFile = Rpt_f1 & "_00" & MyMonth & MyDay

Rpt_xlsActiveWorkbookSaveAs OutReportFile

Rpt_xlsQuit

Rpt_xlsDisplayAlerts = True

Set Rpt_xls = Nothing

End Sub

第一步:打在开始界面中找到mysql

第二步:双击打开mysql软件。,并输入密码。

第三步:如果sql文件的内容中有创建数据库的语句或者你想将表存放在你已有的数据库,在这里就不用创建数据库。

第四步:输入“show databases;”就能看到自己创建的数据库。

第五步:输入“use 数据库名”,开始使用这个数据库。

第六步:开始导入sql文件,输入“source sql文件的路径”(注意你的文件路径要是复制来的,要将"\"全部换成“/”)

第七步:输入“show tables”,你就会看到你导入的表了。

以上就是关于怎样在linux的shell脚本中连接mongodb插入全部的内容,包括:怎样在linux的shell脚本中连接mongodb插入、如何将数据库脚本导入数据库、Javascript怎样连接数据库喔等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10182422.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存