# Creating a new Database (RethinkDB, MongoDB and Redis)
# I- RethinkDB
A rethinkDB instance usually need a minimum of 4GB of RAM
# 1. Installation of "rethinkdb"
wget -qO- https://download.rethinkdb.com/repository/raw/pubkey.gpg | sudo gpg --dearmor -o /usr/share/keyrings/rethinkdb-archive-keyrings.gpg
echo "deb [signed-by=/usr/share/keyrings/rethinkdb-archive-keyrings.gpg] https://download.rethinkdb.com/repository/ubuntu-$(lsb_release -cs) $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
sudo apt-get update
sudo apt-get install rethinkdb
rethinkdb --version
# 2. Database configurations
sudo cp /etc/rethinkdb/default.conf.sample /etc/rethinkdb/instances.d/instance1.conf
nano /etc/rethinkdb/instances.d/instance1.conf
systemctl restart rethinkdb.service
/etc/init.d/rethinkdb status
# 3. If the database is restored from another server
On the first server to export the database use
rethinkdb dump⟶ Exporting all databases
rethinkdb dump -e "DATABASE_NAME"⟶ Exporting "DATABASE_NAME" database
wget http://LINK_TO_YOUR_TAR.GZ_DUMP_FILE
apt install python3-pip
pip install rethinkdb
rethinkdb restore YOUR_TAR.GZ_DUMP_FILE
# If database is password protected use
# rethinkdb restore -p YOUR_TAR.GZ_DUMP_FILE
systemctl restart rethinkdb.service
/etc/init.d/rethinkdb status
# 4. To delete all the databases from a rethinkdb server ⚠️⚠️
Go to server's terminal and execute (To auto close RethinkDB GUI)
ufw allow 8080;ufw reload;clear;echo "RethinkDB GUI is running ...";echo; echo "Press 'Q' on keyboard to stop RethinkDB GUI";echo "AUTO-CLOSE after 120 seconds session"; read -t 120 -n 1 key; if [ $? -eq 0 ]; then echo "Quitting RethinkDB GUI"; else echo "aborted | Quitting RethinkDB GUI"; fi ; ufw deny 8080;ufw reload;echo "RethinkDB GUI is closed"
Replace the
inputarray with the result ofr.dbList()You can delete the database
rethinkdbfrom the list
// r.dbList() output
const input = [
"database1" ,
"database2" ,
"database3" ,
"database4" ,
"database5"
];
let result = "r.dbDrop(\"" + input[0] + "\")";
for (let i = 1; i < input.length; i++) {
result += "\n .do(function() {\n return r.dbDrop(\"" + input[i] + "\")\n })";
}
console.log(result);
Now copy the result (The RethinkDB query) and use it to delete all databases
# 5. Firewall configurations
ufw status
List all available apps
ufw app list
List all used ports
sudo lsof -i -P -n | grep LISTEN
ufw allow 'OpenSSH'
ufw allow 29015
ufw allow 28015
ufw enable
ufw reload
# 6. Create a password and any other modifications
** ⚠️⚠️ Warning | This tutorial will make your database server available to public internet ⚠️⚠️**
Change bind=127.0.0.1 ⟶ to ⟶ bind=0.0.0.0
nano /etc/rethinkdb/instances.d/instance1.conf
Be sure that port 8080 is allowed in the firewall
ufw allow 8080
ufw reload
/etc/init.d/rethinkdb restart
Go to http://SERVER_IP_ADDRESS:8080
Go to http://SERVER_IP_ADDRESS:8080/#dataexplorer
Execute:
r.db("rethinkdb").table("users").get('admin').update({password: 'YOUR_RETHINKDB_PASSWORD'})
Go back to server
Change bind=0.0.0.0 ⟶ to ⟶ bind=127.0.0.1
nano /etc/rethinkdb/instances.d/instance1.conf
/etc/init.d/rethinkdb restart
/etc/init.d/rethinkdb status
# Be sure that port 8080 is allowed in the firewall
ufw deny 8080
ufw reload
# II- MongoDB
# 1. Installation of "mongodb-org"
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
apt-key list
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
apt update
sudo apt install mongodb-org
sudo systemctl start mongod.service
sudo systemctl status mongod.service
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
# 2. Database configurations and password creation
Change bindIp: 127.0.0.1 ⟶ to ⟶ bindIp: 0.0.0.0
nano /etc/mongod.conf
sudo systemctl restart mongod.service
sudo systemctl status mongod.service
Go to mongo shell
mongo
Execute the following commands
db.adminCommand( { listDatabases: 1 } );
use admin;
db.createUser({
user: "admin",
pwd: "YOUR_MONGODB_PASSWORD",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" },
"root"
]
});
//db.createUser( { user: "admin", pwd: "YOUR_MONGODB_PASSWORD", roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: "readWriteAnyDatabase", db: "admin" }, "root" ] } );
exit;
Add this security settings to '/etc/mongod.conf'
#security:
security:
authorization: enabled
nano /etc/mongod.conf
sudo systemctl restart mongod.service
sudo systemctl status mongod.service
# 3. If the database is restored from another server
On the first server to export the database use
mongodump⟶ Exporting all databases
mongodump -d "DATABASE_NAME"⟶ Exporting "DATABASE_NAME" databaseThen
tar -zcvf ./mongo_backup_dump.tar.gz ./dump/To compress it
wget http://LINK_TO_YOUR_TAR.GZ_DUMP_FILE
tar -xvzf mongo_backup_dump.tar.gz
mongorestore -u admin -p MONGODB_PASSWORD ./dump/
sudo systemctl restart mongod.service
sudo systemctl status mongod.service
If the
mongodbis password protected usemongodump -u admin -p "<PASSWORD_HERE>" --authenticationDatabase=admin
# 4. To delete all the databases from a mongodb server ⚠️⚠️
mongo --eval
# If database is password protected use
# mongo -u admin -p <DB_PASSWORD>
Now you will be inside mongo cli
mongo>Execute the following command to delete all databases
db.getMongo().getDBNames().forEach(function(dbName) { if('admin|config|local|test'.includes(dbName)) return;db = db.getSiblingDB(dbName); db.getCollectionNames().forEach(function(collectionName) { db.getCollection(collectionName).drop(); }); });
To list all databases
db.getMongo().getDBNames();
# 5. Firewall configurations
ufw status
List all available apps
ufw app list
List all used ports
sudo lsof -i -P -n | grep LISTEN
ufw allow 'OpenSSH'
ufw allow 27017
ufw enable
ufw reload
# III- Redis
# 1. Installation of "redis-server"
sudo apt install redis-server
Configure redis to listen to all interfaces and set a password
sudo nano /etc/redis/redis.conf
Comment `bind 127.0.0.1 ::1` to listen to all interfaces
Replace `# requirepass foobared` with `requirepass YOUR_REDIS_PASSWORD`
Restart redis service
sudo systemctl restart redis.service
Check redis status
sudo systemctl status redis
redis-cli
To implement redis in nodejs use
ioredispackage
npm i ioredis -s
# 2. Firewall configurations
ufw status
List all available apps
ufw app list
List all used ports
sudo lsof -i -P -n | grep LISTEN
ufw allow 'OpenSSH'
ufw allow 6379
ufw enable
ufw reload