Working with MongoDB as MySQL DBA. Comparing commands from MongoDB to MySQL, similarities and differences. Exploring replication features, failover and recovery, adjusting the variables and checking status and using DML, DDL with different storage engines
2. About us
Lead Database Consultant @Pythian
OSDB managed services since 2014
https://mk.linkedin.com/in/igorle
@igorLE
Lead Database Consultant
at @Pythian since 2015.
https://www.linkedin.com/in/martinarrieta
@MartinArrietaC
3. Overview
• What is MySQL, what MongoDB
• Replication in MongoDB compared to MySQL
• Resyncing nodes after crash
• Changing variables and configuration files
• Status check and processlist
• Security and authentication
• Schema design, DDL, DML
• Storage engine and concurrency
• Error and Slow query logs
• Backups and recovery
4. What is MySQL
• Open-source relational database management system
(RDBMS)
• Stores data in tables and uses Structured Query
Language (SQL) for database access
• Predefined database schema based requirements and
rules to govern the relationships between fields in your
tables
• Related information may be stored in separate tables,
but associated through the use of joins
• Data duplication is minimized
5. What is MongoDB
• Open-source database, stores data in JSON-like documents
that can vary in structure
• Related information is stored together for fast query access
through the MongoDB query language
• Uses dynamic schemas, creating records without first
defining the structure
• Changing the structure simply by adding new fields or
deleting existing ones
• Ability to represent hierarchical relationships, to store arrays,
and other more complex structures easily
• Documents in a collection need not have an identical set of
fields and denormalization of data is common
6. MySQL replication
• Group of mysqld processes that maintain the same data set
• Write operations go to the Master node, reads can go to
Slave
• All changes are recorded into binary log on the master
• Asynchronous replication, Semi-Synchronous replication,
virtually synchronous replication (Galera)
• Slave node replicates the events by copying binary logs as
relay logs
– IO Thread, SQL Thread
– Binary Logs format - Statement, Row, Mixed
• Expire logs days dynamic
• Replication filters
8. MongoDB Replication
• Group of mongod processes that maintain the same
data set
• Write operations go to the Primary node
• All changes are recorded on all nodes into operations
log - oplog
• Asynchronous replication, Statement based
• Secondaries replicate the Primary oplog
• Secondary can use sync source other Secondary
• Automatic failover
• Oplog size change
• Adding new node
10. Crash recovery
MySQL
• InnoDB does crash recovery
• MyISAM can repair tables
• Restore from backup - reseed replication
• Does not handle the quorum
MongoDB
• Primary node crash. Election of new Primary from majority
• Replay events from Oplog - idempotent
• Clear data dir and restart node. Initial Sync
• Repair database
11. Failover
MySQL
• There is no build-in automatic failover
– Alternatives can be MHA, HAProxy, ProxySQL, etc.
• No option for automated initial sync
MongoDB
• Primary nodes goes down for more than 10 seconds
– "heartbeatTimeoutSecs" : 10
• Secondary node holds election
• Secondary with majority of votes becomes new Primary
15. Security and authentication
MongoDB
security:
keyFile: <path-to-keyfile>
use admin
db.createUser( {
user: "admin",
pwd: "changeme1",
roles: [ { role:
"userAdminAnyDatabase", db: "admin" }
]
})
db.getSiblingDB("admin").auth("admin"
, "changeme1" )
MySQL
CREATE USER 'admin'@'localhost'
IDENTIFIED BY 'changeme1';
GRANT ALL ON app_db.*
TO 'admin' @'localhost';
There are not “Roles” in MySQL :(
MySQL 8 will support roles.
CREATE ROLE 'app_developer';
GRANT ALL ON app_db.* TO 'app_developer';
GRANT 'app_developer' TO
'dev1'@'localhost';
16. Security and authentication
MongoDB
Encrypt data in transit with SSL
/etc/mongod.conf
net:
ssl:
mode: requireSSL
PEMKeyFile:
/etc/ssl/mongodb.pem
CAFile: /etc/ssl/ca.pem
shell> mongo --ssl --host
hostname.example.com --sslCAFile
/etc/ssl/ca.pem
MySQL
Encrypt data in transit with SSL
/etc/my.cnf
[mysqld]
ssl-ca=ca.pem
ssl-cert=server-cert.pem
ssl-key=server-key.pem
mysql> GRANT USAGE ON *.*
TO user@'53.40.20.15' REQUIRE SSL;
shell> mysql --ssl-ca=ca.pem
--ssl-cert=client-cert.pem
--ssl-key=client-key.pem
17. Schema design, DDL, DML
MongoDB
PRIMARY> use test
switched to db test
PRIMARY> db.createCollection(
"users",
{ validator: { $and:
[ { name: { $type: "string" } },
{ status: { $in: [ "A", "P" ] } }
]
}
}
)
* Available from MongoDB 3.2
MySQL
mysql> CREATE DATABASE test;
mysql> USE test;
CREATE TABLE `users` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`age` CHAR(3) NULL DEFAULT NULL,
`status` ENUM('A','P') NOT NULL
DEFAULT 'P',
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
18. Schema design, DDL, DML
MongoDB
PRIMARY> show collections
users
PRIMARY> db.users.insert({"age":
"45", "status": 'A', "name": "John"})
WriteResult({ "nInserted" : 1 })
PRIMARY> db.users.find().pretty()
{ "_id" :
ObjectId("57c5d9ab0b044cb923fffaf9")
,
"age" : "45",
"status" : "A",
"name" : "John"
}
MySQL
mysql> SHOW TABLES;
+-----------------+
| Tables_in_test |
+-----------------+
| users |
+-----------------+
mysql> INSERT INTO `users` (`age`,
`status`, `name`) VALUES ( '45', 'A',
'John');
mysql> SELECT * FROM users;
+----+------+--------+------+
| id | age | status | name |
+----+------+--------+------+
| 1 | 45 | A | John |
+----+------+--------+------+
19. Schema design, DDL, DML
MongoDB
PRIMARY> db.users.insert({"age":
"25", "status": 'R', "name": "Tom"})
WriteResult({ "nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" :
"Document failed validation"
}})
PRIMARY> db.users.find().pretty()
{ "_id" :
ObjectId("57c5dbd67fd59bca3fb314ac")
,
"age" : "45",
"status" : "A",
"name" : "John"
}
MySQL
mysql> INSERT INTO `users` (`age`,
`status`, `name`) VALUES ( '25', 'R',
'Tom');
ERROR 1265 (01000): Data truncated
for column 'status' at row 1
mysql> SELECT * FROM users;
+----+------+--------+------+
| id | age | status | name |
+----+------+--------+------+
| 1 | 45 | A | John |
+----+------+--------+------+
24. Backups and recovery
MongoDB
• mongodump
– Logical backup
– Slow to create and restore
• File system snapshot
– LVM, EBS snapshot
• cp or rsync
– Copy files from freezed node
• Cloud manager
– Monthly subscription
– Point in time backup
• Ops manager (Enterprise)
– Yearly subscription
– Point in time backup
MySQL
• mysqldump
– Logical backup
– Slow to create and worse for
restore
• mydumper
– Logical backup
– Still slow but faster than
mysqldump
• xtrabackup
– Binary backup
– Fast to create and restore
• mysqlbackup
– NOT Open Source
– Binary backup