日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mongodb启动成功连不上_MySQL数据导入到MongoDb教程

發布時間:2024/1/23 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mongodb启动成功连不上_MySQL数据导入到MongoDb教程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、安裝與配置MySQL

  • 首先下載MySQL免安裝包,版本是mysql-5.6.24。網址是//dev.mysql.com/downloads/mysql/
  • 解壓MySQL軟件包,得到如下目錄:

  • 配置MySQL的系統環境變量:
    • 打開 Windows 環境變量設置, 新建變量名MYSQL_HOME,變量值為MySQL安裝目錄路徑, 這里為D:DownloadFilesmysql
    • 在環境變量的Path變量中添加;%MYSQL_HOME%bin;
  • MySQL-5.6默認的配置文件是D:DownloadFilesmysqlmy-default.ini如果想要換成你自己的配置文件,待會安裝MySQL服務的時候加上參數--defaults-file="my.ini"
  • 修改配置文件,這里使用默認的配置文件,所以修改my-default.ini如下:
# For advice on how to change settings please see # //dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL.[mysqld]# Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin# These are commonly set, remove the # and set as required.basedir = D:DownloadFilesmysqldatadir = D:DownloadFilesmysqldata # port = ..... # server_id = .....# Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  • 以管理員的身份運行CMD,啟動命令行窗口,然后安裝MySQL服務: mysqld -install 或 mysqld -install MySQL --defaults-file="my.ini" 當提示"Service successfully installed."表示安裝成功。注意:如果使用自己的配置文件參數是--defaults-file而不是--default-file哦!否則會在啟動MySQL服務的時候報錯:C:WINDOWSsystem32>net start mySQL The MySQL service is starting... The MySQL service could not be started. A system error has occurred. System error 1067 has occurred. The process terminated unexpectedly . 出現這個錯誤的時候可以去查看MySQL的錯誤日志,文件位于D:DownloadFilesmysqldata中,文件一般是以err為后綴名的文件,里面可以看到啟動服務時出錯的原因,這樣就可以解決了。
  • 啟動MySQL服務,如果沒有啟動而貿然連接的話會報錯:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061),命令如下: net start mysql 關閉服務:net stop mysql。如果出現錯誤:
發生系統錯誤 2。系統找不到指定的文件。

那么是因為注冊表的問題,只需要去修改HKEY_LOCAL_MACHINE-SYSTEM-CurrentControlSet-services-mysql-ImagePath的值,修改為當前你的MySQL的解壓縮的路徑,當前是D:DownloadFilesmysqldata。

  • 登錄MySQL 執行mysql -uroot -p,之后會出現Enter password:的時候不需要輸入密碼,因為是第一次登錄,所以千萬不要輸入密碼,否則之后就會一直錯誤一直拒絕登錄。登錄成功顯示:
D:DownloadFilesmysql>mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1 Server version: 5.6.24 MySQL Community Server (GPL)Copyright (c) 2000, 2015, 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.
  • 為root用戶設置密碼: set password=password('你的密碼');,成功的話會提示: Query OK, 0 rows affected (0.00 sec)
  • 重新刷新權限: flush privileges;,成功的話也會提示一樣的語句。

2、使用MySQL-Front操作MySQL

  • 先連接上MySQL,打開MySQL-Front軟件,按快捷鍵F12打開登錄信息,新建一個連接:

填上剛才root用戶設置的密碼,以及主機名(localhost),然后連接即可,連接上的界面如下:

  • 新建一個數據庫,如圖操作:

然后選擇正確的字符集,點擊確定就ok了。

  • 接著將已存在的.sql的數據庫導入到我們新建的數據庫中,導入步驟如下:

成功導入之后可以看到結果:

3、啟動與配置MongoDB

  • 在windows下載MongoDb,之后安裝成功,并將MongoDb的安裝目錄的Bin文件夾配置到環境變量中,如下圖:

  • 選擇一個MongoDb的數據和日志存儲目錄,比如E:MongoDb,然后在該文件夾下新建一個配置文件:mongodb.config(名字隨便取)。文件簡單配置如下:
