nginx给Q2A网站做负载均衡的问题 以及数据库主主同步

来源:互联网 发布:如何汉化软件 编辑:程序博客网 时间:2024/06/11 12:47

一、     反向代理Q2A网站,显示结果只有文字,没有js、css、图片

F12查看,发现很多url   /qa-theme/candy/qa-styles.css?1.6.3   、/qa-content/jquery-1.7.2.min.js都是304或404错误,也就是nginx只是将页面的文字返回还给客户端,而js、css、图片找不到,于是添加location来找到Q2A站点的其他内容:
location~.*\.(png|js|css)${
      proxy_pass http://10.10.21.135;
}
reload即可完美访问。原来我写到是proxy_pass http://10.10.21.135/;  这行报错,proxy_pass在location内不能包括url部分,之前看视频也有加不加“/”的问题,百度下:在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

第一种:

location  /proxy/ {

          proxy_pass http://127.0.0.1:81/;

}

会被代理到http://127.0.0.1:81/test.html 这个url

 

第二咱(相对于第一种,最后少一个 /)

location  /proxy/ {

          proxy_pass http://127.0.0.1:81;

}

会被代理到http://127.0.0.1:81/proxy/test.html 这个url


详见原文:http://ftlynx.blog.51cto.com/2833447/839607




二、     有的页面要刷新一次才有结果

这是因为,有个节点没用Q2A网站。

三、     两个节点之间怎么共享数据

这里使用的是主从同步,待改进。修改完my.cnf后要重启数据库才能生效
主服务器:将10.10.21.135的mysql设为host,编辑/var/mysql/my.cnf,添加:
server-id=1
binlog-do-db=question2answer #提供数据同步服务的数据库
#set-variable=binlog-ignore-db=mysql #不记录数据库mysql的更新日志,,,因为设置了这项,导致我的mysql服务器重启失败,在网上查了好多办法,最后还是通过查看错误日志,找到了错误原因,我把这句话注释掉就重启OK了。
进入mysql,增加一个账号专门用于同步
# mysql -h10.10.21.135 -p*****
 mysql>GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO tong@10.10.21.138 IDENTIFIED BY 'pw';
从服务器:修改配置文件my.cnf(win 服务器为 my.ini)
server-id=2 #不能与主服务器配置相同
master-host=10.10.21.135 #主服务器的地址
master-user=tong #主服务器提供给从服务器的用户,该用户中需要包括数据库question2answer的权限
master-password=pw #访问密码  
master-port=3306 #端口,主机的MYSQL端口
master-connect-retry=5 #重试间隔60秒
set-variable=replicate-ignore-db=mysql #略过同步的数据库名,如果有多个,请设置多次
set-variable=replicate-do-db=question2answer #想要同步的数据库名,如果有多个,请设置多次
测试从服务器是否同步:
mysql -p
SHOW SLAVE STATUS\G



看这个:


双向同步的实现
转载于:http://cgwxyz.blog.163.com/blog/static/2628060201101703712766/

1.在两台mysql上创建用户,设置权限

138上添加:

#grant replication slave,replication client,reload,super on *.* to 'test'@'10.10.21.135' identified by '123456' with grant option;/

135上:

#grant replication slave,replication client,reload,super on *.* to 'test'@'10.10.21.138' identified by '123456' with grant option;//用于A访问

执行  #flush privileges; 更新数据库使用户生效。

2.在my.cnf上进行相关配置

135B

server-id         = 2

#双向以解决自动增加id的冲突,设步长2,初始2

auto_increment_increment=2

auto_increment_offset=2

master-host     =

10.10.21.138

master-user     =test

master-pass     =123456

master-port     =3306

master-connect-retry=60

replicate-do-db =db1

replicate-ignore-db=mysql 

server-id         = 1

auto_increment_increment=2

auto_increment_offset=1


master-host     =10.10.21.135

master-user     =test

master-pass     =123456

master-port     =3306

master-connect-retry=60

replicate-do-db =db1


replicate-ignore-db=mysql

  

注意

1.server_id必须为唯一.

2.如果想要同时同步多个库,添加多行replicate-do-db,每行指定一个数据库。不能使用replicate-do-db=db1,db2的形式

3.replicate-ignore-db:指定不进行同步的数据库。

保存后,重启mysql


3.把两台服务器上需要同步的数据库进行拷贝,保证这两台数据库初始状态一致。

4.进行双向同步

双向同步就是把单向同步反过来在做一遍,但一定要注意操作的顺序,这是成功的关键

step1.在A上mysql shell中执行

#show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000054 |    35 |              |                  | 

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

记录下 mysql-bin.000054,和35

step2.在B上执行:

#stop slave;//停止同步

#

CHANGE MASTER TO  MASTER_HOST='10.10.21.135', MASTER_PORT=3306, MASTER_USER='test', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000054', MASTER_LOG_POS=35;

#start slave;//开始同步

step3,执行show slave status\G;如显示如下内容,表示同步设置成功。

Slave_IO_State: Waiting for master to send event

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

step4:上一步没有问题。则在B上继续执行show master status;

#show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000005 |    6854 |              |                  | 

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

step5:在A上执行

#stop slave;//停止同步

#

CHANGE MASTER TO  MASTER_HOST='10.10.21.138', MASTER_PORT=3306, MASTER_USER='test', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=6854;

#start slave;//开始同步

step6:执行show slave status\G;如显示如下内容,表示同步设置成功。

Slave_IO_State: Waiting for master to send event

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

如上述没有啥问题。到此双向同步设置完成。



四、  使用简体中文

将简体中文包放在qa-lang文件夹内

0 0
原创粉丝点击