1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
$configs
[
'db_info'
][
'blog'
][0] =
array
(
'db_host'
=>
'192.168.0.1'
,
'db_name'
=>
'blog'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
$configs
[
'db_info'
][
'blog'
][1] =
array
(
'db_host'
=>
'192.168.0.2'
,
'db_name'
=>
'blog'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
$configs
[
'db_info'
][
'blog'
][2] =
array
(
'db_host'
=>
'192.168.0.2'
,
'db_name'
=>
'blog'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
//...还有很多
|
至于选择哪一台服务器,只需要根据uid做一个简单的匹配就可以了。
再谈到的就是主从数据库了。什么情况下使用主从数据库?比如某个名人博客,访问量相当的大,已经没有办法把他的数据再进行拆分了,这个时候就得考虑主从数据库服务器了,使用多台数据库来分流。这样要适用主从和分库,可能上面配置信息得稍微改动一下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
$configs
[
'db_info'
][
'blog'
][0][
'master'
] =
array
(
'db_host'
=>
'192.168.0.1'
,
'db_name'
=>
'blog'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
$configs
[
'db_info'
][
'blog'
][0][
'slave'
][0] =
array
(
'db_host'
=>
'192.168.0.2'
,
'db_name'
=>
'blog'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
$configs
[
'db_info'
][
'blog'
][0][
'slave'
][1] =
array
(
'db_host'
=>
'192.168.0.3'
,
'db_name'
=>
'blog'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
$configs
[
'db_info'
][
'blog'
][1][
'master'
] =
array
(
'db_host'
=>
'192.168.0.4'
,
'db_name'
=>
'blog'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
$configs
[
'db_info'
][
'blog'
][1][
'slave'
][0] =
array
(
'db_host'
=>
'192.168.0.5'
,
'db_name'
=>
'blog'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
$configs
[
'db_info'
][
'blog'
][1][
'slave'
][1] =
array
(
'db_host'
=>
'192.168.0.6'
,
'db_name'
=>
'blog'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
$configs
[
'db_info'
][
'session'
][0][
'master'
] =
array
(
'db_host'
=>
'192.168.0.7'
,
'db_name'
=>
'session'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
$configs
[
'db_info'
][
'session'
][1][
'master'
] =
array
(
'db_host'
=>
'192.168.0.8'
,
'db_name'
=>
'session'
,
'db_user'
=>
'root'
,
'db_pass'
=>
''
,
);
|
写到这里,我想都应该知道如何分表和分配你的数据库了吧,接下去我就来说一下如何轻松的读取这样的配置信息,如何将这些配置融入你的数据库驱动中。首先以单例摸式创建DB类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
<?php
if
(!defined(
"DB_FETCH_ASSOC"
)) {
define(
"DB_FETCH_ASSOC"
, 1);
}
if
(!defined(
"DB_FETCH_ROW"
)) {
define(
"DB_FETCH_ROW"
, 2);
}
if
(!defined(
"DB_FETCH_ARRAY"
)) {
define(
"DB_FETCH_ARRAY"
, 3);
}
if
(!defined(
"DB_FETCH_DEFAULT"
)) {
define(
"DB_FETCH_DEFAULT"
, DB_FETCH_ASSOC);
}
class
DB {
function
DB(
$dsn
,
$db_key
,
$p_conn
,
$fetch_mode
) {
$this
->dsn =
$dsn
;
$this
->db_key =
$db_key
;
$this
->sql =
''
;
$this
->sqls =
array
();
$this
->u_sqls =
array
();
$this
->q_sqls =
array
();
$this
->u_conn = null;
$this
->q_conn = null;
$this
->p_conn =
$p_conn
;
$this
->fecth_mode =
$fetch_mode
;
$this
->query_num = 0;
$this
->update_num = 0;
}
function
&init(&
$dsn
,
$db_key
,
$p_conn
= false,
$fetch_mode
= DB_FETCH_DEFAULT) {
static
$db
;
$db_key
=
explode
(
'.'
,
$db_key
);
$db_key
=
"['"
. implode(
"']['"
,
$db_key
) .
"']"
;
eval
(
'$flag = isset($db'
.
$db_key
.
');'
);
eval
(
"$db_info = $dsn['db_info']"
.
$db_key
.
";"
);
if
(!
$flag
) {
$obj
=
new
DB(
$db_info
,
$db_key
,
$p_conn
,
$fetch_mode
);
eval
(
'$db'
.
$db_key
.
' = $obj;'
);
unset(
$obj
);
}
return
$db
;
}
}
$db
= &DB::init(
$configs
,
'blog.0'
);
print_r(
$db
);
?>
|
从上面打印结果可以看出,blog数据库集群的第一组数据库服务器被载入到$this->dsn中了。这个下面就是简单的数据COPY的主从服务器,所以可以使用随机数来指定到某一台服务器。以下是一个简单的随机数实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
class
DB {
//....
function
connectDB(
$type
=
"master"
) {
if
(
$type
==
"master"
) {
$db_host
=
$this
->dsn[
"master"
][
"db_host"
];
$db_name
=
$this
->dsn[
"master"
][
"db_name"
];
$db_user
=
$this
->dsn[
"master"
][
"db_user"
];
$db_pass
=
$this
->dsn[
"master"
][
"db_pass"
];
$this
->u_conn = mysqli_connect(
$db_host
,
$db_user
,
$db_pass
);
$this
->selectDB(
$db_name
,
$this
->conn);
if
(!
$this
->u_conn) {
$message
=
"Update DataBase Connect False : ($db_host, $db_user, ******) !"
;
$this
->error(
$message
, 0);
}
}
else
{
if
(
empty
(
$_COOKIE
[
$_configs
[
'cookie_prefix'
] .
'db_no'
])) {
$db_no
=
array_rand
(
$this
->dsn[
"db_info"
][
"slave"
]);
}
else
{
$db_no
=
$_COOKIE
[
$_configs
[
'cookie_prefix'
] .
'db_no'
];
}
$db_info
=
$this
->dsn[
"slave"
][
$db_no
];
$db_host
=
$db_info
[
"db_host"
];
$db_name
=
$db_info
[
"db_name"
];
$db_user
=
$db_info
[
"db_user"
];
$db_pass
=
$db_info
[
"db_pass"
];
$this
->q_conn = mysqli_connect(
$db_host
,
$db_user
,
$db_pass
);
if
(!
$this
->q_conn) {
if
(!
$this
->u_conn) {
$this
->connectDB();
}
$this
->q_conn =
$this
->u_conn;
if
(!
$this
->q_conn) {
$message
=
"Query DataBase Connect False : ($db_host, $db_user, ******) !"
;
$this
->error(
$message
, 0);
}
}
else
{
$this
->selectDB(
$db_name
,
$this
->q_conn);
}
}
}
function
selectDB(
$db_name
,
$conn
) {
if
(
$db_name
!= null) {
if
(! mysqli_select_db(
$conn
,
$db_name
)) {
$code
= mysqli_errno(
$conn
);
$message
= mysqli_error(
$conn
);
$this
->error(
$message
,
$code
);
}
return
true;
}
return
false;
}
function
query(
$sql
,
$limit
= 1,
$quick
= false) {
if
(
$limit
!= null) {
$sql
=
$sql
.
" LIMIT "
.
$limit
;
}
$this
->sqls[] =
$sql
;
$this
->q_sqls[] =
$sql
;
$this
->sql =
$sql
;
if
(
empty
(
$this
->q_conn)) {
$this
->connectDB(
"slave"
);
}
$this
->qrs = mysqli_query(
$this
->q_conn,
$sql
,
$quick
? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT);
if
(!
$this
->qrs) {
$code
= mysqli_errno(
$this
->q_conn);
$message
= mysqli_error(
$this
->q_conn);
$this
->error(
$message
,
$code
);
}
$this
->query_num++;
return
$this
->qrs;
}
function
update(
$sql
) {
$this
->sql =
$sql
;
$this
->sqls[] =
$this
->sql;
$this
->u_sqls[] =
$this
->sql;
if
(
$this
->u_conn == null) {
$this
->connectDB(
"master"
);
}
$this
->urs = mysqli_query(
$this
->u_conn,
$sql
, MYSQLI_USE_RESULT);
$this
->update_num++;
if
(!
$this
->urs) {
return
false;
}
else
{
return
true;
}
}
}
|
至此,基本框架已经出来了,来看看调用方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
// 连接到第一组会话服务器
$db
= &DB::init(
$configs
,
'session.0'
);
// 执行一次查询
$db
[
'session'
][0]->query(
'SELECT ...'
);
// 再次连接BLOG服务器
$db
= &DB::init(
$configs
,
'blog.1'
);
// 执行一次更新
$db
[
'blog'
][1]->update(
'UPDATE ...'
);
// 再次调用会话更新
$db
[
'session'
][0]->update(
'INSERT ...'
);
?>
|