dbpath=E:MongoDbdata logpath=E:MongoDblogmongoDbLog.log #這個第一次啟動的時候不要加這個選項,等到創建管理員用戶之后再加進去重新啟動數據庫 auth=true
  • 啟動MongoDb 命令如下:mongod --config E:MongoDbmongodb.config --storageEngine mmapv1 啟動成功日志:

注意:后面的存儲引擎的配置是因為剛開始采用默認的存儲引擎:wiredTiger(二者有什么區別暫時沒有去深究)的時候,在MongoDb中添加了任何數據在MongoVUE軟件中一直看不到,后來網上搜索了一下才發現還有這種坑。然后就指定了mmapv1這種存儲引擎。另外如果剛開始使用默認的,后來要切換到新的存儲引擎的話,存儲數據的文件夾就不能和以前的文件夾一樣,需要新建一個。
  • 連接MongoDb 接著我們另外開一個CMD窗口,連接MongoDb服務器:
C:Windowssystem32>mongo MongoDB shell version: 3.2.4 connecting to: test >

因為此時我們開啟了服務器的鑒權配置,但是此時我們沒有任何用戶密碼,并且是沒有密碼登錄進去的,所以日志就會提示紅色框中的兩行。 這個時候如果你查看數據庫信息是會被拒絕的,所以需要注釋掉配置文件中的auth=true這行。

> show dbs 2016-04-20T20:54:33.201+0800 E QUERY [thread1] Error: listDatabases failed:{"ok" : 0,"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }","code" : 13 } : _getErrorWithCode@src/mongo/shell/utils.js:23:13 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:53:1 shellHelper.show@src/mongo/shell/utils.js:700:19 shellHelper@src/mongo/shell/utils.js:594:15 @(shellhelp2):1:1
  • 新建一個數據庫并添加用戶 MongoDb自從2.6版本之后用戶管理以及安全性有了很大的提高,能夠做到非常細微的安全管理。其中添加用戶從命令addUser變成了createUser,并且參數也得到擴展:(也就是多了一個role的參數) db.createUser({user:'**',pwd:'***',roles:[***,**]})
  • 新建一個管理員賬戶
> use admin switched to db admin > db.createUser( {user: "admin",pwd: "123456",roles:[{role: "userAdminAnyDatabase",db: "admin"}] } ) Successfully added user: {"user" : "admin","roles" : [{"role" : "userAdminAnyDatabase","db" : "admin"}] } >
  • 新建forum數據庫并添加用戶 重啟MongoDB,鍵入下面命令,使用管理員權限登錄如下:
> use admin switched to db admin > db.auth("admin", "123456") 1 > db.runCommand( ... { ... usersInfo:"admin", ... showPrivileges:true ... } ... ) {"users" : [{"_id" : "admin.admin","user" : "admin","db" : "admin","roles" : [{"role" : "userAdminAnyDatabase","db" : "admin"}],"inheritedRoles" : [{"role" : "userAdminAnyDatabase","db" : "admin"}],"inheritedPrivileges" : [{"resource" : {"db" : "","collection" : ""},"actions" : ["changeCustomData","changePassword","createRole","createUser","dropRole","dropUser","grantRole","revokeRole","viewRole","viewUser"]},{"resource" : {"cluster" : true},"actions" : ["authSchemaUpgrade","invalidateUserCache","listDatabases"]},{"resource" : {"db" : "","collection" : "system.users"},"actions" : ["collStats","dbHash","dbStats","find","killCursors","listCollections","listIndexes","planCacheRead"]},{"resource" : {"db" : "admin","collection" : "system.users"},"actions" : ["collStats","createIndex","dbHash","dbStats","dropIndex","find","killCursors","listCollections","listIndexes","planCacheRead"]},{"resource" : {"db" : "admin","collection" : "system.roles"},"actions" : ["collStats","createIndex","dbHash","dbStats","dropIndex","find","killCursors","listCollections","listIndexes","planCacheRead"]},{"resource" : {"db" : "admin","collection" : "system.version"},"actions" : ["collStats","dbHash","dbStats","find","killCursors","listCollections","listIndexes","planCacheRead"]},{"resource" : {"db" : "admin","collection" : "system.new_users"},"actions" : ["collStats","dbHash","dbStats","find","killCursors","listCollections","listIndexes","planCacheRead"]},{"resource" : {"db" : "admin","collection" : "system.backup_users"},"actions" : ["collStats","dbHash","dbStats","find","killCursors","listCollections","listIndexes","planCacheRead"]}]}],"ok" : 1 } >


