以下內容只是本人實驗的記錄筆記。以下內容是參照某真實案例中的部分內容所攢寫,當然本人對Linux各應用系統不是很熟悉,大家將就看看,有錯的請指出。由於本人是在VMWare上仿照生產網絡環境搭建的架構,爲貪圖方便,故所有服務器IP都同一網絡,在生產環境中管理、應用等都是不同網段並且是從實體網絡設備上隔離,下圖大概的架構圖。
Haproxy 安裝
# cd /home
# tar haproxy-1.5.8.tar.gz
# cd haproxy-1.5.8
# make TARGET=linux26 ARCH=x86_64 -->TARGET是指定內核版本,ARCH指定CPU架構,我使用的是64bit系統
# make install
# mkdir /etc/haproxy
# cp examples/haproxy.cfg /etc/haproxy
# cp examples/haproxy.init /etc/init.d/haproxy
# chmod +x /etc/init.d/haproxy
# ln -s /usr/local/sbin/haproxy /usr/sbin/
# mkdir /usr/share/haproxy
編輯配置文件(兩臺haproxy配置文件相同)
# this config needs haproxy-1.5.8
global
log 127.0.0.1 local0 #日誌輸出配置,所有日誌都記錄在本機,通過local0輸出
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096 #最大連接數
chroot /usr/share/haproxy #改變當前工作目錄。
uid 99 #所屬用戶的uid
gid 99 #所屬運行的gid
daemon #以後臺形式運行haproxy
#debug
#quiet
defaults
log global
mode http #默認的模式mode { tcp|http|health },tcp是4層,http是7層,health只會返回OK
# option httplog
option dontlognull
retries 3 #兩次連接失敗就認爲是服務器不可用
option redispatch #當serverId對應的服務器掛掉後,強制定向到其他健康的服務器
option abortonclose #當服務器負載很高的時候,自動結束掉當前隊列處理比較久的鏈接
maxconn 2000 #默認的最大連接數
timeout connect 5000 #連接超時
timeout client 50000 #客戶端超時
timeout server 50000 #服務器超時
timeout check 5s #心跳檢測超時
listen www.master.com 172.16.1.60:80 #設定對外服務器的名稱或是IP,對外服務器時使用80 Port
# option httpchk *
balance leastconn #banlance roundrobin 輪詢,balance source 保存session值,支持static-rr,leastconn,first,uri等參數
option httplog
cookie SERVERID insert indirect nocache
server slave2 172.16.1.52:80 cookie server01 check inter 2000 fall 3
server slave3 172.16.1.53:80 cookie server02 check inter 2000 fall 3
server slave4 172.16.1.54:80 cookie server03 check inter 2000 fall 3
capture cookie ASPSESSION len 32
timeout server 20000
option httpclose # disable keep-alive
option checkcache # block response if set-cookie & cacheable
rspidel ^Set-cookie:\ IP= # do not let this cookie tell our internal IP address
listen admin_stats #haproxy服務狀態
bind 172.16.1.60:8888
option httplog
stats auth admin:admin
stats uri /stats
stats admin if TRUE
stats hide-version
stats refresh 5s
listen HA-Mysql 172.16.1.60:23306 # Mysql高可用性服務
mode tcp
option mysql-check user haproxy # 需要在被監控的機器上的mysql新建一個無密碼用戶haproxy(此可以隨便建: create user 'haproxy'@'%' identified by ''; )
balance roundrobin
server slave2db01 172.16.1.52:3306 weight 1 check inter 12000 rise 3 fall 2
server slave3db02 172.16.1.53:3306 weight 1 check inter 12000 rise 3 fall 2
server slave3db03 172.16.1.54:3306 weight 1 check inter 12000 rise 3 fall 2
option tcpka
啓動haproxy服務,查看狀態: service haproxy start
HAProxy故障說明:
[ALERT] 164/110030 (11606) : Starting proxy linuxyw.com: cannot bind socket
這個問題,其實就是因爲你的haproxy沒有得到VIP的原因,而你的配置文件又綁定了VIP地址,所以會提示以上錯誤。當然,你也要確保你的haproxy服務器做了hearbeat或keepalived,綁定VIP,要不就無法高可用了。
解決方法:
修改內核參數: /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1
保存結果,使結果生效
sysctl -p
或者使用echo進去,前提是sysctl.conf文件中沒有本條參數:
echo 'net.ipv4.ip_nonlocal_bind = 1'>>/etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1 意思是啓動haproxy的時候,允許忽視VIP的存在
---------------------------------------------
keepalive 安裝與配置
安裝與配置keepalived (wget http://www.keepalived.org/software/keepalived1.2.22.tar.gz )
[root@master keepalived-1.2.22]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@master keepalived-1.2.22]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@master keepalived-1.2.22]# mkdir /etc/keepalived -p
[root@master keepalived-1.2.22]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@master keepalived-1.2.22]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@master keepalived-1.2.22]# /etc/init.d/keepalived start
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 172.16.1.25
smtp_connect_timeout 30
router_id LVS_7
}
vrrp_instance VI_1 {
state MASTER #設置爲主服務器
interface eth0 #監聽網絡接口
virtual_router_id 55 #主、備必須一樣相當於VR(虛擬路由器)ID
priority 150 #(主、備機取不同的優先級,主機值較大,備份機值較小,值越大優先級越高)
advert_int 1 #VRRP Multicast廣播週期秒數
authentication {
auth_type PASS #VRRP認證方式,主備必須一致
auth_pass 1111 #(密碼)
}
virtual_ipaddress {
172.16.1.60/24 #VRRP HA虛擬地址
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 56
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.61/24
}
}
查看狀態
--------------------------------------------------------------------------------------
Nginx安裝
#tarzxvf nginx-1.6.2.tar.gz
#cd nginx-1.6.2
#useradd -s/sbin/nologin nginx -->創建不登錄用戶
# ./configure--user=nginx --group=nginx --prefix=/opt/nginx
# make && makeinstall
/opt/nginx/conf/nginx.conf 文件配置如下(三臺Web服務配置文件相同):
[root@slave2 conf]#cat nginx.conf
#user nobody;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local]"$request" '
'$status$body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name master.com;
root html/www;
index index.html index.htm;
rewrite ^/(.*)http://www.master.com/$1 permanent; #ip防問301跳轉第二種方法,permanent永久跳轉;
}
server{
listen 80;
server_name www.master.com master.com; #第一個虛擬服務器,增加master.com別名實現跳轉(第一種方法)
root html/www;
index index.html index.htm;
}
server{
listen 80;
server_name bbs.master.com; #第二個虛擬服務器
root html/bbs;
index index.html index.htm;
}
server{
listen 80;
server_name blog.master.com; #第三個虛擬服務器
root html/blog;
index index.html index.htm;
}
}
######################################################################################
# 第二第配置nginx.conf寫法
#mkdir/opt/nginx/conf/extra -->建立extra文件夾用於存放各類server.confserver
# 修改nginx.conf配置如下:
[root@slave2 conf]#cat nginx.conf
#user nobody;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local]"$request" '
'$status$body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include extra/www.conf;-->加載配置文件
include extra/blog.conf;-->加載配置文件
include extra/bbs.conf;-->加載配置文件
}
#nginx 增加對PHP支持需安裝如下軟件:
libmcrypt-2.5.8.tar.gz
mhash-0.9.9.9.tar.gz
libiconv-1.14.tar.gz
php-fpm-5.3.3-47.el6.x86_64.rpm-->此爲nginx需用php-fpm,端口爲9000,啓動命令 service php-fpm start
php-fpm的配置文件/etc/php-fpm.d/www.conf
#各類配置如下:
備註:1). 原配置文件對php支持語句爲:fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
2). 必須改爲:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
3). 通過location指令,將所有以php爲後綴的文件都給給127.0.0.1:9000來處理,而這裏的ip address和端口就是FastCGI進程監聽的IP Address和端口;
*如果沒有php-fpm沒有或是啓動 nginx對PHP文件報File not found.錯
#####################################################################################
#/opt/nginx/sbin/nginx-s reload -->重新加載服務器
#/opt/nginx/sbin/nginx->服務器啓動
#/opt/nginx/sbin/nginx-t ->檢查語法
# ps -ef | grep nginx/ # lsof -i :80 / #netstat -lntcp | grep 80
-------------------------------------------------------------------
Mysql主從服務器
1.主從服務器分別作以下操作:
1.1、版本一致
1.2、初始化表,並在後臺啓動mysql
1.3、修改root的密碼
2、修改主服務器master:
#vi /etc/my.cnf
[mysqld]
log_bin=mysql-bin //[必須]啓用二進制日誌
server-id=222 //[必須]服務器唯一ID,默認是1,一般取IP最後一段
3、修改從服務器slave:
#vi /etc/my.cnf
[mysqld]
log_bin=mysql-bin //[不是必須]啓用二進制日誌
server-id=226 //[必須]服務器唯一ID,默認是1,一般取IP最後一段
4、重啓兩臺服務器的mysql
/etc/init.d/mysql restart
5、在主服務器上建立帳戶並授權slave:
GRANTREPLICATION SLAVE ON *.* TO 'slave'@'172.16.1.53' IDENTIFIED BY 'password';
GRANTREPLICATION SLAVE ON *.* TO 'slave'@'172.16.1.52' IDENTIFIED BY 'password';
6、登錄主服務器的mysql,查詢master的狀態
mysql>show masterstatus;
7、配置從服務器Slave:
mysql> CHANGEMASTER TO MASTER_HOST='172.16.1.54',
master_user ='slave',
master_password ='password',
master_log_file ='mysql-bin.00002',
master_log_pos = 191;
8、檢查從服務器複製功能狀態:
mysql>show slave status \G
9.Mysql開啓遠程登錄
(1)通過MySQL用戶去限制訪問
權限系統目的:
MySQL基於安全考慮root賬戶一般只能本地訪問,但是在開發過程中可能需要打開root的遠程訪問權限,今天介紹的就是如何開啓和關閉Mysql遠程訪問
MySQL權限系統的主要功能是證實連接到一臺給定主機的用戶,並且賦予該用戶在數據庫上的SELECT、INSERT、UPDATE和DELETE權限。
附加的功能包括有匿名的用戶並對於MySQL特定的功能例如LOAD DATA INFILE進行授權及管理操作的能力。
權限系統原理:
MySQL權限系統保證所有的用戶只執行允許做的事情。當你連接MySQL服務器時,你的身份由你從那兒連接的主機和你指定的用戶名來決定。
連接後發出請求後,系統根據你的身份和你想做什麼來授予權限。
MySQL在認定身份中考慮你的主機名和用戶名字,是因爲幾乎沒有原因假定一個給定的用戶在因特網上屬於同一個人。
例如,從office.com連接的用戶joe不一定和從elsewhere.com連接的joe是同一個人。
MySQL通過允許你區分在不同的主機上碰巧有同樣名字的用戶來處理它:你可以對joe從office.com進行的連接授與一個權限集,而爲joe從elsewhere.com的連接授予一個不同的權限集。
階段1:服務器檢查是否允許你連接。
階段2:假定你能連接,服務器檢查你發出的每個請求。看你是否有足夠的權限實施它。
例如,如果你從數據庫表中選擇(select)行或從數據庫刪除表,服務器確定你對錶有SELECT權限或對數據庫有DROP權限。 如果連接時你的權限被更改了(通過你和其它人),這些更改不一定立即對你發出的下一個語句生效。MySQL權限是保存在cache中,這個時候就你需要執行
flush privileges;
開啓遠程訪問:
- 更新用戶
mysql>use mysql;
mysql>update userset host = "%" where user = "root";
mysql>flushprivileges;
- 添加用戶
mysql>use mysql;
mysql>insert intouser(host, user, password) values("%", "root",password("yourpassword"))
mysql>grant all privilegeson *.* to 'root'@'%' with grant option #賦予任何主機訪問數據庫權限
mysql>flush privileges;
-關閉遠程訪問:
mysql> use mysql;
mysql>update userset host = "localhost" where user = "root" and host="%";
mysql> flushprivileges;
- 查看用戶權限:
mysql> useinformation_schema;
mysql> select *from user_privileges;
查看當前mysql用戶:
mysql>use mysql;
mysql>select user, hostfrom user;
更新用戶:
mysql> updatemysql.user set password=password('新密碼') whereUser="phplamp" and Host="localhost";
mysql> flushprivileges;
刪除用戶:
mysql> DELETEFROM user WHERE User="phplamp" and Host="localhost";
mysql> flushprivileges;
user host指定方法:
Host值可以是主機名或IP號,或’localhost’指出本地主機。
你可以在Host列值使用通配符字符“%”和“_”。
host值’%’匹配任何主機名,空Host值等價於’%’。它們的含義與LIKE操作符的模式匹配操作相同。例如,’%’的Host值與所有主機名匹配,而’%.mysql.com’匹配mysql.com域的所有主機。
##############################################################################################
MySQL從5.1升級至5.6會出現 Mysql創建用戶出錯:ERROR1054 (42S22): Unknown column 'plugin' in 'mysql.user'
mysql> create userqzwx identified by 'qzwx';
ERROR 1054 (42S22):Unknown column 'plugin' in 'mysql.user'
經排查,發現是5.16版本mysql.user表的沒有plugin,而升級數據庫的時候,mysql.user表沒有升級,才導致字段缺少,可以通過以下方法修改mysql.user表:
mysql>use mysql;
mysql>ALTER TABLEuser ADD Create_tablespace_priv ENUM('N','Y') NOT NULL DEFAULT 'N' AFTERTrigger_priv;
mysql>ALTER TABLEuser ADD plugin CHAR(64) NULL AFTER max_user_connections;
mysql>ALTER TABLEuser ADD authentication_string TEXT NULL DEFAULT NULL AFTER plugin;
mysql>ALTER TABLEuser ADD password_expired ENUM('N','Y') NOT NULL DEFAULT 'N' AFTERauthentication_string;
############################################################################################
MYSQL5.1升級至5.6
備份數據庫,升級MySQL通常不會丟失數據,但保險起見,我們需要做這一步。輸入命令: mysqldump -u xxx -h xxx -P 3306 -p --all-databases > databases.sql
停止MySQL服務:service mysqld stop
卸載舊版MySQL,輸入命令 yum remove mysql mysql-* -y
移除命令執行後,可再看看是否有殘餘的mysql,輸入命令:yum list installed | grep mysql
如果有,可輸入命令刪除:rum remove mysql-libs
下載安裝最新的rpm文件: rpm -Uvh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
安裝MySQL,輸入命令:yum install mysql-community-server
檢查安裝完成的版本
*升級後mysql不能啓動,要檢查/etc/my.cnf 配置文件以及刪除/var/lib/mysql下面log文件,如ibdata1 ib_logfile0 ib_logfile1
* 升級後slave主機不能連接master主機,要先重置slave配置文件以及刪除/var/lib/mysql下面的log文檔,命令reset slave;再重新加載連接master配置
Mysql問題解決方案
起動bin-log 日誌,master不能啓動並出現以下如下問題:
2016-09-07 08:45:2521038 [ERROR] Failed to open log (file './mysql-bin.000004', errno 2)
2016-09-07 08:45:2521038 [ERROR] Could not open log file
2016-09-07 08:45:2521038 [ERROR] Can't init tc log
2016-09-07 08:45:2521038 [ERROR] Aborting
解決方法:進入mysql目錄刪除 mysql-bin.index 文件,讓系統重新建立mysql-bin.index
MySQL主從失敗 錯誤Got fatal error 1236解決方法show slave status錯誤:
mysql> show slave status\G
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 120
Slave_IO_Running: No
.....
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log:
'Client requested master to start replication from impossible position'
查看Mysql Log
[root@host03log]# cat mysqld.log
2016-09-07 10:31:3518240 [ERROR] Error reading packet from server: Could not find first log filename in binary log index file (server_errno=1236)
2016-09-07 10:31:3518240 [ERROR] Slave I/O: Got fatal error 1236 from master when reading datafrom binary log: 'Could not find first log file name in binary log index file',Error_code: 1236
2016-09-07 10:31:3518240 [Note] Slave I/O thread exiting, read up to log 'log-bin.000002',position 120
2016-09-07 10:31:3518240 [Warning] Slave SQL: If a crash happens this configuration does notguarantee that the relay log info will be consistent, Error_code: 0
2016-09-07 10:31:3518240 [Note] Slave SQL thread initialized, starting replication in log'log-bin.000002' at position 120, relay log './mysqld-relay-bin.000001'position: 4
解決方法:
[root@host03 log]#mysqlbinlog /opt/mysql/data01/mysql-bin.000001 > test.txt
[root@host03 log]#cat test.txt
/*!50530 SET@@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET@@session.max_insert_delayed_threads=0*/;
/*!50003 SET@OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#160907 10:31:34server id 43 end_log_pos 120 CRC32 0xeec57cc1 Start:binlog v 4, server v 5.6.32-log created 160907 10:31:34 at startup
# Warning: this binlogis either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
BnzPVw8rAAAAdAAAAHgAAAABAAQANS42LjMyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAGfM9XEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAcF8
xe4=
'/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added bymysqlbinlog */;
/*!50003 SETCOMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET@@SESSION.PSEUDO_SLAVE_MODE=0*/;
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_log_file='mysql-bin.000001',master_log_pos=4;
Query OK, 0 rows affected (0.06 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
出現以目兩項表明正常運行
管理日誌常用命令
mysql> show binlog events;
mysql> show binlog events in 'mysql-bin.000001';
mysql> show binlog events in 'mysql-bin.000001' from 120;