实验环境:12台虚拟机
cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=f297f992-f824-4489-890c-e237ea9c4a9f
DEVICE=ens33
ONBOOT=no
IPADDR=192.168.88.100
PREFIX=24
GATEWAY=192.168.88.254
DNS1=172.16.23.211
用Linux虚拟机模拟
[路由器]$ ip a
#下面是输出
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:e1:fc:ac brd ff:ff:ff:ff:ff:ff
inet 192.168.88.254/24 brd 192.168.88.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::6a7a:d4a:a7ed:75b5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:e1:fc:b6 brd ff:ff:ff:ff:ff:ff
inet 172.16.23.254/16 brd 172.16.255.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet6 fe80::9a8a:1c56:7288:a4ca/64 scope link noprefixroute
valid_lft forever preferred_lft forever
echo "net.ipv4.ip_forward=1" > /etc/sysctl.conf
查看
[路由器]$ sysctl -p
net.ipv4.ip_forward = 1
[路由器]$ iptables -t nat -A POSTROUTING -s 192.168.88.0/24 ! -d 192.168.88.0/24 -j SNAT --to-source 172.16.23.254
IP: 172.16.23.211
yum install -y pdns pdns-backend-mysql
yum -y install httpd php php-mysql php-mbstring mariadb-server
systemctl start httpd
systemctl start mariadb
#查找到包含launch= 的行,修改并添加下面的内容
[powerDNS]$ vim /etc/pdns/pdns.conf
#大概在250行
launch=gmysql
gmysql-host=localhost
gmysql-port=3306
gmysql-dbname=powerdns
gmysql-user=powerdns
gmysql-password=123
[powerDNS]$ mysql
MariaDB [(none)]> create database powerdns;
MariaDB [(none)]> grant all privileges on powerdns.* to powerdns@localhost identified by "123";
USE powerdns;
CREATE TABLE domains (
id INT AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
master VARCHAR(128) DEFAULT NULL,
last_check INT DEFAULT NULL,
type VARCHAR(6) NOT NULL,
notified_serial INT DEFAULT NULL,
account VARCHAR(40) DEFAULT NULL,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE UNIQUE INDEX name_index ON domains(name);
CREATE TABLE records (
id BIGINT AUTO_INCREMENT,
domain_id INT DEFAULT NULL,
name VARCHAR(255) DEFAULT NULL,
type VARCHAR(10) DEFAULT NULL,
content VARCHAR(64000) DEFAULT NULL,
ttl INT DEFAULT NULL,
prio INT DEFAULT NULL,
change_date INT DEFAULT NULL,
disabled TINYINT(1) DEFAULT 0,
ordername VARCHAR(255) BINARY DEFAULT NULL,
auth TINYINT(1) DEFAULT 1,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE INDEX nametype_index ON records(name,type);
CREATE INDEX domain_id ON records(domain_id);
CREATE INDEX recordorder ON records (domain_id, ordername);
CREATE TABLE supermasters (
ip VARCHAR(64) NOT NULL,
nameserver VARCHAR(255) NOT NULL,
account VARCHAR(40) NOT NULL,
PRIMARY KEY (ip, nameserver)
) Engine=InnoDB;
CREATE TABLE comments (
id INT AUTO_INCREMENT,
domain_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
type VARCHAR(10) NOT NULL,
modified_at INT NOT NULL,
account VARCHAR(40) NOT NULL,
comment VARCHAR(64000) NOT NULL,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE INDEX comments_domain_id_idx ON comments (domain_id);
CREATE INDEX comments_name_type_idx ON comments (name, type);
CREATE INDEX comments_order_idx ON comments (domain_id, modified_at);
CREATE TABLE domainmetadata (
id INT AUTO_INCREMENT,
domain_id INT NOT NULL,
kind VARCHAR(32),
content TEXT,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE INDEX domainmetadata_idx ON domainmetadata (domain_id, kind);
CREATE TABLE cryptokeys (
id INT AUTO_INCREMENT,
domain_id INT NOT NULL,
flags INT NOT NULL,
active BOOL,
content TEXT,
PRIMARY KEY(id)
) Engine=InnoDB;
CREATE INDEX domainidindex ON cryptokeys(domain_id);
CREATE TABLE tsigkeys (
id INT AUTO_INCREMENT,
name VARCHAR(255),
algorithm VARCHAR(50),
secret VARCHAR(255),
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE UNIQUE INDEX namealgoindex ON tsigkeys(name, algorithm);
systemctl start pdns
systemctl enable pdns
yum -y install php-devel php-gd php-mcrypt php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mcrypt php-mhash gettext
systemctl restart httpd
cd /var/www/html
wget http://downloads.sourceforge.net/project/poweradmin/poweradmin-2.1.7.tgz
tar xvf poweradmin-2.1.7.tgz
mv poweradmin-2.1.7 poweradmin
设置下权限
setfacl -Rm u:apache:rwx poweradmin
提供先前配置的数据库详情,同时为Poweradmin设置管理员密码
标题 | 说明 |
---|---|
Username | PowerAdmin用户名 |
Password | 上述用户的密码 |
Hostmaster | 创建SOA记录指定默认主机管理员 |
Primary nameserver | 主域名服务器 |
Secondary namesever | 辅域名服务器 |
示例(根据你自己的):
[powerDNS]$ mysql
MariaDB [(none)]> GRANT SELECT, INSERT, UPDATE, DELETE ON powerdns.* TO 'poweradmin'@'localhost' IDENTIFIED BY '123';
[powerDNS]$ mv poweradmin/inc/config-me.inc.php poweradmin/inc/config.inc.php
[powerDNS]$ vim /var/www/html/poweradmin/inc/config.inc.php
18 $db_host = 'localhost';
19 $db_port = '3306';
20 $db_user = 'powerdns';
21 $db_pass = '123';
22 $db_name = 'powerdns';
23 $db_type = 'mysql';
...
26 $db_layer = 'PDO'; # or MDB2
...
30 $session_key = 'gKB$t5Qx%l!%$d+M~T$Hv+lABp$nNKKRe{7v}W3SMO0=kN';
...
34 $iface_lang = 'en_EN';
...
43 $dns_hostmaster = 'powerserver';
44 $dns_ns1 = '192.168.99.101';
45 $dns_ns2 = '192.168.99.101';
[powerDNS]$ rm -rf /var/www/html/poweradmin/install/
username:admin
password:123
这里如果登录不了提示:Error: You have to install PHP mcrypt extension
解决:
vim /etc/httpd/conf.modules.d/10-php.conf
...
5
6 LoadModule php5_module modules/libphp5.so
7 LoadModule php5_module modules/mcrypt.so
8
外网IP:172.16.23.200
内网IP:192.168.99.101
[防火墙]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[防火墙]# sysctl -P
iptables -t nat -A PREROUTING -d 172.16.23.200 -p tcp --dport 80 -j DNAT --to-destination 192.168.99.100
主机:192.168.99.107~109 |
107~109主机上都安装
yum -y install mariadb-server
systemctl start mariadb
systemctl enable mariadb
主服务器:192.168.99.107 |
vim /etc/my.cnf
[mysqld]
#加这2条
server_id=107
log_bin
systemctl restart mariadb
[主mysql]$ mysql
MariaDB [(none)]> grant replication slave on *.* to repluser@'%' identified by '123';
从服务器1:192.168.99.108 |
[从1]$ vim /etc/my.cnf
[mysqld]
server_id=108
read_only
systemctl restart mariadb
[从1]$ mysql
MariaDB [(none)]> change master to
master_host='192.168.99.107',
master_user='repluser',
master_password='123',
master_port=3306,
master_log_file='mariadb-bin.000001',
master_log_pos=0;
MariaDB [(none)]> start slave ;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.99.107
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 386
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 672
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
从服务器2:192.168.99.109 |
[从2]$ vim /etc/my.cnf
[mysqld]
server_id=109
read_only
systemctl restart mariadb
[从2]$ mysql
MariaDB [(none)]> change master to
master_host='192.168.99.107',
master_user='repluser',
master_password='123',
master_port=3306,
master_log_file='mariadb-bin.000001',
master_log_pos=0;
MariaDB [(none)]> start slave ;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.99.107
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000001
Read_Master_Log_Pos: 386
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 672
Relay_Master_Log_File: mariadb-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
主服务器:192.168.99.107 |
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
MariaDB [(none)]> set global rpl_semi_sync_master_enabled=on;
从服务器1:192.168.99.108 |
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=on;
MariaDB [(none)]> stop slave;
MariaDB [(none)]> start slave;
从服务器2:192.168.99.109 |
MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=on;
MariaDB [(none)]> stop slave;
MariaDB [(none)]> start slave;
主服务器:192.168.99.107 |
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE '%semi%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 2 |
...
MariaDB [(none)]> create database wpdb;
MariaDB [(none)]> grant all privileges on wpdb.* to wpuser@'192.168.99.%' identified by "123";
MariaDB [(none)]> grant replication client on *.* to monitor@'192.168.99.%' identified by '123';
MariaDB [(none)]> grant all on *.* to sqluser@'%' identified by '123';
proxySQL:192.168.99.106 |
[106]$ vim /etc/yum.repos.d/proxysql.repo
[proxysql_repo]
name= ProxySQL YUM repository
baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever
gpgcheck=1
gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
yum clean all
yum install proxysql
systemctl start proxysql
yum -y install mariadb
mysql -uadmin -padmin -P6032 -h127.0.0.1
连接成功,简单看看
MySQL [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name | file |
+-----+---------------+-------------------------------------+
| 0 | main | |
| 2 | disk | /var/lib/proxysql/proxysql.db |
| 3 | stats | |
| 4 | monitor | |
| 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
MySQL > insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.99.107',3306);
MySQL > insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.99.108',3306);
MySQL > insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.99.109',3306);
MySQL > select * from mysql_servers;
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10 | 192.168.99.107 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 20 | 192.168.99.108 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
| 20 | 192.168.99.109 | 3306 | ONLINE | 1 | 0 | 1000 | 0 | 0 | 0 | |
+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
MySQL > load mysql servers to runtime;
MySQL > save mysql servers to disk;
MySQL [(none)]> set mysql-monitor_username='monitor';
MySQL [(none)]> set mysql-monitor_password='123';
MySQL [(none)]> load mysql variables to runtime;
MySQL [(none)]> save mysql variables to disk;
MySQL [(none)]> select * from mysql_server_connect_log;
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
| hostname | port | time_start_us | connect_success_time_us | connect_error |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
...
| 192.168.99.107 | 3306 | 1564319683112439 | 3277 | NULL |
| 192.168.99.108 | 3306 | 1564319683641071 | 3359 | NULL |
| 192.168.99.109 | 3306 | 1564319684169766 | 1197 | NULL |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
MySQL> select * from mysql_server_ping_log;
MySQL> select * from mysql_server_read_only_log;
MySQL> select * from mysql_server_replication_lag_log;
设置分组信息
mysql_replication_hostgroups
表,该表有3个字段:writer_hostgroup
写组,reader_hostgroup
读组,comment
备注, 指定写组的id为10,读组的id为20MySQL> insert into mysql_replication_hostgroups values(10,20,"test");
MySQL> load mysql servers to runtime;
MySQL> save mysql servers to disk;
MySQL> select hostgroup_id,hostname,port,status,weight from mysql_servers;
+--------------+----------------+------+--------+--------+
| hostgroup_id | hostname | port | status | weight |
+--------------+----------------+------+--------+--------+
| 10 | 192.168.99.107 | 3306 | ONLINE | 1 |
| 20 | 192.168.99.108 | 3306 | ONLINE | 1 |
| 20 | 192.168.99.109 | 3306 | ONLINE | 1 |
+--------------+----------------+------+--------+--------+
MySQL> insert into mysql_users(username,password,default_hostgroup) values('sqluser','123',10);
MySQL> insert into mysql_users(username,password,default_hostgroup) values('wpuser','123',10);
MySQL> load mysql users to runtime;
MySQL> save mysql users to disk;
[proxySQL]$ mysql -usqluser -p123 -P6033 -h127.0.0.1 -e 'select @@server_id'
+-------------+
| @@server_id |
+-------------+
| 106 |
+-------------+
在proxysql上配置路由规则,实现读写分离
与规则有关的表:mysql_query_rules和mysql_query_rules_fast_routing,后者是前者的扩展表,1.4.7之后支持
插入路由规则:将select语句分离到20的读组,select语句中有一个特殊语句SELECT…FOR UPDATE它会申请写锁,应路由到10的写组
#先进入
[proxySQL]$ mysql -uadmin -padmin -P6032 -h127.0.0.1
MySQL> insert into mysql_query_rules
(rule_id,active,match_digest,destination_hostgroup,apply)VALUES
(1,1,'^SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);
MySQL> load mysql query rules to runtime;
MySQL> save mysql query rules to disk;
注意:因ProxySQL根据rule_id顺序进行规则匹配,select … for update规则的rule_id必须要小于普通的select规则的rule_id
MySQL [(none)]> select rule_id,active,match_digest,destination_hostgroup,apply from mysql_query_rules;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_digest | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| 1 | 1 | ^SELECT.*FOR UPDATE$ | 10 | 1 |
| 2 | 1 | ^SELECT | 20 | 1 |
+---------+--------+----------------------+-----------------------+-------+
到这里就可以实现读写分离了
在这里强调下:
(1)进入proxySQL管理界面是:mysql -uadmin -padmin -P6032 -h127.0.0.1
,端口号是:6032,默认的帐号密码是admin和admin。
(2)如果使用mysql -usqluser -p123 -P6033 -h127.0.0.1
则访问的是主服务器上的数据库了。
主机:192.168.99.105 |
[NFS]$ yum install -y nfs-utils
[NFS]$ mkdir /data/wordpress
[NFS]$ vim /etc/exports
/data/wordpress 192.168.99.0/24(rw,all_squash,anonuid=997,anongid=995)
#加载
[NFS]$ exportfs -r
[NFS]$ systemctl restart nfs-server
[NFS]$ groupadd -g 995 apache
[NFS]$ useradd -r -u 997 -g 995 -s /sbin/nologin apache
192.168.99.105 |
[NFS]$ cd
[NFS]$ wget ftp://192.168.99.1/Magedu37/files/lamp/wordpress-5.0.3-zh_CN.tar.gz
[NFS]$ tar xf wordpress-5.0.3-zh_CN.tar.gz -C /data/
[NFS]$ rm -f wordpress-5.0.3-zh_CN.tar.gz
[NFS]$ chown -R apache.apache /data/wordpress/
主机:192.168.99.104 |
[http]$ yum -y install php httpd php-mysql php-mbstring
[http]$ vim /etc/httpd/conf.d/test.conf
documentroot /data/wordpress
servername blog.jibill.com
require all granted
[http]$ systemctl restart httpd
[http]$ yum -y install nfs-utils
[http]$ mkdir /data/wordpress
[http]$ mount 192.168.99.105:/data/wordpress /data/wordpress
vim /etc/hosts
192.168.99.104 blog.jibill.com
主机:192.168.99.103 |
[httpA]$ yum -y install php httpd php-mysql php-mbstring
[httpA]$ vim /etc/httpd/conf.d/test.conf
documentroot /data/wordpress
servername blog.jibill.com
require all granted
[httpA]$ systemctl restart httpd
[httpA]$ yum -y install nfs-utils
[httpA]$ mkdir /data/wordpress
[httpA]$ mount 192.168.99.105:/data/wordpress /data/wordpress
[LVS]$ ip route del default via 192.168.99.2
[LVS]$ ip route add default via 192.168.99.101
[LVS]$ yum -y install ipvsadm
[LVS]$ ip a a 192.168.99.100 dev lo
[LVS]$ ipvsadm -A -t 192.168.99.100:80 -s wrr
[LVS]$ ipvsadm -a -t 192.168.99.100:80 -r 192.168.99.103 -g -w 1
[LVS]$ ipvsadm -a -t 192.168.99.100:80 -r 192.168.99.104 -g -w 1
下面2个设置在webA和webB上都要配置
4. 设置webA和webB的路由和arp设置
ip route del default via 192.168.99.2
ip route add default via 192.168.99.101
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ip a a 192.168.99.100 dev lo
192.168.88.100 |
blog.jibill.com/
成功