其次新建一個我們的數據庫:use forum,當你敲入這個命令的時候,如果服務器中沒有這個數據庫便會新建一個數據庫。

接著我們創建一個用戶:

> db.createUser( { "user" : "forum","pwd": "123456","customData" : { employeeId: 12345 },"roles" : [ { role: "dbOwner", db: "forum" }]},{ w: "majority" , wtimeout: 5000 } )

新建成功的提示:

Successfully added user: {"user" : "forum","customData" : {"employeeId" : 12345},"roles" : [{"role" : "dbOwner","db" : "forum"}] } >

查看系統當前用戶:

> show users {"_id" : "forum.forum","user" : "forum","db" : "forum","customData" : {"employeeId" : 12345},"roles" : [{"role" : "dbOwner","db" : "forum"}] }這里有一個小問題:如果你是在數據庫test下創建一個可以操作forum數據庫的用戶,那么第三個字段db顯示的是你當前所在的數據庫,我經過試驗發現那樣的話你在forum數據庫認證該用戶會出現認證失敗的情況,這個問題有待解釋!!

備注:

Built-In Roles(內置角色):

1. 數據庫用戶角色:read、readWrite; 2. 數據庫管理角色:dbAdmin、dbOwner、userAdmin; 3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager; 4. 備份恢復角色:backup、restore; 5. 所有數據庫角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase 6. 超級用戶角色:root // 這里還有幾個角色間接或直接提供了系統超級用戶的訪問(dbOwner 、userAdmin、userAdminAnyDatabase) 7. 內部角色:__system
  • 重新開啟鑒權啟動數據庫 這次重啟數據庫之后連接的時候就可以采用用戶名和密碼的方式來連接:

mongo -u forum -p 123456

成功之后可以查看admin這個數據庫的集合:

> use admin switched to db admin > show collections system.indexes system.users system.version
  • 打開MongoVUE連接MongoDb 下載的MongoVUE的軟件有點老舊,新版本在官網上一直訪問不了,不知道為啥?下載地址: //download.csdn.net/download/show_594/7484123 打開軟件之后新建一個連接:

之后點擊紅色的框框測試連接,如果這個時候報錯誤:

Connection was refused Unable to connect to server localhost:27017: Invalid credential for database 'forum'.. Type: MongoDB.Driver.MongoConnectionException Stack: 在 MongoDB.Driver.Internal.DirectMongoServerProxy.Connect(TimeSpan timeout, ReadPreference readPreference)在 MongoDB.Driver.MongoServer.Connect(TimeSpan timeout)在 MongoDB.Driver.MongoServer.Connect()在 MangoUI.MMongo.Open(Boolean mustWrite)在 MangoUI.MMongo.Open()在 MangoUI.MConnection.get_IsValid()在 MangoUI.WinConnect.btnTest_Click(Object sender, EventArgs e)Invalid credential for database 'forum'. Type: MongoDB.Driver.MongoAuthenticationException Stack: 在 MongoDB.Driver.Communication.Security.MongoCRAuthenticationProtocol.Authenticate(MongoConnection connection, MongoCredential credential)在 MongoDB.Driver.Communication.Security.Authenticator.Authenticate(MongoCredential credential)在 MongoDB.Driver.Communication.Security.Authenticator.Authenticate()在 MongoDB.Driver.Internal.MongoConnection.Open()在 MongoDB.Driver.Internal.MongoConnection.GetNetworkStream()在 MongoDB.Driver.Internal.MongoConnection.SendMessage(BsonBuffer buffer, Int32 requestId)在 MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage message)在 MongoDB.Driver.Operations.CommandOperation`1.Execute(MongoConnection connection)在 MongoDB.Driver.MongoServerInstance.RunCommandAs[TCommandResult](MongoConnection connection, String databaseName, IMongoCommand command)在 MongoDB.Driver.MongoServerInstance.Ping(MongoConnection connection)在 MongoDB.Driver.MongoServerInstance.Connect()在 MongoDB.Driver.Internal.DirectMongoServerProxy.Connect(TimeSpan timeout, ReadPreference readPreference)Command 'authenticate' failed: auth failed (response: { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }) Type: MongoDB.Driver.MongoCommandException Stack: 在 MongoDB.Driver.Operations.CommandOperation`1.Execute(MongoConnection connection)在 MongoDB.Driver.Communication.Security.MongoCRAuthenticationProtocol.RunCommand(MongoConnection connection, String databaseName, IMongoCommand command)在 MongoDB.Driver.Communication.Security.MongoCRAuthenticationProtocol.Authenticate(MongoConnection connection, MongoCredential credential) Inputs:: Command: authenticate Ok: False ErrorMsg: auth failed Request: { "authenticate" : 1, "user" : "forum", "nonce" : "a7926dad539b682f", "key" : "0a5b321ba5814aa59f3b428aa8a1845d" } Response: { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }

