SlideShare a Scribd company logo
Working with MongoDB as
MySQL DBA
Date: Oct-5-2016
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
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
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
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
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
MySQL replication
MySQL Master
MySQL Process
Binary LogsMySQL Data
MySQL Slave
MySQL Process
SQL Thread
Relay logs
IO Thread MySQL Data
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
MongoDB Replication
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
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
Variables and configuration file
MongoDB
/etc/mongod.conf
systemLog:
destination: file
path: /var/log/mongodb/mongod.log
logAppend: true
storage:
dbPath: /mongodb/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
net:
bindIp: 127.0.0.1
port: 27017
operationProfiling:
slowOpThresholdMs: 100
mode: off
MySQL
/etc/my.cnf
[client]
port = 3306
socket = /var/run/mysqld/mysql.sock
[mysqld]
port = 3306
user = mysql
socket = /var/run/mysqld/mysql.sock
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
log_error = hostname_error.log
slow_query_log_file = hostname_slow.log
innodb_buffer_pool_size = 128M
innodb_file_per_table = 1
[mysqldump]
max_allowed_packet = 64M
Server status
MongoDB
PRIMARY> db.serverStatus()
{ "host" : "mongodb1",
"version" : "3.0.11",
"process" : "mongod",
"pid" : NumberLong(15805),
"uptime" : 9522445,
…...
"uptimeEstimate" : 9409413,
"localTime" :
ISODate("2016-09-29T14:39:43.769Z",
…...
"connections" : {
"current" : 802,
"available" : 50398,
"totalCreated" :
NumberLong(644684)
}
MySQL
mysql> SHOW GLOBAL STATUS;
+--------------------------+------------+
| Variable_name | Value |
+--------------------------+------------+
| Aborted_clients | 0 |
| Aborted_connects | 0 |
| Connections | 30023 |
| Created_tmp_disk_tables | 0 |
| Created_tmp_tables | 8340 |
| Created_tmp_files | 60 |
...
| Threads_connected | 1 |
| Threads_running | 1 |
| Uptime | 80380 |
+--------------------------+------------+
Processlist
MongoDB
PRIMARY> db.currentOp()
{ "inprog" : [ {
"desc" : "conn531228",
"threadId" : "0xe856680",
"connectionId" : 531228,
"opid" : 1798444507,
"active" : true,
"secs_running" : 0,
"microsecs_running" : NumberLong(995),
"op" : "query",
"ns" : "hbrg.user.cart",
"query" : { "$query" : { "user" :
"C2ABBA1F-3555-3649-85D8-7A544F15EA94" }},
"planSummary" : "IXSCAN { user: 1 }",
......
} ] }
MySQL
mysql> SHOW FULL PROCESSLISTG
************* 1. row ***************
Id: 3123
User: igor
Host: localhost
db: test
Command: Query
Time: 0
State: NULL
Info: SHOW FULL PROCESSLIST
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';
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
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;
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 |
+----+------+--------+------+
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 |
+----+------+--------+------+
Schema design, DDL, DML
MongoDB
PRIMARY> db.users.update({ "_id" :
ObjectId("57c5dbd67fd59bca3fb314ac")}
, {$set : {"status":'P'} } )
WriteResult({ "nMatched" : 1,
"nUpserted" : 0, "nModified" : 1 })
PRIMARY> db.users.remove( {} )
PRIMARY> ALTER collection users … ?
PRIMARY> db.users.update({ "_id" :
ObjectId("57c5dbd67fd59bca3fb314ac")}
, {$set : {"position":"DBA"} } )
MySQL
mysql> UPDATE users SET status='P'
WHERE id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1
Warnings: 0
mysql> truncate users; delete from
users;
mysql> ALTER TABLE `users` ADD
COLUMN `position` VARCHAR(50) NULL
AFTER `name`;
mysql> UPDATE `users` SET
`position`='DBA' WHERE `id`=1;
Schema design, DDL, DML
MongoDB
PRIMARY> db.users.createIndex({"name":1})
PRIMARY>
db.users.find({"name":"John"}).explain()
{ ........
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : { "name" : 1 },
"indexName" : "name_1",
........
"indexBounds" : {"name" : [
"["John", "John"]"
] }
}
PRIMARY> db.users.drop( )
MySQL
mysql> ALTER TABLE `users` ADD INDEX
`name_idx` (`name`);
mysql> explain select * from users where
name = 'John'G
*********** 1. row **************
id: 1
select_type: SIMPLE
table: users
type: ref
possible_keys: name_idx
key: name_idx
……..
rows: 1
Extra: Using index condition
1 row in set (0.00 sec)
mysql> DROP TABLE users;
Storage engines
MongoDB
MMAPv1
WiredTiger
RocksDB
In-memory
PerconaFT
MySQL
MyISAM
InnoDB
MyRocks
Memory
CSV
Archive
Federated
Blackhole
Log files
MongoDB
Error log
Slow query log
/var/log/mongodb/mongod.log
MySQL
Error log
Slow query log
/var/log/mysql/error.log
/var/log/mysql/slow_query.log
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
Questions?
We’re Hiring!
https://pythian.com

More Related Content

PDF
Maintenance for MongoDB Replica Sets
PDF
How to scale MongoDB
PDF
Exploring the replication and sharding in MongoDB
PDF
Exploring the replication in MongoDB
PDF
MongoDB HA - what can go wrong
PDF
Enhancing the default MongoDB Security
PPT
NoSQL Analytics: JSON Data Analysis and Acceleration in MongoDB World
PPTX
2014 05-07-fr - add dev series - session 6 - deploying your application-2
Maintenance for MongoDB Replica Sets
How to scale MongoDB
Exploring the replication and sharding in MongoDB
Exploring the replication in MongoDB
MongoDB HA - what can go wrong
Enhancing the default MongoDB Security
NoSQL Analytics: JSON Data Analysis and Acceleration in MongoDB World
2014 05-07-fr - add dev series - session 6 - deploying your application-2

What's hot (20)

PPTX
Introduction to Sharding
PPTX
MongoDB - External Authentication
PDF
Sharding
PPT
Migrating to MongoDB: Best Practices
PPTX
Back to Basics 2017: Introduction to Sharding
PPTX
Back to Basics Webinar 6: Production Deployment
PPTX
Advanced Sharding Features in MongoDB 2.4
PDF
Webinar: Schema Patterns and Your Storage Engine
PDF
Evolution of MonogDB Sharding and Its Best Practices - Ranjith A - Mydbops Team
PDF
MongodB Internals
PDF
MongoDB Sharding Fundamentals
PPTX
MongoDB Auto-Sharding at Mongo Seattle
PDF
Webinar: Was ist neu in MongoDB 2.4
PPTX
High Performance Applications with MongoDB
KEY
Mongodb sharding
PPTX
Sharding
PDF
Challenges with MongoDB
PPTX
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
PPTX
Choosing a Shard key
PPTX
MongoDB in the Middle of a Hybrid Cloud and Polyglot Persistence Architecture
Introduction to Sharding
MongoDB - External Authentication
Sharding
Migrating to MongoDB: Best Practices
Back to Basics 2017: Introduction to Sharding
Back to Basics Webinar 6: Production Deployment
Advanced Sharding Features in MongoDB 2.4
Webinar: Schema Patterns and Your Storage Engine
Evolution of MonogDB Sharding and Its Best Practices - Ranjith A - Mydbops Team
MongodB Internals
MongoDB Sharding Fundamentals
MongoDB Auto-Sharding at Mongo Seattle
Webinar: Was ist neu in MongoDB 2.4
High Performance Applications with MongoDB
Mongodb sharding
Sharding
Challenges with MongoDB
MongoDB: Comparing WiredTiger In-Memory Engine to Redis
Choosing a Shard key
MongoDB in the Middle of a Hybrid Cloud and Polyglot Persistence Architecture
Ad

Similar to Working with MongoDB as MySQL DBA (20)

PPT
MongoDB Tick Data Presentation
PPTX
Scaling MongoDB
PPTX
PDF
MongoDB.pdf
PPTX
MySQL 5.7 New Features for Developers
KEY
Tek tutorial
PPTX
Vitalii Bondarenko - Масштабована бізнес-аналітика у Cloud Big Data Cluster. ...
KEY
DPC Tutorial
KEY
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
PPTX
MongoDB at Scale
PDF
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
PDF
MongoDB Administration 101
PPTX
MongoDB Schema Design: Practical Applications and Implications
PPTX
Dev Jumpstart: Build Your First App with MongoDB
PDF
What is MariaDB Server 10.3?
PDF
Hadoop & no sql new generation database systems
PPTX
MongoDC 2012: "Operationalizing" MongoDB@AOL
PPTX
Operationalizing MongoDB at AOL
PDF
PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...
MongoDB Tick Data Presentation
Scaling MongoDB
MongoDB.pdf
MySQL 5.7 New Features for Developers
Tek tutorial
Vitalii Bondarenko - Масштабована бізнес-аналітика у Cloud Big Data Cluster. ...
DPC Tutorial
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
MongoDB at Scale
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
MongoDB Administration 101
MongoDB Schema Design: Practical Applications and Implications
Dev Jumpstart: Build Your First App with MongoDB
What is MariaDB Server 10.3?
Hadoop & no sql new generation database systems
MongoDC 2012: "Operationalizing" MongoDB@AOL
Operationalizing MongoDB at AOL
PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...
Ad

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Chapter 2 Digital Image Fundamentals.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced IT Governance
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
Chapter 2 Digital Image Fundamentals.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Sensors and Actuators in IoT Systems using pdf
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
20250228 LYD VKU AI Blended-Learning.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Review of recent advances in non-invasive hemoglobin estimation
Understanding_Digital_Forensics_Presentation.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Empathic Computing: Creating Shared Understanding
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced IT Governance
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm

Working with MongoDB as MySQL DBA

  • 1. Working with MongoDB as MySQL DBA Date: Oct-5-2016
  • 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
  • 7. MySQL replication MySQL Master MySQL Process Binary LogsMySQL Data MySQL Slave MySQL Process SQL Thread Relay logs IO Thread MySQL Data
  • 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
  • 12. Variables and configuration file MongoDB /etc/mongod.conf systemLog: destination: file path: /var/log/mongodb/mongod.log logAppend: true storage: dbPath: /mongodb/data journal: enabled: true wiredTiger: engineConfig: cacheSizeGB: 2 net: bindIp: 127.0.0.1 port: 27017 operationProfiling: slowOpThresholdMs: 100 mode: off MySQL /etc/my.cnf [client] port = 3306 socket = /var/run/mysqld/mysql.sock [mysqld] port = 3306 user = mysql socket = /var/run/mysqld/mysql.sock basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp log_error = hostname_error.log slow_query_log_file = hostname_slow.log innodb_buffer_pool_size = 128M innodb_file_per_table = 1 [mysqldump] max_allowed_packet = 64M
  • 13. Server status MongoDB PRIMARY> db.serverStatus() { "host" : "mongodb1", "version" : "3.0.11", "process" : "mongod", "pid" : NumberLong(15805), "uptime" : 9522445, …... "uptimeEstimate" : 9409413, "localTime" : ISODate("2016-09-29T14:39:43.769Z", …... "connections" : { "current" : 802, "available" : 50398, "totalCreated" : NumberLong(644684) } MySQL mysql> SHOW GLOBAL STATUS; +--------------------------+------------+ | Variable_name | Value | +--------------------------+------------+ | Aborted_clients | 0 | | Aborted_connects | 0 | | Connections | 30023 | | Created_tmp_disk_tables | 0 | | Created_tmp_tables | 8340 | | Created_tmp_files | 60 | ... | Threads_connected | 1 | | Threads_running | 1 | | Uptime | 80380 | +--------------------------+------------+
  • 14. Processlist MongoDB PRIMARY> db.currentOp() { "inprog" : [ { "desc" : "conn531228", "threadId" : "0xe856680", "connectionId" : 531228, "opid" : 1798444507, "active" : true, "secs_running" : 0, "microsecs_running" : NumberLong(995), "op" : "query", "ns" : "hbrg.user.cart", "query" : { "$query" : { "user" : "C2ABBA1F-3555-3649-85D8-7A544F15EA94" }}, "planSummary" : "IXSCAN { user: 1 }", ...... } ] } MySQL mysql> SHOW FULL PROCESSLISTG ************* 1. row *************** Id: 3123 User: igor Host: localhost db: test Command: Query Time: 0 State: NULL Info: SHOW FULL PROCESSLIST
  • 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 | +----+------+--------+------+
  • 20. Schema design, DDL, DML MongoDB PRIMARY> db.users.update({ "_id" : ObjectId("57c5dbd67fd59bca3fb314ac")} , {$set : {"status":'P'} } ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) PRIMARY> db.users.remove( {} ) PRIMARY> ALTER collection users … ? PRIMARY> db.users.update({ "_id" : ObjectId("57c5dbd67fd59bca3fb314ac")} , {$set : {"position":"DBA"} } ) MySQL mysql> UPDATE users SET status='P' WHERE id=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> truncate users; delete from users; mysql> ALTER TABLE `users` ADD COLUMN `position` VARCHAR(50) NULL AFTER `name`; mysql> UPDATE `users` SET `position`='DBA' WHERE `id`=1;
  • 21. Schema design, DDL, DML MongoDB PRIMARY> db.users.createIndex({"name":1}) PRIMARY> db.users.find({"name":"John"}).explain() { ........ "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "name" : 1 }, "indexName" : "name_1", ........ "indexBounds" : {"name" : [ "["John", "John"]" ] } } PRIMARY> db.users.drop( ) MySQL mysql> ALTER TABLE `users` ADD INDEX `name_idx` (`name`); mysql> explain select * from users where name = 'John'G *********** 1. row ************** id: 1 select_type: SIMPLE table: users type: ref possible_keys: name_idx key: name_idx …….. rows: 1 Extra: Using index condition 1 row in set (0.00 sec) mysql> DROP TABLE users;
  • 23. Log files MongoDB Error log Slow query log /var/log/mongodb/mongod.log MySQL Error log Slow query log /var/log/mysql/error.log /var/log/mysql/slow_query.log
  • 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