摘要:
每个数据库对于db,schema以及user,role都有一套自己的设计, 不同数据库间对于相同名字的东西例如database和schema可以说南辕北辙, 例如mysql中schema其实是database的同义词.
本文分析monetdb的database的概念和作用
备份: https://stoneatom.yuque.com/staff-ft8n1u/qfqtnb/gfqc62fozh0qsyqm
database的概念和作用:
- 和mysql的database完全不同, mysql的database和schema是同义词, 但是mdb的database完全不同
- mdb的database可以当作实例来理解, 一个db包含mserver运行的一切, 以下逐个解释
- mdb的db在物理文件上, 包含最顶层的目录结构, mserver是以db为单元启动
- 用户每指定一个不同的db做连接, 就启动一个新的mserver进程
- db与db之间, 从物理文件目录结构上就彻底隔离, 除了预定义的对象, 两个db之间没有任何相同的地方
- 无论用户定义的schema,user,role,UDF等等, 两个不同的db, 都是互相不能使用也不能访问
- 换个简单的说法, user是db的一部分, 一个db可以创建多个user, 但是一个user却只能位于一个db
- 这里有个漏洞, 如果两个不同的db里创建两个相同名字的user, 能说这个user是同一个?
- 显然不能, 因为这两个名字一样的user, 无论是password, 还是赋予的role, 还是创建的schema, 以及默认的schema, 都是独立的, 位于不同的db中
- 所以很明显, 就算是在两个不同的db中创建两个名字一样的user, 这两个user也是完全不同的, 仅仅只是user名字一样. 但是这两个user却位于两个不独立的db实例中!
- 那么该如何理解mdb中的database与mysql中的database的区别呢?
- mysql的database是schema的同义词
- 也就是说mysql的database对应的是mdb中的schema
- 而mdb的schema是mdb的database的一部分
- 那么mdb的database的作用就是mysql的整个实例
- 从运行的角度, mdb的一个实例是一个mserver进程, mysql的一个实例是一个mysqld进程.
- 一个mserver进程管理一个mdb的db, 也只能管理一个db. 而这个db包含运行时所有对象, 且与其他的mdb的db互相独立
- 一个mysqld进程管理所有的mysql对象, 即包含运行时的一切对象, 等价于mdb的一个mserver
mclient打开两个mdb的database分析:
节点架构图:
节点图说明:
- mserver对应的可以理解成mysqld
- 客户端每连接一个不同的database,就会启动一个新的mserver进程
- 如果两个客户端连接同一个database,那么就不会启动新的mserer进程
两个客户端分别连接两个不同的database的例子分析:
一. 创建两个database
# mytest
monetdb create mytest
monetdb release mytest
# mycrack
monetdb create mycrack
monetdb release mycrack
二. 分别查看两个database下的目录结构
[root@dev-stonedb-zhangshilong1 mytest]# pwd
/dbfarm/mytest
[root@dev-stonedb-zhangshilong1 mytest]# ll
total 4
-rw------- 1 root root 0 Sep 28 02:46 6a292eb5-8a55-4861-af04-1e48573199f2
drwx------ 9 root root 4096 Sep 28 02:46 bat
-rw------- 1 root root 0 Sep 22 06:27 mdbtrace.log
drwx------ 3 root root 17 Sep 22 06:27 sql_logs
[root@dev-stonedb-zhangshilong1 mycrack]# pwd
/dbfarm/mycrack
[root@dev-stonedb-zhangshilong1 mycrack]# ll
total 0
-rw------- 1 root root 0 Sep 28 02:45 b2bc7dd0-a500-496e-9fee-d58cacbd7ddd
drwx------ 4 root root 30 Sep 28 02:45 bat
-rw------- 1 root root 0 Sep 28 02:45 mdbtrace.log
drwx------ 3 root root 17 Sep 28 02:45 sql_logs
分析:
- 可以看出都是作为顶层目录结构
- 两个database互相独立
三. 用两个客户端分别连接mytest库和mycrack库
[root@dev-stonedb-zhangshilong1 ~]# mclient -d mytest -u crack
password:
Welcome to mclient, the MonetDB/SQL interactive terminal (unreleased)
Database: MonetDB v11.45.17, 'mapi:monetdb://dev-stonedb-zhangshilong1:50000/mytest'
FOLLOW US on https://twitter.com/MonetDB or https://github.com/MonetDB/MonetDB
Type \q to quit, \? for a list of available commands
auto commit mode: on
sql>
[root@dev-stonedb-zhangshilong1 ~]# mclient -d mycrack
password:
Welcome to mclient, the MonetDB/SQL interactive terminal (unreleased)
Database: MonetDB v11.45.17, 'mapi:monetdb://dev-stonedb-zhangshilong1:50000/mycrack'
FOLLOW US on https://twitter.com/MonetDB or https://github.com/MonetDB/MonetDB
Type \q to quit, \? for a list of available commands
auto commit mode: on
sql>
四. 查看打开两个不同的database时mserver与database的情况:
[root@dev-stonedb-zhangshilong1 ~]# ps -ef | grep mserver | grep -v grep
root 1372563 1372339 0 02:45 ? 00:00:11 /usr/local/bin/mserver5 --dbpath=/dbfarm/mycrack --set=merovingian_uri mapi:monetdb://dev-stonedb-zhangshilong1:50000/mycrack --set=mapi_listenaddr none --set=mapi_usock /dbfarm/mycrack/.mapi.sock --set=monet_vault_key /dbfarm/mycrack/.vaultkey --set=gdk_nr_threads 64 --set=max_clients 64 --set=sql_optimizer default_pipe
root 1373285 1372339 0 02:46 ? 00:00:10 /usr/local/bin/mserver5 --dbpath=/dbfarm/mytest --set=merovingian_uri mapi:monetdb://dev-stonedb-zhangshilong1:50000/mytest --set=mapi_listenaddr none --set=mapi_usock /dbfarm/mytest/.mapi.sock --set=monet_vault_key /dbfarm/mytest/.vaultkey --set=gdk_nr_threads 64 --set=max_clients 64 --set=sql_optimizer default_pipe
分析:
- 可以看出连接了两个database, 就启动了两个mserver
- 每个mserver管理一个不同的database
五. 对比用两个客户端连接同一个database, 查看mserver的情况
[root@dev-stonedb-zhangshilong1 ~]# mclient -d mytest -u crack
password:
Welcome to mclient, the MonetDB/SQL interactive terminal (unreleased)
Database: MonetDB v11.45.17, 'mapi:monetdb://dev-stonedb-zhangshilong1:50000/mytest'
FOLLOW US on https://twitter.com/MonetDB or https://github.com/MonetDB/MonetDB
Type \q to quit, \? for a list of available commands
auto commit mode: on
sql>
[root@dev-stonedb-zhangshilong1 ~]# mclient -d mytest
password:
Welcome to mclient, the MonetDB/SQL interactive terminal (unreleased)
Database: MonetDB v11.45.17, 'mapi:monetdb://dev-stonedb-zhangshilong1:50000/mytest'
FOLLOW US on https://twitter.com/MonetDB or https://github.com/MonetDB/MonetDB
Type \q to quit, \? for a list of available commands
auto commit mode: on
sql>
[root@dev-stonedb-zhangshilong1 ~]# monetdbd stop /dbfarm
[root@dev-stonedb-zhangshilong1 ~]#
[root@dev-stonedb-zhangshilong1 ~]# monetdbd start /dbfarm
[root@dev-stonedb-zhangshilong1 ~]#
[root@dev-stonedb-zhangshilong1 ~]# ps -ef | grep mserver | grep -v grep
[root@dev-stonedb-zhangshilong1 ~]#
[root@dev-stonedb-zhangshilong1 ~]# ps -ef | grep mserver | grep -v grep
root 1506323 1506096 0 05:47 ? 00:00:00 /usr/local/bin/mserver5 --dbpath=/dbfarm/mytest --set=merovingian_uri mapi:monetdb://dev-stonedb-zhangshilong1:50000/mytest --set=mapi_listenaddr none --set=mapi_usock /dbfarm/mytest/.mapi.sock --set=monet_vault_key /dbfarm/mytest/.vaultkey --set=gdk_nr_threads 64 --set=max_clients 64 --set=sql_optimizer default_pipe
分析:
- 可以看出用两个不同的客户端, 打开同一个database, 只有一个mserver来管理这个database
- 所以是一个mserver管理一个不同的database
六.两个客户端连接两个不同的database, 查看资源隔离情况
客户端一连接mycrack:
[root@dev-stonedb-zhangshilong1 ~]# mclient -d mycrack
password:
Welcome to mclient, the MonetDB/SQL interactive terminal (unreleased)
Database: MonetDB v11.45.17, 'mapi:monetdb://dev-stonedb-zhangshilong1:50000/mycrack'
FOLLOW US on https://twitter.com/MonetDB or https://github.com/MonetDB/MonetDB
Type \q to quit, \? for a list of available commands
auto commit mode: on
sql>
sql>select * from sys.schemas;
+------+----------+---------------+-------+--------+
| id | name | authorization | owner | system |
+======+==========+===============+=======+========+
| 2000 | sys | 2 | 3 | true |
| 2114 | tmp | 2 | 3 | true |
| 6547 | json | 3 | 3 | true |
| 6643 | profiler | 3 | 3 | true |
| 6832 | wlc | 3 | 3 | true |
| 6851 | wlr | 3 | 3 | true |
| 7302 | logging | 3 | 3 | true |
+------+----------+---------------+-------+--------+
7 tuples
sql>select * from sys.users;
+-----------+---------------+----------------+-------------+------------+-------------+--------------+--------------+
| name | fullname | default_schema | schema_path | max_memory | max_workers | optimizer | default_role |
+===========+===============+================+=============+============+=============+==============+==============+
| monetdb | MonetDB Admin | 2000 | "sys" | 0 | 0 | default_pipe | 3 |
| .snapshot | Snapshot User | 2000 | "sys" | 0 | 0 | default_pipe | 6831 |
+-----------+---------------+----------------+-------------+------------+-------------+--------------+--------------+
2 tuples
sql>select * from sys.roles;
+------+----------+---------+
| id | name | grantor |
+======+==========+=========+
| 1 | public | 0 |
| 2 | sysadmin | 0 |
+------+----------+---------+
2 tuples
客户端二连接mytest:
[root@dev-stonedb-zhangshilong1 ~]# mclient -d mytest -u crack
password:
Welcome to mclient, the MonetDB/SQL interactive terminal (unreleased)
Database: MonetDB v11.45.17, 'mapi:monetdb://dev-stonedb-zhangshilong1:50000/mytest'
FOLLOW US on https://twitter.com/MonetDB or https://github.com/MonetDB/MonetDB
Type \q to quit, \? for a list of available commands
auto commit mode: on
sql>
sql>select * from sys.schemas;
+------+-------------+---------------+-------+--------+
| id | name | authorization | owner | system |
+======+=============+===============+=======+========+
| 2000 | sys | 2 | 3 | true |
| 2114 | tmp | 2 | 3 | true |
| 6547 | json | 3 | 3 | true |
| 6643 | profiler | 3 | 3 | true |
| 6832 | wlc | 3 | 3 | true |
| 6851 | wlr | 3 | 3 | true |
| 7302 | logging | 3 | 3 | true |
| 7464 | mysch | 3 | 3 | false |
| 7465 | hack | 7466 | 3 | false |
| 7467 | crack | 7468 | 3 | false |
| 7470 | copy_schema | 7469 | 3 | false |
| 7491 | sch_t2 | 3 | 3 | false |
+------+-------------+---------------+-------+--------+
12 tuples
sql>select * from sys.users;
+-----------+---------------+----------------+-------------+------------+-------------+--------------+--------------+
| name | fullname | default_schema | schema_path | max_memory | max_workers | optimizer | default_role |
+===========+===============+================+=============+============+=============+==============+==============+
| monetdb | MonetDB Admin | 2000 | "sys" | 0 | 0 | default_pipe | 3 |
| .snapshot | Snapshot User | 2000 | "sys" | 0 | 0 | default_pipe | 6831 |
| hack | hacker | 7464 | "sys,mysch" | 0 | 0 | default_pipe | 7466 |
| crack | cracker | 7467 | "sys" | 0 | 0 | default_pipe | 7468 |
| user1 | copy user | 7470 | "sys" | 0 | 0 | default_pipe | 7471 |
| user2 | copy user | 7470 | "sys" | 0 | 0 | default_pipe | 7472 |
+-----------+---------------+----------------+-------------+------------+-------------+--------------+--------------+
6 tuples
sql>select * from sys.roles;
+------+-----------+---------+
| id | name | grantor |
+======+===========+=========+
| 1 | public | 0 |
| 2 | sysadmin | 0 |
| 7469 | copy_role | 3 |
+------+-----------+---------+
3 tuples
分析:
- 可以看出除了预定义的对象外, 两个客户端连接两个database的数据完全隔离
- 所以mdb中的mserver和database, 其实就是一个独立的实例, 等同于一个mysqld