To tell the truth, this is do not want to write this kind of foundation, but when I configure the online tutorial although a lot, but most are lack of arms and legs, or not detailed description, here I will press my deployment experience, hand in hand to teach again
If there is any missing or unclear description, please contact me. I will either not write this basic article, since I have written it, it will be updated continuously, just like the previous basic article to build the Go development environment in VsCode, teach you how to configure
This article is based on MongoDB4.0, CentOS environment setup deployment
This is a problem solving, no nonsense, follow the steps, we can deploy without thinking about anything
Deployment began
The Linux server deployed by some companies cannot connect to the Internet. In this case, upload the compressed package to the Linux server in FTP mode and decompress the package
Tar -xvzf mongodb-linux-x86_64-3.2.10. TGZ // Decompress // The ROOT permission is required. Mv mongodb-linux-x86_64-3.2.10 /usr/local/mongodb // Move the decompressed file to the specified directory and rename itcd /usr/local/mongoDB/ // Switch to the migrated mongoDB directoryCopy the code
If you can connect to the Internet, MongoDB’s official website is quite good. After all, I have not done the Internet download, SO I will not write
Okay, so let’s move on to what we did after we unzipped
Create data directory
cd /usr/localMkdir data/db // Create a db directory for storing database data mkdirlog/ / createlogLog directory for storing log dataCopy the code
Config File Configuration
Start by creating our configuration file
cd /usr/localVim conf/ mongodb. conf/ / Create and open the conf fileCopy the code
Then fill out our Conifg file configuration
# mongodb config file
port=27888 # port192.168.105.142 bind_ip = 127.0.0.1# enable extranet access 0.0.0.0
dbpath=/usr/local/mongoDB/data/db # database storage
logpath=/usr/local/mongoDB/log/mongodb.log Log file storage
fork=true # Set up background run
# auth=true
Copy the code
Note auth=true first, because the newly deployed MongoDB has not created users. If this function is enabled directly, MongoDB cannot be started
Therefore, if you want to set access to MongoDB, after setting the account password, uncomment auth=true and restart MongoDB
Profile configuration
Configuration of the system profile, which is a necessary step for each software installation, add environment variables to the end of the profile
vim /etc/profile
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
Copy the code
If you don’t like vim for text addition, you can also use Echo to append the end of the file’s content
echo 'export MONGODB_HOME=/usr/local/mongodb' >> /etc/profile
echo 'export PATH=$PATH:$MONGODB_HOME/bin' >> /etc/profile
Copy the code
You can perform either of the preceding operations. However, you need to restart the system configuration for both operations to take effect
source /etc/profile
Copy the code
At this point, MongoDB has been installed and configured. Now we need to start MongoDB
Start the mongo
Since the startup parameters are configured in config, we directly start the startup in config form
mongod --config .. /conf/mongodb.confCopy the code
Create users and assign permissions
Here I suggest using Studio 3T to connect to our MongoDB server to create users, for no other reason than simplicity
How do we set user permissions in Studio 3T
Select the library to which you want to set permissions and add users
Assign permissions to users
For non-admin libraries, users generally only need to assign readWrite permission
For the admin library, we need to keep an administrator account and assign root permission in case of need. However, in normal operations, do not use the root user if possible. It is easy to perform some dangerous operations and may cause some files that can only be operated by the root user. In other words, it affects the stability of the database
Create user admin admin admin admin admin admin admin admin admin admin admin admin admin admin admin admin admin admin admin admin admin admin adminanydatabase
db.createUser(
{
user: "naonao".pwd: "123456",
roles:[
{role:"root",db:"admin"}
,{role:"userAdminAnyDatabase",db:"admin"}]})Copy the code
As you can see, there is a Json array inside roles, so if we only need one permission, keep the elements in the ROLES array just one
Log in to mongoDB using the specified account password and port
Turn off the MongoDB service and remove the user authentication auth=true comment from the configuration file before using user login
Go to the mongoDB/bin file path
cd /usr/mongoDB/bin
Copy the code
Then use the account password and specify the corresponding port to start MongoDB. If the default port is 27017, do not specify
mongo -u "username" -p "password" -authenticationDatabase "admin" --port 27888
Copy the code
The default authentication for an account is admin, so if the user belongs to the admin database, it is ok to leave –authenicationDatabase “admin” out
If the username and password used belong to another library, then you must specify the corresponding library. Change ‘admin’ to the corresponding library of the login user
Close the mongo
> use admin
> db.shutdownServer()
Copy the code
Or simply kill the corresponding Mongo process
Query the MongoDB process first
ps -aux | grep mongo
Copy the code
KillallMongoDB all associated processes
killall mongo
Copy the code
Sometimes using kill to specify the corresponding PID of mongo can not end the process, and the mongo process still exists after killall is executed, so we have to use killall-9 mongo
However, killall-9 is a dangerous command. This powerful and dangerous command forces the process to terminate abruptly while running, and the process cannot clean itself up after it finishes. The hazard is that system resources cannot be released normally. It is not recommended unless other methods fail to be used.
Linux opens specified ports
MongoDB has now been built and can be used normally on Linux
If our MongoDB is still insecure about security after adding the account password, we can change our port instead of using the default port 27017
After the port change, we need to remember to open the corresponding port on Linux, otherwise the client will not be able to connect to our MongoDB server
You can enable the port in either of two ways. One is to enable the port after the restart. The other is permanent, and the restart of the server does not affect the opening of the port
Here I set MongoDB’s port to 27888
After Linux restarts, the port needs to be reopened:
iptables -I INPUT -p tcp --dport 27888 -j ACCEPT
Copy the code
Permanent: add the following command to /etc/sysconfig/iptables
-A INPUT -p tcp -m tcp --dport 27888 -j ACCEPT
# restart iptables
service iptables restart
Copy the code
If the iptables file doesn’t exist, we’ll just create one
After the server restarts, MongoDB is automatically started
Sometimes we will restart the server, but inevitably something will forget to open, or another colleague restarted the server, he does not know how to open
So we need to configure MongoDB to open automatically so that we can avoid a lot of unnecessary trouble
cd /etc/rc.d
Append MongoDB enable_mode to rc.local
echo '/usr/local/mongoDB/bin/mongod --config /usr/local/mongoDB/conf/mongodb.conf' >> rc.local
Copy the code
If you have custom ports for MongoDB, don’t forget to configure them to open automatically upon startup
Also write Linux shutdown/restart command here, can verify that we configure the restart effect of the content is successful
Linux shutdown/restart
Restart command: reboot (restart the computer now) shutdown now Shutdown command: shutdown -h nowCopy the code
Most of the time, we connect to Linux servers remotely, so just reboot. Don’t try shutdown. Remote connection of the case, off after you how boot??