MYSQL数据 库主 从配置php
环境准备:192.168.227.138为master主服务器,192.168.227.139o slave从服务器html
在主和从服务器都安装mysql相关软件,命令以下:mysql
yum install -y mysql-server mysql mysql-devel sql
安装完毕后,在Master修改vi /etc/my.cnf内容以下:数据库
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id=1
auto_increment_offset=1
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
服务器
replicate-do-db=allsocket
启动mysql便可,/etc/init.d/mysqld restartide
而后修改slave mysql数据库my.cnf 配置文件内容以下:rest
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id=2
auto_increment_offset=2
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
master-host=192.168.227.138
master-user=root
master-pass=123456
master-port=3306
master-connect-retry=60
server
replicate-do-db=all
在slave 数据库服务器上设置权限,执行以下命令:
grant replication slave on *.* to 'root'@'%' identified by '123456';
在Master数据库执行以下命令:
mysql>show master status;
而后在slava 服务器指定master ip 和同步的pos点:
mysql> change master to
-> master_host='192.168.227.138',master_user='root',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=253;
在slave启动slave start 并执行show slave\G 查看mysql主从状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.227.138
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysq-bin.000002
Read_Master_Log_Pos: 106
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 250
Relay_Master_Log_File: mysq-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 106
Relay_Log_Space: 406
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
在master主服务器上建立一个数据库,slave从服务器上会同步。
当主服务断开链接,从服务切换,修改主服务器 vi config_global.php 文件
[root@localhost html]# cd config/[root@localhost config]# ls
config_global_default.php config_ucenter_default.php index.htm
config_global.php config_ucenter.php
[root@localhost config]# vi config_global.php
修改master主服务器/var/www/html/config/config_ucenter.php
在slave从服务器上进行受权:
mysql> grant all on discuz.* to discuz@'192.168.227.138' identified by '123456';