并且查看日志有:

[conn9] authenticate db: forum { authenticate: 1, user: "forum", nonce: "xxx", key: "xxx" } 2016-04-20T22:08:20.965+0800 I ACCESS [conn9] Failed to authenticate forum@forum with mechanism MONGODB-CR: AuthenticationFailed: MONGODB-CR credentials missing in the user document

那么解決方法是:

注意:執行下面的命令的時候因為用戶權限不夠,所以需要暫時關掉鑒權再重啟服務器執行這些命令。C:Windowssystem32>mongo MongoDB shell version: 3.2.4 connecting to: test > use admin switched to db admin > > > db.system.users.remove({}) WriteResult({ "nRemoved" : 2 }) > db.system.version.remove({}) WriteResult({ "nRemoved" : 1 }) > db.system.version.insert({ "_id" : "authSchema", "currentVersion" : 3 }) WriteResult({ "nInserted" : 1 })

之后就可以連接成功:

原因:

MongoDB 3 authentication mechanism has been changed from : MongoDB Challenge and Response (MONGODB-CR) to challenge and response mechanism (SCRAM-SHA-1). You have to delete your created user then change admin.system.version.authSchema to 3 instead of 5. 參考:https://docs.mongodb.org/manual/core/authentication/。

備注:Unable to connect to server localhost:27017: 未將對象引用設置到對象的實例。 因為我們新創建的而且很多數據庫都是新的什么也沒有,所以一般就是直接置空。如果你確定有相應的用戶名密碼數據庫再填上。
  • 導入MySQL數據庫 下圖是MongoDb數據庫,可以看到我們創建的forum數據庫以及新建的一條記錄:

接下去我們將導入MySQL數據庫,如下圖所示操作:

接著選擇我們需要導入的數據庫以及集合,這里以member為例子導入:

導入成功之后我們可以看到forum數據庫多了mvforummember這個集合。

  • 導出MongoDb的數據 命令格式如下: mongoexport -d dataname -c collection -o file 所以我們在命令行可以輸入:
E:MongoDb>mongoexport -d forum -c mvnforummember -o member.json --username forum --password 123456 2016-04-20T22:47:50.050+0800 connected to: localhost 2016-04-20T22:47:50.558+0800 exported 4075 records
  • 如果用的是mondodump則是dump出不一樣的數據:
E:MongoDb>mongodump -h localhost -d forum -o ./dump/ --username forum --password 123456 2016-04-20T22:44:38.716+0800 writing forum.system.indexes to 2016-04-20T22:44:38.720+0800 done dumping forum.system.indexes (2 documents) 2016-04-20T22:44:38.721+0800 writing forum.forum to 2016-04-20T22:44:38.722+0800 writing forum.mvnforummember to 2016-04-20T22:44:38.723+0800 done dumping forum.forum (1 document) 2016-04-20T22:44:38.790+0800 done dumping forum.mvnforummember (4075 documents)

因為我們要將這些數據導入到Linux系統中的MongoDb服務器,所以需要的json數據。

  • 導入Mongo數據到Linux系統中的MongoDb服務器中 命令是: mongoimport -d forum -c member -f member.json

如果是剛才使用mongodump導出的數據那么需要使用配套的mongostore命令來恢復數據。 mongorestore -d forum -c mvnforummember --dir=./dump/forum/mvnforummember.bson

總結

以上是生活随笔為你收集整理的mongodb启动成功连不上_MySQL数据导入到MongoDb教程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。