# /usr/local/ 安裝目錄 # /data/src/ 源文件包目錄 # yum install gcc gcc-c++ automake autoconf libtool make cmake
nginx依賴如下模塊:javascript
rewrite模塊須要 pcre 庫php
ssl 功能須要openssl庫css
gzip模塊須要 zlib 庫html
# tar -zxvf pcre-8.38.tar.gz # cd pcre-8.38 # ./configure # make & make install
# tar -zxvf openssl-1.0.1c.tar.gz # cd openssl-1.0.1c # ./config # make & make install
# tar -zxvf zlib-1.2.8.tar.gz # cd zlib-1.2.8 # ./configure # make & make install
先創建www用戶組,www用戶java
# groupadd www # useradd -g www www
接着配置時,直接指定pcre,zlib,openssl的路徑
node
# tar -zxvf nginx-1.4.2.tar.gz # cd nginx-1.4.2 # ./configure --with-http_ssl_module \--with-pcre=/data/src/pcre-8.38 \--with-zlib=/data/src/zlib-1.2.8 \--with-openssl=/data/src/openssl-1.0.1c # make & make install
沒有報錯的話,就說明安裝成功了。接下來配置nginx。路徑位於/usr/local/nginx/conf/nginx.conf,路徑不一樣的話,能夠用命令查找一下 locate nginx.conf,貼上個人配置:mysql
nginx.confnginx
user www www; worker_processes 10; pid /var/run/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; server_tokens off; expires 1h; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; error_page 404 /404.jpg; fastcgi_connect_timeout 20; fastcgi_send_timeout 30; fastcgi_read_timeout 120; fastcgi_buffer_size 256k; fastcgi_buffers 8 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_temp_path /dev/shm; gzip on; gzip_min_length 2048; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_types text/plain text/css application/xml application/x-javascript ; log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; server { listen 80; server_name localhost; index index.html index.htm index.php; root /data/www; charset utf-8; expires 2h; location ~ .*\.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; expires off; } if ( $request_uri ~* (\.svn|\.swp) ) { return 404; } access_log off; } }
測試配置文件是否正確c++
配置文件沒問題,確保系統的 80 端口沒被其餘程序佔用,啓動nginxsql
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ps命令查看一下啓動結果
安裝php以前,先安裝如下依賴包:
# yum -y install libmcrypt-devel mhash-devel libxslt-devel \ libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \ zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \ ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \ krb5 krb5-devel libidn libidn-devel openssl openssl-devel
接着安裝
# tar -zxvf php-5.5.30.tar.gz # cd php-5.5.30 # ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-iconv-dir=/usr/local/iconv --with-freetype-dir --with-jpeg-dir --with-png-dir --enable-zip --with-zlib --with-gd --enable-pcntl --disable-debug --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --disable-ipv6 --enable-static --enable-maintainer-zts --enable-sockets --enable-soap --with-openssl --enable-fpm # make & make install
沒有報錯的話,就說明安裝成功了。再接下來複制php.ini,php-fpm.conf到/etc/下並修改之:
cp /data/src/php-5.5.30/php.ini-development /etc/php.ini cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
php.ini
date.timezone = "Asia/Shanghai"
php-fpm.conf
[global] pid = /var/run/php-fpm.pid error_log = log/php-fpm.log [www] user = www group = www pm.max_children = 256 pm.start_servers = 256 pm.min_spare_servers = 5 pm.max_spare_servers = 256
OK,配置完成,啓動php-fpm
#/usr/local/php/sbin/php-fpm -c /etc/php.ini -y /usr/local/php/etc/php-fpm.conf
ps命令查看一下啓動結果
至此,nginx和php都安裝成功了,在/data/www/分別創建index.html,index.php,看是否能正常訪問。
# groupadd -r mysql # useradd -g mysql -r -M -s /sbin/nologin mysql //這個mysql用戶不能登陸
# tar -zxvf mariadb-5.5.46.tar.gz # cd mariadb-5.5.46 # cmake . -DMYSQL_UNIX_ADDR=/tmp/mysql1.sock -DSYSCONFDIR=/usr/local/db1 -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DMYSQL_USER=mysql -DCMAKE_INSTALL_PREFIX=/usr/local/db1 -DMYSQL_DATADIR=/home/data/db1 -DWITH_XTRADB_STORAGE_ENGINE=1 -DWITH_FEDERATEDX_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STPRAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWIYH_READLINE=1 -DWIYH_SSL=system -DVITH_ZLIB=system -DWITH_LOBWRAP=0 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci # make -j 4 # make install
複製配置文件
# cp /usr/local/db1/support-files/my-large.cnf /usr/local/db1/my.cnf
初始化數據庫
# /usr/local/db1/scripts/mysql_install_db --basedir=/usr/local/db1 --datadir=/home/data/db1 --user=mysql --defaults-file=/usr/local/db1/my.cnf
複製服務啓動腳本
# cp /usr/local/db1/support-files/mysql.server /etc/init.d/mysql1
編輯啓動腳本mysql1(路徑:/etc/init.d/mysql1)
找到 $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 & 行,改成 $bindir/mysqld_safe --defaults-file=/usr/local/db1/my.cnf --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 & 即添加了 --defaults-file=/usr/local/db1/my.cnf
賦予執行權限
# chmod +x /etc/init.d/mysql1 # chkconfig mysql1 on
修改配置文件my.cnf(路徑:/usr/local/db1/my.cnf)
[mysqld] log-error = /var/log/mysql_error.log pid-file =/usr/local/db1/mysql1.pid user=mysql datadir = /home/data/db1 basedir = /usr/local/db1 [mysqld_safe] log-error= /var/log/mysql_error.log pid-file=/usr/local/db1/mysql1.pid
更改 目錄權限
# chown -R mysql:mysql /home/data/db1 # chown -R mysql:mysql /home/data/db1/. # chown -R mysql:mysql /usr/local/db1 # chown -R mysql:mysql /usr/local/db1/.
啓動MariaDB:
# /etc/init.d/mysql1 start
設置root的密碼
# /usr/local/db1/bin/mysqladmin -u root password 'root'
重複3.2的步驟,把db1替換成db2,mysql1替換成mysql2,3306替換成3307,
至此,主備庫都安裝好了,而且成功啓動。ps命令查看結果以下:
創建文件目錄,用於主備同步二進制文件存放,並設置權限
# mkdir -p /home/data/binlogs/ # chown -R mysql:mysql /home/data/binlogs/
修改主庫配置文件my.cnf(路徑:/usr/local/db1/my.cnf)
[mysqld] log-bin=/home/data/binlogs/master-bin #設置用於存放二進制文件的路徑以及文件名(master-bin.000001) server-id = 1 #主備庫的id不能相同
修改備庫配置文件my.cnf(路徑:/usr/local/db2/my.cnf)
[mysqld] #log-bin=mysql-bin #註釋掉這一行 server-id = 2 #主備庫的id不能相同
重啓主庫,備庫
# /etc/init.d/mysql1 restart # /etc/init.d/mysql2 restart
進入主庫,創建backup用戶(指定IP地址,用戶名,密碼,分配權限),並查看master狀態。
# /usr/local/db1/bin/mysql -uroot -proot MariaDB [(none)]> CREATE USER 'backup'@'127.0.0.1' IDENTIFIED BY 'root'; MariaDB [(none)]> GRANT RELOAD,LOCK TABLES, REPLICATION CLIENT ,REPLICATION SLAVE ON *.* TO 'backup'@'127.0.0.1'; MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> SHOW MASTER STATUS; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000002 | 245 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
進入備庫,進行設置。
# /usr/local/db2/bin/mysql -uroot -proot MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='127.0.0.1',MASTER_USER='backup',MASTER_PASSWORD='root',MASTER_PORT=3306,MASTER_LOG_FILE='master-bin.000002',MASTER_LOG_POS=245; MariaDB [(none)]> START SLAVE; MariaDB [(none)]> SHOW SLAVE STATUS\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 127.0.0.1 Master_User: backup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000002 Read_Master_Log_Pos: 245 Relay_Log_File: mysql2-relay-bin.000005 Relay_Log_Pos: 530 Relay_Master_Log_File: master-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: 245 Relay_Log_Space: 1110 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: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec)
說明:
MASTER_HOST,指的是主庫用於同步的IP地址,由於本演示主庫備都在同一臺機子,因此是127.0.0.1。這個IP地址要和主庫所指的IP地一致'backup'@'127.0.0.1'
MASTER_USER,指的是主庫用於同步的用戶名,就是主庫所設置的backup
MASTER_PASSWORD,指的是主庫用於同步的密碼,root
MASTER_PORT,指的是主庫用於同步的端口,3306
MASTER_LOG_FILE,指的是主庫用於同步的二進制文件名稱,經過主庫SHOW MASTER命令獲得,FILE字段值
MASTER_LOG_POS,指的是主庫用於同步的二進制文件的偏移量,經過主庫SHOW MASTER命令獲得,Position字段值
Slave_IO_Running: Yes,Slave 的 I/O 狀態
Slave_SQL_Running: Yes,SQL 線程狀態
只有當Slave 的 I/O狀態和SQL 線程狀態,都爲YES時,才說明主備同步成功了。若是失敗了,就查看Last_SQL_Error的提示信號,再具體問題具體分析,解決了。
另外:
MariaDB [mysql]>use mysql; //選擇系統數據庫MySQL MariaDB [mysql]>select Host,User,Password from user; //查看全部用戶 MariaDB [mysql]>delete from user where password=""; //刪除全部密碼爲空的用戶 MariaDB [mysql]>GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'root'; //爲root添加遠程鏈接的能力 MariaDB [mysql]>flush privileges; //刷新MySQL的系統權限 MariaDB [mysql]>exit;
設置防火牆,以便局域網內的其它服務器能夠訪問(路徑:/etc/sysconfig/iptables),添加如下內容:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT
重啓防火牆
# /etc/init.d/iptables restart