-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDB on centOs.txt
93 lines (74 loc) · 3.59 KB
/
MongoDB on centOs.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Only on CentOs and RedHat
Firstly Update your repositories
- sudo yum update -y
Disable selinux
- sudo vi /etc/selinux/config , change selinux=enforcing to selinux=disabled
Note: Type commands from Root or use "Sudo" infront of most commands.
$ is not in root
# is in root
I suggest to run all this commands from user, not from the root. (recommended)
1)Open VI editor
- sudo vi /etc/yum.repos.d/mongodb-org-3.6.repo (use sudo if its not root)
2)copy below details in vi editor.
right click on screen to copy, make sure you copied it correctly.
This is path for required repository.
press ESC button and then "i" for insert data.
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
3)Now press ESC button and then ":", then "wq!" to copy and quit from editor.
4)Before we move on, we should verify that the MongoDB repository exists within the yum utility.
The repolist command displays a list of enabled repositories:
- sudo yum repolist (use sudo if its not root)
5)Now install MongoDB server into system.(use sudo if its not root)
- sudo yum install -y mongodb-org
incase if you are using fedora
dnf install mongodb
6)you can start mongodb directly (use sudo if its not root)
- sudo systemctl start mongod
- sudo systemctl restart mongod for restart
- sudo systemctl stop mongod for stop
Incase you want mongodb to autostart on system boot
- sudo chkconfig mongod on (or) systemctl enable mongod
7)Check the installation sucesseded:(use sudo if its not root)
- tail /var/log/mongodb/mongod.log
8)check the version(use sudo if its not root)
- mongod --version
9)connect the mongodb commandline
- mongo
10)Sample commands to test MongoDB
> db - list databases
> use mydb - will create and take you to "mydb" database.
>show collections - list collections
>db.createCollection("nameit", {capped: false}) to create collection
> db.collectionname.save({a:1}) - will create "collectionname" container
in "mydb" database and adds value of a:1. If you didn't create any collection before, it will create for you.
> db.collectionname.find() - will show all the data in the "test" container.
> exit - for exit from db server.
> db.help() - any help required.
Other Useful things:
1)To check configurations of MongoDB
- go to /etc/mongod.conf
- uncomment the security section and add host addresses or 0.0.0.0 for access from any network,
which will be commented out in default.
2)Adding database and users
- use database_name, will create database with that name.
To create admin user, type below command with required/appropriate values.
- db.createUser({user: "my-admin", pwd: "password", roles:[{role: "userAdminAnyDatabase", db: "admin"}]})
To login into db as that user
- mongo -u my-admin -p --authenticationDatabase admin
And before adding users, create a new database to store user data, so create a new database.
- use user-details for "user-details as a database.
Just to give read/write role to a new user to multiple databases at a time
- db.createUser({user: "read-user", pwd: "password", roles:[{role: "read", db: "db_name"}, {role:"readWrite", db: "db_name"}]})
3) If you are planning to store data at specific folder, other than dafault var/lib/mongo
create you database folder
- mkdir -p /data/db
Give permissions to the default mongodb user 'mongod'
- chmod -R 777 /data/db
- chown -R mongod:mongod /data/db (default user mongod, default group mongod)
Thanks and Regards
Anugnu veeravathni