Installation Environment:
Centos 7
安装包 4.x MongoDB版本
1. Install MongoDB with tgz package
Official website download link: Portal
Unzip command:
Note that it is the tgz package
tar -zxvf mongodb 的 tgz 包名
Unzip the folder and rename it to mongodb:
mv mongodb 的 tgz 包名 mongodb
mongodb.conf configuration file:
#指定数据路径
dbpath=/home/mongodb/data
#指定MongoDB日志文件
logpath=/home/mongodb/mongodb.log
# 使用追加的方式写日志
logappend=true
#端口号
port=27017
#方便外网访问,外网所有ip都可以访问,不要写成固定的linux的ip
bind_ip=0.0.0.0
fork=true # 以守护进程的方式运行MongoDB,创建服务器进程
#auth=true #启用用户验证
#bind_ip=0.0.0.0 #绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定则默认本地所有IP
#开启登录认证
#
auth = true
2. Error reporting problem - start without the “–fork”
The problem is that the two folders data and log are not created
Order:
mkdir {
data, log}
2.1 Directory authority assignment:
chmod 777 data/
chmod 777 log/
2.2 Setting system variables
#修改/etc/profile,添加环境变量,方便执行MongoDB命令
export MONGODB_HOME=/home/mongodb
PATH=$PATH:$MONGODB_HOME/bin
Refresh the /etc/profiel configuration file
source /etc/profile
Start mongodb in configuration file mode
2.3 Start mongodb as a configuration file
Start command: -f
start as a daemon
mongod -f /home/mongodb/mongodb.conf
3. Connect mongo command
One thing to note is that if you use mongo directly, you will only use the default ip + port (ie 127.0.0.1: 27017
)
If you want to access the specified port command as follows:
mongo
或
mongo --host=127.0.0.1 --port=9567
Fourth, delete the mongodb database
Simply delete the mongodb/
entire folder.
5. View MongoDB database and create Mongodb database
mongodb3.0 has no admin database
The installation and test version of MongoDB this time is version 4.x
5.1 View MongoDB database
show dbs can view the existing database of Mongodb
show dbs
display result is empty
That is to say, before using show dbs
the command , the 4.x MongoDB version does not have any database, and it is empty
5.2 Create a MongoDB database
Create the database admin manually below
use DATABASE_NAME
If the database does not exist, create the database, otherwise switch to the specified database.
Note: In MongoDB, the collection will only be created after the content is inserted! That is to say, after the collection (data table) is created, a document (record) must be inserted before the collection is actually created.
db
Command to view the current database name
db
Six, custom user Create user, set account, password, permission
If you encounter problems with MongoDB user access rights, please note that if you start MongoDB with a configuration file, please comment it out auth = True
and cancel the login verification.
role
: Specified User Rights
db
: Specified Database
// admin数据库 创建管理员账号
> use admin
switched to db admin
> db.createUser({ user: "admin", pwd: "password", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
>
// 创建root账号
> db.createUser({
user: "root",pwd: "123456", roles: [ { role: "root", db: "admin" } ]})
If the user already exists, the following error will be reported:
// 进入自己的数据库,创建自己数据库的管理角色
> use test
> db.createUser({
user: "test",pwd: "123456",roles: [ {
role: "dbOwner", db: "test" } ]})
>
7. Description of User Permissions and Roles
rule | illustrate |
---|---|
root | Only available in the admin database. super account, super authority |
Read | Allow the user to read the specified database |
readWrite | Allow the user to read and write the specified database |
dbAdmin | Allows users to perform administrative functions in the specified database, such as index creation, deletion, viewing statistics or accessing system.profile |
userAdmin | Allow users to write to the system.users collection, you can create, delete and manage users in the specified database |
clusterAdmin | It is only available in the admin database, granting the user the administrative authority for all shard and replica set related functions |
readAnyDatabase | Only available in the admin database, granting the user read access to all databases |
readWriteAnyDatabase | Only available in the admin database, giving users read and write permissions for all databases |
userAdminAnyDatabase | Only available in the admin database, giving the user userAdmin permissions for all databases |
dbAdminAnyDatabase | Only available in the admin database, giving the user dbAdmin privileges for all databases |
dbOwner | (including dbAdmin, readWrite, userAdmin) |
reference link
8. Commonly used MongoDB commands
8.1 User login, view table (set) in database, view data item (row data/document) in table (set), update user, delete user
The author records:
Compared with relational database commands, such as MySQL database, the use database name when switching database commands is the same as the MongoDB command, except that MongoDB does not need to create a tedious database table structure like MySQL, and MongoDB can easily Automatically create the database yourself.
8.1.1 Automatically create or automatically switch the current database
use test
: Indicates that the test database will be used, that is, the use command will operate in the test database. (use command, this must be the first command after the successful connection to mongodb, only after use
the command , you can use db.auth()
the command to log in to the user)
8.1.2 User login
db.auth()
: Authorize the test database, namelyLog in to the MongoDB database, otherwise it will not operate normally,Normal login return value is 1, That is to say, if you are using the mongo command and successfully connected to mongodb, you should use a certain database first db.auth(用户名, 用户密码)
, otherwise you will not be able to do it later, because this is a newly installed mongodb database and there are no users.
Error: logical sessions can't have multiple authenticated users
The reason is that mongodb does not allow multiple users to be authenticated in one session.
(One thing to note: after using a certain database, it is best not to db.auth()
enter two different users to log in to the current shell session, otherwise an error will be reported logical sessions can't have multiple authenticated users
, that is to say, a session, directly use a certain database, and then db.auth() Log in as a user who has permission to access the database. If two users are accidentally logged in, log out directly, use mongo
the command to connect to the mongodb database again, and repeat the previous use
and d b.auth()
commands)
show collections
: Display a list of all collections in the test database.
db.test.find()
: View a list of documents in the test collection.
Take the admin database as an example:
(Because of the current 4.x Mongodb version, the admin database here is newly created, so there is no document data, and the find()
command finds out that it is empty.)
Other commonly used commands, taking the test user as an example:
update user
db.updateUser(test, writeConcern)
Ditto, capital U
delete users
db.dropUser('test')
Note: not dropuser()
but dropUser()
with a capital U
Generally speaking, use to the specified database, and then use the db.dropUser() command to delete the specified user
show users
db.dropUser('test')
8.2 User remote login
shell terminal connection ( mongo ip:端口/数据库名 -u 用户名 -p 用户密码
)
mongo 10.128.218.14:27017/database -u username -p password
reference link
2. MongoDB:To see additional information in this output, start without the “–fork“ option