MongoDB与Mysql常用命令解释
原文
本文旨在介紹MongoDB,Mysql的常用命令:將MongoDB 和傳統(tǒng)的關(guān)系型數(shù)據(jù)庫的常用命令對照起來學(xué)習(xí),更加便于記憶和理解。
MongoDB是由數(shù)據(jù)庫(database/repository)、集合(collection)、文檔對象(document)三個層次組成。MongoDB中集合對應(yīng)關(guān)系型數(shù)據(jù)庫里的表,但是集合中沒有列、行和關(guān)系的概念,這體現(xiàn)了模式自由的特點。
?
傳統(tǒng)的關(guān)系數(shù)據(jù)庫一般由數(shù)據(jù)庫(database)、表(table)、記錄(record)三個層次概念組成,?
?
常用命令介紹:
?
| MySQL | MongoDB | 說明 |
| mysqld | mongod | 服務(wù)器守護進程 |
| mysql | mongo | 客戶端工具 |
| mysqldump | mongodump | 邏輯備份工具 |
| mysql | mongorestore | 邏輯恢復(fù)工具 |
| ? | db.repairDatabase() | 修復(fù)數(shù)據(jù)庫 |
| mysqldump | mongoexport | 數(shù)據(jù)導(dǎo)出工具 |
| source | mongoimport | 數(shù)據(jù)導(dǎo)入工具 |
| grant * privileges on *.* to … | Db.addUser() Db.auth() | 新建用戶并權(quán)限 |
| show databases | show dbs | 顯示庫列表 |
| Show tables | Show collections | 顯示表列表 |
| Show slave status | Rs.status | 查詢主從狀態(tài) |
| Create table users(a int, b int) | db.createCollection("mycoll", {capped:true, size:100000})?另:可隱式創(chuàng)建表。 | 創(chuàng)建表 |
| Create INDEX idxname ON users(name) | db.users.ensureIndex({name:1}) | 創(chuàng)建索引 |
| Create INDEX idxname ON users(name,ts DESC) | db.users.ensureIndex({name:1,ts:-1}) | 創(chuàng)建索引 |
| Insert into users values(1, 1) | db.users.insert({a:1, b:1}) | 插入記錄 |
| Select a, b from users | db.users.find({},{a:1, b:1}) | 查詢表 |
| Select * from users | db.users.find() | 查詢表 |
| Select * from users where age=33 | db.users.find({age:33}) | 條件查詢 |
| Select a, b from users where age=33 | db.users.find({age:33},{a:1, b:1}) | 條件查詢 |
| select * from users where age<33 | db.users.find({'age':{$lt:33}}) | 條件查詢 |
| select * from users where age>33 and age<=40 | db.users.find({'age':{$gt:33,$lte:40}}) | 條件查詢 |
| select * from users where a=1 and b='q' | db.users.find({a:1,b:'q'}) | 條件查詢 |
| select * from users where a=1 or b=2 | db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } ) | 條件查詢 |
| select * from users limit 1 | db.users.findOne() | 條件查詢 |
| select * from users where name like "%Joe%" | db.users.find({name:/Joe/}) | 模糊查詢 |
| select * from users where name like "Joe%" | db.users.find({name:/^Joe/}) | 模糊查詢 |
| select count(1) from users | Db.users.count() | 獲取表記錄數(shù) |
| select count(1) from users where age>30 | db.users.find({age: {'$gt': 30}}).count() | 獲取表記錄數(shù) |
| select DISTINCT last_name from users | db.users.distinct('last_name') | 去掉重復(fù)值 |
| select * from users ORDER BY name | db.users.find().sort({name:-1}) | 排序 |
| select * from users ORDER BY name DESC | db.users.find().sort({name:-1}) | 排序 |
| EXPLAIN select * from users where z=3 | db.users.find({z:3}).explain() | 獲取存儲路徑 |
| update users set a=1 where b='q' | db.users.update({b:'q'}, {$set:{a:1}}, false, true) | 更新記錄 |
| update users set a=a+2 where b='q' | db.users.update({b:'q'}, {$inc:{a:2}}, false, true) | 更新記錄 |
| delete from users where z="abc" | db.users.remove({z:'abc'}) | 刪除記錄 |
| ? | db. users.remove() | 刪除所有的記錄 |
| drop database IF EXISTS test; | use test db.dropDatabase() | 刪除數(shù)據(jù)庫 |
| drop table IF EXISTS test; | db.mytable.drop() | 刪除表/collection |
| ? | db.addUser(‘test’, ’test’) | 添加用戶 readOnly-->false |
| ? | db.addUser(‘test’, ’test’, true) | 添加用戶 readOnly-->true |
| ? | db.addUser("test","test222") | 更改密碼 |
| ? | db.system.users.remove({user:"test"}) 或者db.removeUser('test') | 刪除用戶 |
| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | use admin | 超級用戶 |
| ? | db.auth(‘test’, ‘test’) | 用戶授權(quán) |
| ? | db.system.users.find() | 查看用戶列表 |
| ? | show users | 查看所有用戶 |
| ? | db.printCollectionStats() | 查看各collection的狀態(tài) |
| ? | db.printReplicationInfo() | 查看主從復(fù)制狀態(tài) |
| ? | show profile | 查看profiling |
| ? | db.copyDatabase('mail_addr','mail_addr_tmp') | 拷貝數(shù)據(jù)庫 |
| ? | db.users.dataSize() | 查看collection數(shù)據(jù)的大小 |
| ? | db. users.totalIndexSize() | 查詢索引的大小 |
?
?
?mongodb語法
?
?
MongoDB的好處挺多的,比如多列索引,查詢時可以用一些統(tǒng)計函數(shù),支持多條件查詢,但是目前多表查詢是不支持的,可以想辦法通過數(shù)據(jù)冗余來解決多表查詢的問題。
MongoDB對數(shù)據(jù)的操作很豐富,下面做一些舉例說明,內(nèi)容大部分來自官方文檔,另外有部分為自己理解。
?
查詢colls所有數(shù)據(jù)
db.colls.find() //select * from colls
?
通過指定條件查詢
db.colls.find({‘last_name’: ‘Smith’});//select * from colls where last_name=’Smith’
?
指定多條件查詢
db.colls.find( { x : 3, y : “foo” } );//select * from colls where x=3 and y=’foo’
?
指定條件范圍查詢
db.colls.find({j: {$ne: 3}, k: {$gt: 10} });//select * from colls where j!=3 and k>10
?
查詢不包括某內(nèi)容
db.colls.find({}, {a:0});//查詢除a為0外的所有數(shù)據(jù)
?
支持<, <=, >, >=查詢,需用符號替代分別為$lt,$lte,$gt,$gte
db.colls.find({ “field” : { $gt: value } } );?
db.colls.find({ “field” : { $lt: value } } );?
db.colls.find({ “field” : { $gte: value } } );
db.colls.find({ “field” : { $lte: value } } );
?
也可對某一字段做范圍查詢
db.colls.find({ “field” : { $gt: value1, $lt: value2 } } );
?
不等于查詢用字符$ne
db.colls.find( { x : { $ne : 3 } } );
?
in查詢用字符$in
db.colls.find( { “field” : { $in : array } } );
db.colls.find({j:{$in: [2,4,6]}});
?
not in查詢用字符$nin
db.colls.find({j:{$nin: [2,4,6]}});
?
取模查詢用字符$mod
db.colls.find( { a : { $mod : [ 10 , 1 ] } } )// where a % 10 == 1
?
$all查詢
db.colls.find( { a: { $all: [ 2, 3 ] } } );//指定a滿足數(shù)組中任意值時
?
$size查詢
db.colls.find( { a : { $size: 1 } } );//對對象的數(shù)量查詢,此查詢查詢a的子對象數(shù)目為1的記錄
?
$exists查詢
db.colls.find( { a : { $exists : true } } ); //?存在a對象的數(shù)據(jù)
db.colls.find( { a : { $exists : false } } ); //?不存在a對象的數(shù)據(jù)
?
$type查詢$type值為bsonhttp://bsonspec.org/數(shù)?據(jù)的類型值
db.colls.find( { a : { $type : 2 } } ); //?匹配a為string類型數(shù)據(jù)
db.colls.find( { a : { $type : 16 } } ); //?匹配a為int類型數(shù)據(jù)
?
使用正則表達式匹配
db.colls.find( { name : /acme.*corp/i } );//類似于SQL中l(wèi)ike
?
內(nèi)嵌對象查詢
db.colls.find( { “author.name” : “joe” } );
?
1.3.3版本及更高版本包含$not查詢
db.colls.find( { name : { $not : /acme.*corp/i } } );
db.colls.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );
?
sort()排序
db.colls.find().sort( { ts : -1 } );//1為升序2為降序
?
limit()對限制查詢數(shù)據(jù)返回個數(shù)
db.colls.find().limit(10)
?
skip()跳過某些數(shù)據(jù)
db.colls.find().skip(10)
?
snapshot()快照保證沒有重復(fù)數(shù)據(jù)返回或?qū)ο髞G失
?
count()統(tǒng)計查詢對象個數(shù)
db.students.find({‘a(chǎn)ddress.state’ : ‘CA’}).count();//效率較高
db.students.find({‘a(chǎn)ddress.state’ : ‘CA’}).toArray().length;//效率很低
?
group()對查詢結(jié)果分組和SQL中g(shù)roup by函數(shù)類似
?
distinct()返回不重復(fù)值
連接MYSQL
格式: mysql -h主機地址 -u用戶名 -p用戶密碼
1、例1:連接到本機上的MYSQL。
首先在打開DOS窗口,然后進入目錄 mysqlbin,再鍵入命令mysql -uroot -p,回車后提示你輸密碼,如果剛安裝好MYSQL,超級用戶root是沒有密碼的,故直接回車即可進入到MYSQL中了,MYSQL的提示符是:mysql>
2、例2:連接到遠程主機上的MYSQL。假設(shè)遠程主機的IP為:110.110.110.110,用戶名為root,密碼為abcd123。則鍵入以下命令:
mysql -h110.110.110.110 -uroot -pabcd123
(注:u與root可以不用加空格,其它也一樣)
3、退出MYSQL命令: exit (回車)
注意:想要成功連接到遠程主機,需要在遠程主機打開MySQL遠程訪問權(quán)限
方法如下:
在遠程主機中以管理員身份進入
輸入如下命令
mysql>GRANT ALL PRIVILEGES ON *.* TO 'agui'@%'IDENTIFIED BY '123' WITH GRANT OPTION;
FLUSH PRIVILEGES;
//賦予任何主機訪問數(shù)據(jù)的權(quán)限
mysql>FLUSH PRIVILEGES
//修改生效
agui為我們使用的用戶名
密碼為123
即:在遠程主機上作好設(shè)置,我們即可通過mysql -h110.110.110.110 -uagui -p123連接進遠程主機修改和取消MySQL超級用戶root密碼
(一)、密碼的修改:使用mysqladmin命令?
1、例如你的 root用戶現(xiàn)在沒有密碼,你希望的密碼修改為abc,那么命令是:mysqladmin -u root password abc?
2、如果你的root現(xiàn)在有密碼了,那么修改密碼為abc的命令是:mysqladmin -u root -p password ‘newpassword'
注意,命令回車后會問你舊密碼,輸入舊密碼之后命令完成,密碼修改成功。?
?
????????? (二)、密碼的消除?
1、以root登錄:mysql -u root -p?
2、mysql>use mysql;?
3、mysql>update user set password='' where user='root';?
重啟mysql服務(wù)就生效了。
?
修改Mysql中普通用戶的密碼:
???????1.直接在數(shù)據(jù)庫中修改記錄
mysql> use mysql
mysql> update user set password = password(”new_password”) where user = “user_name”;
mysql> flush privileges;
其實這種方法就是更新一條數(shù)據(jù)庫記錄,與普通update語句不同的是,密碼加密存儲,需用password()函數(shù)來生成,另一個不同點是需要刷新權(quán)限表。
???????2.在數(shù)據(jù)庫中運行set password
mysql> set password for user_name = password(”new_password”);
mysql> flush privileges;
同第一種方法,也要刷新權(quán)限表
???????3.直接在shell環(huán)境運行mysqladmin
> mysqladmin -u user_name -p password “new_password”
> mysqladmin flush-privileges
這個方法我試了幾次,每次都能將密碼記錄修改掉,但是每次修改后都無法登錄,即使重啟數(shù)據(jù)庫也無濟于事。所以建議不要采用本方法修改用戶密碼,尤其是root密碼。
?????? 4.grant all privileges on db.table to?user_name@localhost?identified by “your_pwd”;
用戶名密碼的生效不必用flush privileges刷新
注:
db.table: db表示授權(quán)哪個庫,table是相應(yīng)庫里的表。可以用*.*表示所有庫所有表。注意,如果想表示某個庫的所有表,必須用db_name.*,后面的”.*”不可省略,否則權(quán)限將無法賦予。
user_name@localhost:?user_name表示用戶名,localhost表示該用戶只能在本地訪問該庫,可以用%表示從任何地方訪問該庫,也可以用111.11.22.33來表示地址
your_pwd: 給用戶設(shè)置的密碼
?
Mysql創(chuàng)建普通用戶
1.使用
insert into mysql.user(Host,User,Password) values("localhost","ea",password("ea"));
時有可能遇到:Field 'ssl_cipher' doesn't have a default value的錯誤。
GRANT USAGE ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
“username”替換為將要授權(quán)的用戶名,比如clientusr;
“password”替換為clientusr設(shè)置的密碼;
locaohost可以改為%,方便你從別的IP登錄。
如:GRANT USAGE ON *.* TO 'ea'@'localhost' IDENTIFIED BY 'ea' WITH GRANT OPTION;
?
然后對你建的用戶進行授權(quán)
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON tablename.*? TO 'username'@'localhost' IDENTIFIED BY 'password';
本語句中的權(quán)限根據(jù)實際需要確定:
"tablename"替換為授權(quán)訪問的數(shù)據(jù)表table名
"username"是步驟2授權(quán)用戶名
"password"是步驟2授權(quán)用戶的設(shè)置密碼
這樣就為該用戶授予了對某數(shù)據(jù)表的SELECT, INSERT, UPDATE, DELETE, CAREATE, DROP權(quán)限。
如: GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON easy_activity.*? TO 'ea'@'localhost' IDENTIFIED BY 'ea';
?
生效授權(quán):一句話即可:FLUSH PRIVILEGES;
?
用MySQL創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)庫表
1、使用SHOW語句找出在服務(wù)器上當(dāng)前存在什么數(shù)據(jù)庫:
| mysql> SHOW DATABASES; +----------+ | Database | +----------+ | mysql | | test | +----------+ 3 rows in set (0.00 sec) |
2、創(chuàng)建一個數(shù)據(jù)庫abccs?
mysql> CREATE DATABASE abccs;?
注意不同操作系統(tǒng)對大小寫的敏感。?
3、選擇你所創(chuàng)建的數(shù)據(jù)庫?
mysql> USE abccs?
Database changed?
此時你已經(jīng)進入你剛才所建立的數(shù)據(jù)庫abccs.?
4、 創(chuàng)建一個數(shù)據(jù)庫表?
首先看現(xiàn)在你的數(shù)據(jù)庫中存在什么表:?
mysql> SHOW TABLES;?
Empty set (0.00 sec)?
說明剛才建立的數(shù)據(jù)庫中還沒有數(shù)據(jù)庫表。下面來創(chuàng)建一個數(shù)據(jù)庫表mytable:???我們要建立一個你公司員工的生日表,表的內(nèi)容包含員工姓名、性別、出生日期、出生城市。?
| mysql> CREATE TABLE mytable (name VARCHAR(20), sex CHAR(1), -> birth DATE, birthaddr VARCHAR(20)); Query OK, 0 rows affected (0.00 sec) |
由 于name、birthadd的列值是變化的,因此選擇VARCHAR,其長度不一定是20。可以選擇從1到255的任何長度,如果以后需要改變它的字 長,可以使用ALTER TABLE語句。);性別只需一個字符就可以表示:"m"或"f",因此選用CHAR(1);birth列則使用DATE數(shù)據(jù)類型。?
創(chuàng)建了一個表后,我們可以看看剛才做的結(jié)果,用SHOW TABLES顯示數(shù)據(jù)庫中有哪些表:?
| mysql> SHOW TABLES; +---------------------+ | Tables in menagerie | +---------------------+ | mytables | +---------------------+ |
5、顯示表的結(jié)構(gòu):?
| mysql> DESCRIBE mytable; +-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | deathaddr | varchar(20) | YES | | NULL | | +-------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) |
6、 往表中加入記錄?
我們先用SELECT命令來查看表中的數(shù)據(jù):?
mysql> select * from mytable;?
Empty set (0.00 sec)
這說明剛才創(chuàng)建的表還沒有記錄。?加入一條新記錄:?
| mysql> insert into mytable -> values (′abccs′,′f′,′1977-07-07′,′china′); Query OK, 1 row affected (0.05 sec) |
再用上面的SELECT命令看看發(fā)生了什么變化。我們可以按此方法一條一條地將所有員工的記錄加入到表中。?
7、用文本方式將數(shù)據(jù)裝入一個數(shù)據(jù)庫表?
如果一條一條地輸入,很麻煩。我們可以用文本文件的方式將所有記錄加入你的數(shù)據(jù)庫表中。創(chuàng)建一個文本文件“mysql.txt”,每行包含一個記錄,用定位符(tab)把值分開,并且以在CREATE TABLE語句中列出的列次序給出,例如:?
| abccs f 1977-07-07 china mary f 1978-12-12 usa tom m 1970-09-02 usa |
使用下面命令將文本文件“mytable.txt”裝載到mytable表中:mysql> LOAD DATA LOCAL INFILE "mytable.txt" INTO TABLE pet;?
再使用如下命令看看是否已將數(shù)據(jù)輸入到數(shù)據(jù)庫表中:mysql> select * from mytable;
總結(jié)
以上是生活随笔為你收集整理的MongoDB与Mysql常用命令解释的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于BASYS2的VHDL程序——交通灯
- 下一篇: 简单工厂模式与工厂方法模式