龚哥哥 - 山里男儿 爱生活、做自己!
MySQL双主_单主_热备份_实时备份
发表于 2015-8-31 | 浏览(6520) | 数据库

6630381670443118394.jpg

两台服务器

主库ip:192.168.1.1
从库ip:192.168.1.2

主库配置、编辑 my.cnf  [mysqld]下添加

server-id=1
log-bin=mysql-bin
relay-log=relay-bin
relay-log-index=relay-bin-index
replicate-do-db=devil #需要备份的数据库
slave_skip_errors = 1062 #忽略错误

#解决互相数据插入自增冲突
auto_increment_offset=1
auto_increment_increment=2

保存重启mysql

从库配置、编辑 my.cnf  [mysqld]下添加

server-id=2
log-bin=mysql-bin
relay-log=relay-bin
relay-log-index=relay-bin-index
replicate-do-db=devil #需要备份的数据库
slave_skip_errors = 1062 #忽略错误

#解决自增冲突
auto_increment_offset=2
auto_increment_increment=2

保存重启mysql

主库配置、进入mysql控制台执行

mysql -uroot -proot

stop slave;

change master to master_host='192.168.1.2',master_user='root',master_password='root';

开启配置
start slave;

查看警告
Show warnings;

查看配置状态
show slave status\G

    以下两项yes表示状态配置成功,如果error有错误根据提示解决:
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

从库配置、进入mysql控制台执行

change master to master_host='192.168.1.1',master_user='root',master_password='root';

stop slave;

start slave;

Show warnings;

show slave status\G

以下两项yes表示状态配置成功,如果error有错误根据提示解决:
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

如果以上顺利通过,则可以进行测试了。以下是可能遇到的问题。

日志冲突解决

查看所有二进制日志
SHOW MASTER LOGS;

清空所有二进制日志
RESET MASTER;

网络连接失败

Last_IO_Errno: 1130
1;一般是防火墙开启,拒绝3306端口。或者是配置错误,如用户名或密码
2;还有可能是主库备份帐号的访问权限,进入主库mysql控制台
  use mysql;
  select * from user where user='root';
  update user set host = '%' where user ='root';
  flush privileges;

如果不使用root用户,可以创建一个备份用户

grant replication slave,replication client on *.* to '用户名'@'可执行操作的ip' identified by '密码';

阅读全文

jQuery的基本选择器使用
发表于 2015-8-31 | 浏览(5369) | 前端

废话不多说,直接上代码,将所有 赋值到一个 html 文件中即可测试,// 表示注释

jquery-1.8.3.min.js 这个jQuery 下载地址,

下载地址:http://code.jquery.com/jquery-1.8.3.min.js

<!DOCTYPE html
<html>
    <head>
        <meta charset="UTF-8" />
        <title>jQuery 属性选择器</title>
        <script type="text/javascript" src="./jquery-1.8.3.min.js"></script>
        <script type="text/javascript">
            //$(document).ready(function() { 会选中全部
            $(document).ready(function() {
                //点击 button 按钮就会调用
                $('button').click(function() {
                    //$('#dd').css('color', '#F00');
                    //$('.cc').css('font-size', '38px');  //单个

                    //同时选作多个设置颜色
                    //$('#dd, .cc').css('color', '#0ff');

                    //会选择 ul 下的所有子元素,多级
                    //$('.ul span').css('color', '#F00');

                    //只会选择 ul 下的子元素,孙元素不会被选中
                    //$('.ul > span').css('color', '#F00');

                    //选作 class iia 紧的挨着的下一个元素
                    //$('.iia + li').css('color', '#F00').css('font-size', '38px');

                    //下面有两个iib 这样子的话,就会选中两个,最后一个和跟最后一个键紧挨着的一个
                    //$('.iib + li').css('color', '#F00');
                    //当选择下一个紧挨着的一个元素不存在时,则不会生效
                    //$('#iic + li').css('color', '#F00');

                    //意思class iia 下面的所有子 li 元素。逗号,则不一样的元素
                    //$('.iia ~ li,p').css('color', '#F00');

                    // 选中html标签中 属性为 id 的所有元素
                    //$('[id]').css('color', '#F00');

                    // 选中html标签中 属性为 for 的所有元素
                    //$('[for]').css('color', '#F00');

                    // 选中html标签中 属性为 asdf 的所有元素,可以应用于自定义属性
                    //$('[asdf]').css('color', '#F00');

                    //选中属性 for 等于 aaa 的所有元素
                    //$('[for="aaa"]').css('color', '#F00');

                    //选中 class 属性值为 php 的所有元素
                    //$('[class="php"]').css('color', '#F00');

                    //选中 class 不为 php 的元素。
                    //$('[class != "php"]').css('color', '#F00');

                    //选中 class 为 i 字母开头的元素
                    //$('[class ^= "i"]').css('color', '#F00');

                    //选中 class 以 b 字母结束的元素
                    //$('[class $= "b"]').css('color', '#F00');

                    //选中 class 包含 f 字母的所有元素
                    //$('[class *= "f"]').css('color', '#F00');

                    //选中 标签中必须包含 class 和 id 属性等于 iic 的元素
                    //$('[class][id = "iic"]').css('color', '#F00');

                    //选中标签中同时包含 class 属性值为 gege 和 id 属性值为 gg 的元素
                    //$('[class = "gege"][id = "gg"]').css('color', '#F00');

                    //选中 class ul,中间的空格表示下一及,后代选择器,下 class 的值为 iid 和 id 值为 bod 的元素
                    $('.ul [class = "iid"][id = "bod"]').css('color', '#F00');
                });
            });

        </script>
    </head>
    <body>
        <button>按钮 1</button>
        <button>按钮 2</button>
        <div id="dd">div dd</div>
        <div class="cc">div cc</div>
        <div class="cc">div cc2</div>

        <ul class="ul">
        <span>你是我的uuuullll</span>
            <li class="iia" id="good">苹果手机aagood 2</li>
            <li class="iib">安卓手机12bbb</li>
            <li class="iibf">软件开发2bbb</li>
            <li id="iic" class="php" asdf="fff">程序员2ccc</li>
            <p>p看看 ppp</p>
            <span>
                <li class="iid" id="bod" for="ccc">嘻嘻嘻嘻</li>
                <li id="good">哈哈哈哈哈good 2</li>
            </span>
        <li class="df">l滴滴滴滴
            <span>spspsp</span>
        </li>
        </ul>
        <label for="aaa" class="gege" id="gg">l乐乐乐乐</label>
        <li class="iid" id="bod" for="ccc">嘻嘻嘻嘻2222222</li>
    </body>
</html>

阅读全文

CentOS安装memcached服务
发表于 2015-8-31 | 浏览(4988) | 服务器

1;yum安装

yum -y install memcached

2;验证是否安装成功,如果输出一些帮助信息表示安装成功

memcached -h

3;将memcache加入启动列表

chkconfig --level 2345 memcached on

4;配置Memcache

vi /etc/sysconfig/memcached
文件中内容如下
PORT=”11211″ 端口
USER=”root” 使用的用户名
MAXCONN=”1024″ 同时最大连接数
CACHESIZE=”64″ 使用的内存大小
OPTIONS=”" 附加参数

5;启动memcached

启动:service memcached start
关闭:service memcached stop
重启:service memcached restart

加入系统启动项
  echo "service memcached start" >> /etc/rc.local 

6;分配空间(11211 端口号可以自定义设置)

memcached -p 11211 -m 512 -d -u root start

7;连接memcached

telnet 121.199.44.203 11211

阅读全文

php安装memcache和memcached扩展
发表于 2015-8-31 | 浏览(5441) | 服务器

安装php memcache

wget http://pecl.php.net/get/memcache-3.0.6.tgz
tar -zxvf memcache-3.0.6.tgz
cd memcache-3.0.6
/data/soft/php/bin/phpize (如果不知道phpize在什么位置,可以用find / -name phpize查找)
./configure –enable-memcache –with-php-config=/data/soft/php/bin/php-config –with-zlib-dir
make
make install

记录下安装成功后的提示,类似于:
Installing shared extensions: /data/soft/php/modules/
把这个地址记录下来

在php.ini文件里面添加扩展

vim /data/soft/php/bin/php.ini
添加 extension=memcache.so

最后验证一下是否安装完成

php -m|grep memcache
应该会显示memcache

到这里php memcach安装完成

安装php memcached扩展

1、先安装 memcached 依赖库 libmemcached

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/usr/local/libmemcached --with-memcached
make
make install

现在安装php memcached扩展

wget http://pecl.php.net/get/memcached-2.2.0.tgz
tar -zxvf memcached-2.2.0.tgz
cd memcached-2.2.0
/data/soft/php/bin/phpize
./configure --enable-memcached --with-php-config=/data/soft/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl

在php.ini文件里面添加扩展

vim /data/soft/php/bin/php.ini
  添加 extension=memcached.so

最后验证一下是否安装完成

php -m|grep memcached
应该会显示memcached

到这里php memcachd安装完成

阅读全文

CentOS和Linux使用crontab运行定时任务(做一些你想做的)
发表于 2015-8-31 | 浏览(5438) | 服务器

安装crontab:

yum install crontabs

说明:

/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置

查看crontab服务状态:service crond status
手动启动crontab服务:service crond start
查看crontab服务是否已设置为开机启动,执行命令:ntsysv

加入开机自动启动

chkconfig –level 35 crond on

1、crontab 命令

功能说明:设置计时器。
语  法:crontab [-u <用户名称>][配置文件] 或crontab [-u <用户名称>][-elr]
补充说明:cron是一个常驻服务,它提供计时器的功能,让用户在特定的时间得以执行预设的指令或程序。只要用户会编辑计时器的配置文件,就可以使 用计时器的功能。其配置文件格式如下:
Minute Hour Day Month DayOFWeek Command

参  数:
-e  编辑该用户的计时器设置。
-l  列出该用户的计时器设置。
-r  删除该用户的计时器设置。
-u<用户名称>  指定要设定计时器的用户名称。

2、crontab 格式

基本格式:
* * * * *  command
分 时 日 月 周  命令
第1列表示分钟1~59 每分钟用*或者*/1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列 表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令

# Use the hash sign to prefix a comment
# +—————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +———- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +—- day of week (0 – 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed

crontab文件的一些例子:

30 21 * * * /etc/init.d/nginx restart
每晚的21:30重启nginx。

45 4 1,10,22 * * /etc/init.d/nginx restart
每月1、10、22日的4 : 45重启nginx。

10 1 * * 6,0 /etc/init.d/nginx restart
每周六、周日的1 : 10重启nginx。

0,30 18-23 * * * /etc/init.d/nginx restart
每天18 : 00至23 : 00之间每隔30分钟重启nginx。

0 23 * * 6 /etc/init.d/nginx restart
每星期六的11 : 00 pm重启nginx。

* */1 * * * /etc/init.d/nginx restart
每一小时重启nginx

* 23-7/1 * * * /etc/init.d/nginx restart
晚上11点到早上7点之间,每 隔一小时重启nginx

0 11 4 * mon-wed /etc/init.d/nginx restart
每月的4号与每周一到周三 的11点重启nginx

0 4 1 jan * /etc/init.d/nginx restart
一月一号的4点重启nginx 

*/30 * * * * /usr/sbin/ntpdate 210.72.145.20
每半小时同步一下时间

阅读全文

CentOS下完全卸载mysql
发表于 2015-8-31 | 浏览(5648) | 数据库

一、yum方式安装的mysql

yum remove mysql mysql-server mysql-libs compat-mysql51
rm -rf /var/lib/mysql
rm /etc/my.cnf

查看是否还有mysql软件

rpm -qa|grep mysql

如果存在的话,继续删除即可。

二、rpm方式安装的mysql

1、查看系统中是否以rpm包安装的mysql:

[root@localhost opt]# rpm -qa | grep -i mysql
MySQL-server-5.6.17-1.el6.i686
MySQL-client-5.6.17-1.el6.i686

2、卸载mysql

[root@localhost local]# rpm -e MySQL-server-5.6.17-1.el6.i686
[root@localhost local]# rpm -e MySQL-client-5.6.17-1.el6.i686

3、删除mysql服务

[root@localhost local]# chkconfig --list | grep -i mysql
[root@localhost local]# chkconfig --del mysql

4、删除分散mysql文件夹

[root@localhost local]# whereis mysql 或者 find / -name mysql
mysql: /usr/lib/mysql /usr/share/mysql

清空相关mysql的所有目录以及文件

rm -rf /usr/lib/mysql
rm -rf /usr/share/mysql
rm -rf /usr/my.cnf

通过以上几步,mysql应该已经完全卸载干净了

阅读全文

MySQL mysql-5.1升级到mysql-5.6
发表于 2015-8-31 | 浏览(5583) | 数据库

查看是否有旧版本mysql

rpm -qa|grep mysql

CentOS卸载旧版本mysql

yum remove mysql mysql-server mysql-libs compat-mysql51
rm -rf /data/soft/mysql/*
rm /etc/my.cnf.rpmsave

删除mysql服务
  chkconfig --list | grep -i mysql
  chkconfig --del mysql

删除分散mysql文件夹
  whereis mysql 或者 find / -name mysql

安装cmake

wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2.tar.gz
tar -zxvf cmake-2.8.10.2.tar.gz
cd cmake-2.8.10.2
./bootstrap
gmake
gmake install

下载mysql安装包进行安装

wget http://dev.mysql.com/get/archives/mysql-5.6/mysql-5.6.11.tar.gz
tar zxvf mysql-5.6.11.tar.gz
cd mysql-5.6.11
cmake . -DCMAKE_INSTALL_PREFIX=/data/soft/mysql -DMYSQL_DATADIR=/data/dbdata -DSYSCONFDIR=/data/soft/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_USER=mysql -DWITH_DEBUG=0 -DMYSQL_TCP_PORT=3306 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1 -DWITH_SSL=yes
make
make install

生成配置文件链接

cp /data/mysql/support-files/my-default.cnf /data/mysql/my.cnf
cp /data/mysql/support-files/my-default.cnf /etc/my.cnf

修改$PATH生成文件

修改$PATH生成文件
vim /etc/profile

最后一行加上
PATH=$PATH:/data/soft/mysql/bin
这样MySQL的命令行软件就可以直接使用了

复制mysql的服务shell文件

cp /data/soft/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld

创建MySQL系统数据库

/data/soft/mysql/scripts/mysql_install_db --user=mysql --basedir=/data/soft/mysql --datadir=/data/dbdata

开启MySQL服务

service mysqld start

设置MySQL开机启动

echo "service mysqld start" >> /etc/rc.local

到这里mysql安装完成了, 我们开始使用客户端连接mysql

如果报错:
  SQL Error (1130): Host 'ip' is not allowed to connect to this MySQL server

首先按下面的步骤登录Mysql服务器

登录mysql需要切换到dos下的mysql的bin目录,进行如下操作:

#mysql -uroot -ppassword
mysql>use mysql;

mysql>update user set host = '%'  where user ='root';

mysql>flush privileges;

mysql>select 'host','user' from user where user='root';

mysql>quit
OK。远程连接成功!

阅读全文

CentOS_6.5安装Nginx+PHP+MySQL
发表于 2015-8-31 | 浏览(6733) | 服务器

准备工作

yum -y install automake autoconf libtool make gcc gcc-c++ 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-devel

一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。

可以是任何目录,本文选定的是/data/soft/src

一:Nginx安装

cd /data/soft/src

1.安装PCRE库

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

cd /data/soft/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
tar -zxvf pcre-8.34.tar.gz
cd pcre-8.34
./configure
make
make install

2.安装zlib库

http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

cd /data/soft/src
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install

3.安装ssl(某些vps默认没装ssl)

cd /data/soft/src
wget http://www.openssl.org/source/openssl-1.0.2e.tar.gz
tar -zxvf openssl-1.0.2e.tar.gz
./config
make && make install
cp apps/openssl /usr/bin/

4.安装nginx

cd /data/soft/src
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --sbin-path=/data/soft/nginx/nginx \--conf-path=/data/soft/nginx/nginx.conf \--pid-path=/data/soft/nginx/nginx.pid \--with-http_ssl_module \--with-pcre=/data/soft/src/pcre-8.38 \--with-zlib=/data/soft/src/zlib-1.2.8 \--with-openssl=/data/soft/src/openssl-1.0.2e
make
make install

安装成功后 /data/soft/nginx 目录下如下:
  fastcgi.conf            koi-win             nginx.conf.default
  fastcgi.conf.default    logs                scgi_params
  fastcgi_params          mime.types          scgi_params.default
  fastcgi_params.default  mime.types.default  uwsgi_params
  html                    nginx               uwsgi_params.default
  koi-utf                 nginx.conf          win-utf

5.启动

确保系统的 80 端口没被其他程序占用,运行/data/soft/nginx/nginx 命令来启动 Nginx,

netstat -ano|grep 80
如果查不到结果后执行,有结果则忽略此步骤
sudo /usr/local/nginx/nginx

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

到这里nginx就安装完成了,如果只是处理静态html就不用继续安装了,如果你需要处理php脚本的话,还需要安装php-fpm。

6.Nginx开机启动

echo "/data/soft/nginx/sbin/nginx" >> /etc/rc.local

二:编译安装php-fpm

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.

PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的,具体选项含义可以到这里查看:http://www.php.net/manual/en/configure.about.php

1.php-fpm安装(推荐安装方式)

cd /data/soft/src
wget http://museum.php.net/php5/php-5.4.7.tar.gz
tar zvxf php-5.4.7.tar.gz
cd php-5.4.7
./configure --prefix=/data/soft/php --enable-fpm --with-mcrypt \--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \--with-gd --with-jpeg-dir --enable-pdo --enable-ftp --with-pdo_sqlite --with-openssl=/usr/local/ssl/ --with-pdo_mysql
make && make install

2.以上就完成了php-fpm的安装

下面是对php-fpm运行用户进行设置
cd /data/soft/php
cp etc/php-fpm.conf.default etc/php-fpm.conf

vi etc/php-fpm.conf
修改
  user = www
  group = www

  如果www用户不存在,那么先添加www用户
    groupadd www
    useradd -g www www

3.修改nginx配置文件以支持php-fpm

修改nginx配置文件为,nginx.conf
其中server段增加如下配置:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
  root /data/www; #项目根目录
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

重启nginx服务器
  /data/nginx/nginx -s reload


4.创建测试php文件

在/data/www下创建index.php文件,输入如下内容:
<?php
  echo phpinfo();
?>

5.启动php-fpm服务

/data/soft/php/sbin/php-fpm

5.5杀死php-fpm进程

killall php-fpm(可以配合启动命令实现重启效果)

6.php-fpm关闭与重启

php-fpm 关闭
  kill -INT `cat /data/soft/php/var/run/php-fpm.pid`

php-fpm 重启
  kill -USR2 `cat /data/soft/php/var/run/php-fpm.pid`

7.php-fpm开机启动

echo "/data/soft/php/sbin/php-fpm" >> /etc/rc.local

8.解决php-fpm.pid文件不存在(重启php-fpm)

vim /data/soft/php/etc/php-fpm.conf
  打开 pid = run/php-fpm.pid

三:mysql安装

cd /data/soft/src
wget http://downloads.mysql.com/archives/mysql-5.1/mysql-5.1.70.tar.gz
tar -zxvf mysql-5.1.70.tar.gz
cd mysql-5.1.70

yum install ncurses ncurses-devel
./configure  '--prefix=/data/soft/mysql' '--without-debug' '--with-charset=utf8' '--with-extra-charsets=all' '--enable-assembler' '--with-pthread' '--enable-thread-safe-client' '--with-mysqld-ldflags=-all-static' '--with-client-ldflags=-all-static' '--with-big-tables' '--with-readline' '--with-ssl' '--with-embedded-server' '--enable-local-infile' '--with-plugins=innobase'  make
make install

到此mysql就安装到了/data/soft/mysql路径下,下面开始mysql的配置工作

配置文件

cp support-files/my-medium.cnf /etc/my.cnf

mysql设置开机自启动

cp -r support-files/mysql.server /etc/init.d/mysqld  
/sbin/chkconfig --del mysqld
/sbin/chkconfig --add mysqld

配置权限表

chown -R mysql:mysql /data/soft/mysql
/data/soft/mysql/bin/mysql_install_db --user=mysql

启动mysql

/etc/init.d/mysqld start

mysql初始化配置

export PATH=/data/soft/mysql/bin:$PATH
/data/soft/mysql/bin/mysql_secure_installation
注:这里根据提示设置mysql的root密码

到这里mysql安装完成了, 我们开始使用客户端连接mysql

如果报错:
  SQL Error (1130): Host '192.168.1.100' is not allowed to connect to this MySQL server

首先按下面的步骤登录Mysql服务器

登录mysql需要切换到dos下的mysql的bin目录,进行如下操作:

#mysql -uroot -ppassword
mysql>use mysql;

mysql>update user set host = '%'  where user ='root';

mysql>flush privileges;

mysql>select 'host','user' from user where user='root';

mysql>quit
OK。远程连接成功!

软件包可以到360云盘直接下载:http://yunpan.cn/cfX8FB5UTnfDR (提取码:91e7)

阅读全文

2015最新CentOS安装Nginx+PHP
发表于 2015-8-31 | 浏览(5278) | 服务器

yum安装基本库

yum -y install automake autoconf libtool make gcc gcc-c++ 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

准备

mkdir /data
cd /data
mkdir soft www logs dbdata
cd soft/
mkdir nginx php mysql
cd ../logs
mkdir nginx php mysql

一:Nginx安装

cd /data/soft/src

1.安装PCRE库

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

cd /data/soft/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
tar -zxvf pcre-8.40.tar.gz
cd pcre-8.40
./configure
make && make install

2.安装zlib库

http://zlib.net/zlib-1.2.11.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

cd /data/soft/src
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install

3.安装ssl(某些vps默认没装ssl)

cd /data/soft/src
wget http://www.openssl.org/source/openssl-1.0.2.tar.gz
tar -zxvf openssl-1.0.2.tar.gz
./config
make && make install
cp apps/openssl /usr/bin/
如果提示覆盖,确认就OK了,再查看openssl version版本就是最新版了

4.安装nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /data/soft/nginx 目录下的详细步骤:

cd /data/soft/src
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --prefix=/data/soft/nginx \--sbin-path=/data/soft/nginx/nginx \--conf-path=/data/soft/nginx/nginx.conf \--pid-path=/data/soft/nginx/nginx.pid \--lock-path=/data/soft/nginx/nginx.lock \--error-log-path=/data/logs/nginx/error.log \--http-log-path=/data/logs/nginx/access.log \--http-client-body-temp-path=/data/logs/nginx/client_body_temp \--http-proxy-temp-path=/data/logs/nginx/proxy_temp \--http-fastcgi-temp-path=/data/logs/nginx/fastcgi_temp \--http-uwsgi-temp-path=/data/logs/nginx/uwsgi_temp \--http-scgi-temp-path=/data/logs/nginx/scgi_temp \--user=nginx \--group=nginx \--with-http_ssl_module \--with-pcre=/data/src/pcre-8.40 \--with-zlib=/data/src/zlib-1.2.11 \--with-openssl=/data/src/openssl-1.0.2e
make && make install

cp /data/soft/nginx/nginx /etc/init.d/
/etc/init.d/nginx           启动nginx
/etc/init.d/nginx -s reload     重启nginx
/etc/init.d/nginx -s stop       关闭nginx

添加nginx开机启动

echo "/etc/init.d/nginx" >> /etc/rc.local

二:编译安装php-fpm

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.

PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的,具体选项含义可以到这里查看:http://www.php.net/manual/en/configure.about.php

1.php-fpm安装(推荐安装方式)

cd /data/soft/src
wget http://museum.php.net/php5/php-5.4.7.tar.gz
tar zvxf php-5.4.7.tar.gz
cd php-5.4.7
./configure --prefix=/data/soft/php --enable-fpm --with-mcrypt \--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \--with-gd --with-jpeg-dir --enable-pdo --enable-ftp --with-pdo_sqlite --with-openssl=/usr/local/ssl/ --with-pdo_mysql
make && make install

2.以上就完成了php-fpm的安装、下面是对php-fpm运行用户进行设置

cd /data/soft/php
cp etc/php-fpm.conf.default etc/php-fpm.conf

vi etc/php-fpm.conf 修改
user = www
group = www

如果www用户不存在,那么先添加www用户
  groupadd www
  useradd -g www www

去掉前面的注释
  pid = run/php-fpm.pid

3.修改nginx配置文件以支持php-fpm

修改nginx配置文件为,nginx.conf

其中server段增加如下配置

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  location ~ \.php$ {
  root /data/www; #项目根目录
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

重启nginx服务器
  /data/nginx/nginx -s reload

4.创建测试php文件、在/data/www下创建index.php文件,输入如下内容

<?php
  echo phpinfo();
?>

5.启动php-fpm服务

/data/soft/php/sbin/php-fpm

5.5杀死php-fpm进程

killall php-fpm(可以配合启动命令实现重启效果)

6.php-fpm关闭与重启

php-fpm 关闭
  kill -INT `cat /data/soft/php/var/run/php-fpm.pid`

php-fpm 重启
  kill -USR2 `cat /data/soft/php/var/run/php-fpm.pid`

7.php-fpm开机启动

cp /data/soft/php/sbin/php-fpm /etc/init.d/php-fpm
echo "/etc/init.d/php-fpm" >> /etc/rc.local

阅读全文

ThinkPHP分页驱动 仿百度
发表于 2015-8-31 | 浏览(6022) | PHP

52c6db6d3b1b0.jpg

<?php
 class PagingModel
 {
    private $m_PagingDataArray;  //接收页面提交的post或者get的一维数组条件
    private $m_Configuration;  //配置项数据
    private $m_Fraction;  //每个页面显示的条数
    private $m_Total;  //数据的总条数
    private $m_Page;  //页面传递过来的页码值
    private $m_Starting;  //查询数据的起始值
    private $m_TotalFraction;  //计算出来的总页数
    private $m_Url;  //分页使用的url地址
    private $m_PageCoent;  //是否开启页面数字分页按钮

    /*
        构造方法
    */
    public function __construct($PagingDataArray = array(), $Configuration = array())
    {
        /* 初始化属性数据 */
        $this->m_PagingDataArray = array();
        $this->m_Configuration = array();

        /* 基础数据设置 */
        $this->SetPagingDataArray($PagingDataArray);
        $this->SetConfiguration($Configuration );
        $this->SetBasisData();
    }

    /*
        设置数据
    */
    private function SetPagingDataArray($PagingDataArray)
    {
        /* 判断配置项的数据是否为空 */
        if(false == empty($PagingDataArray)) {
            $this->m_PagingDataArray = $PagingDataArray;
        } else {
            $this->m_PagingDataArray = array();
        }
    }

    /*
        设置配置项数据
    */
    private function SetConfiguration($Configuration)
    {
        /* 判断配置项的数据是否为空 */
        if(false == empty($Configuration)) {
            $this->m_Configuration = $Configuration;
        } else {
            $this->m_Configuration = array();
        }
    }

    /*
        处理判断数组中是否存在某个键名
    */
    private function Setuppase($Property, $Key, $Content)
    {
        /* 判断 $Key 是否在数组中存在的键名 */
        if(true == array_key_exists($Key, $this->m_Configuration)) {
            $this->$Property = $this->m_Configuration["$Key"];
        } else {
            $this->$Property = $Content;
        }
    }

    /*
        基础数据设置
    */
    private function SetBasisData()
    {
        $this->SetFraction();
        $this->SetTotal();
        $this->SetPage();
        $this->SetStarting();
        $this->SetTotalFraction();
        $this->SetUrl();
        $this->SetPageCoent();
    }
    /*
        设置每页显示数据的条数
    */
    private function SetFraction()
    {
        $this->Setuppase('m_Fraction', 'traction', 15);
    }

    /*
        设置数据的总条数
    */
    private function SetTotal()
    {
        $this->Setuppase('m_Total', 'total', 0);
    }

    /*
        设置页面传递过来的页码值
    */
    private function SetPage()
    {
        /* 判断 $Key 是否在数组中存在的键名 */
        if(true == array_key_exists('page', $this->m_PagingDataArray)) {
            $this->m_Page = max(1, (false == empty($this->m_PagingDataArray['page']) ? intval($this->m_PagingDataArray['page']) : 0));
        } else {
            $this->m_Page = 1;
        }
    }

    /*
        设置查询数据的起始值
    */
    private function SetStarting()
    {
        $this->m_Starting = ($this->m_Page - 1) * $this->m_Fraction;
    }

    /*
        设置计算出来的总页数, 总页数 = 总条数除以每页显示的条数。
    */
    private function SetTotalFraction()
    {
        $this->m_TotalFraction = ceil($this->m_Total/$this->m_Fraction);

        /* 当前页数大于最大页数时,将总页数的值赋值给当前页面,防止超越操作。*/
        if($this->m_TotalFraction <= $this->m_Page) {
            $this->m_Page = $this->m_TotalFraction;
        }
    }

    /*
        设置分页的url地址
    */
    private function SetUrl()
    {
        $this->Setuppase('m_Url', 'url', null);
    }

    /*
        设置是否开启显示数字分页按钮
    */
    private function SetPageCoent()
    {
        $this->Setuppase('m_PageCoent', 'pagecoent', 0);
    }

    /*
    获取查询数据的起始值
    */
    public function GetStarting()
    {
        return $this->m_Starting;
    }

    /*
        获取每页显示的条数值
    */
    public function GetFraction()
    {
        return $this->m_Fraction;
    }

    /*
        获取拼接的每页显示的数字页码
    */
    private function GetPageCoent($PageUrl)
    {
        /* 如果page值不等于1的时候 */
        if($this->m_Page != 1) {
            /* 如果分页数值加显示的分页个数值大于当前总页码数的时候 */
            if(($this->m_Page+$this->m_PageCoent) > $this->m_TotalFraction) {
                /* 计算起始值 */
                $Pageis = $this->m_Page-$this->m_PageCoent;
                /* 计算最大数值 */
                $PageMax = $this->m_TotalFraction;

            /* 如果分页数值加显示的分页个数值不大于当前总页码数的时候 */
            } else {
                /* 计算起始值,如果当前page小于等于显示的页数时,就将起始设置为1,防止负数 */
                if($this->m_Page <= $this->m_PageCoent) {
                    $Pageis = 1;
                } else {
                    $Pageis = $this->m_Page-$this->m_PageCoent;
                }

                /* 计算最大数值,当前page数值加需要显示的页码个数值 */
                $PageMax = (($this->m_Page+$this->m_PageCoent));
            }

            /* 如果显示页码值大于等于总页码值时,将起始值设置为1 */
            if($this->m_PageCoent >= $this->m_TotalFraction) {
                $Pageis = 1;
            }

        /* 如果page等于1的时候 */
        } else {
            /* 如果显示页码值大于等于总页码值时,就将总页码值赋值给循环的最大值 */
            if($this->m_PageCoent >= $this->m_TotalFraction) {
                $PageMax = $this->m_TotalFraction;
            } else {
                $PageMax = $this->m_PageCoent+1;
            }
            $Pageis = 1;
        }

        /* 循环拼接需要显示的分页数值个数代码 */
        $PageCoent = '<li>';
        for($Pagei=$Pageis; $Pagei<=$PageMax; $Pagei++) {
            if($this->m_Page == $Pagei) {
                $PageCoent .= '<span class="pageingpagecoent pageingpagecoents">'.$Pagei.'</span>';
            } else {
                $PageCoent .= '<a href="'.$PageUrl.$Pagei.'"><span class="pageingpagecoent">'.$Pagei.'</span></a>';
            }

        }
        /* 返回拼接好的代码 */
        return $PageCoent;
    }

    /*
        获取url拼接,处理URL拼接方法
    */
    private function GetUrlSplice()
    {
        $UrlSplice = '?';
        if(false == empty($this->m_PagingDataArray)) {
            //删除当前数组中的page数据
            unset($this->m_PagingDataArray['page']);
            foreach($this->m_PagingDataArray as $PKey=>$pValue) {
                /* 拼接普通url */
                if((false == empty($pValue)) && (false == is_array($pValue))) {
                    $UrlSplice .= $PKey.'='.$pValue.'&';
                }

                /* 拼接是数组的url */
                /*if((false == empty($pValue)) && (true == is_array($pValue))) {

                }*/
            }
            //print_r($this->m_PagingDataArray);
        }
        return $UrlSplice;
    }

    /*
        返回拼接好的html代码(包括js代码)
    */
    public function GetPagingHtmlInfo()
    {
        $UrlSplice = $this->GetUrlSplice();

        $PageUrl = $this->m_Url.$UrlSplice.'page=';
        $PageUrls = $PageUrl.($this->m_Page-1);
        $PageUrly = $PageUrl.($this->m_Page+1);

        if($this->m_PageCoent > 0) {
            $PageCoent = $this->GetPageCoent($PageUrl);
        } else {
            $PageCoent = null;
        }

        /* 定义分页数据 */
        $Html = '<div id="pageingmodel"><ul id="pageing">';

        $Home = '<li><a href="'.$PageUrl.'1">首页</a></li>';
        $Previous = '<li><a href="'.$PageUrls.'">上一页</a></li>';
        $Next = '<li><a href="'.$PageUrly.'">下一页</a></li>';
        $End = '<li><a href="'.$PageUrl.$this->m_TotalFraction.'">尾页</a></li>';

        $HomeS = '<li class="pageban">首页</li>';
        $PreviousS = '<li class="pageban">上一页</li>';
        $NextS = '<li class="pageban">下一页</li>';
        $EndS = '<li class="pageban">尾页</li>';

        /* 当只有一页数据的时候,就没有拼接url地址 */
        if($this->m_TotalFraction == 1) {
            $Html .= $HomeS.$PreviousS.$PageCoent.$NextS.$EndS;
        /* 当没有数据的时候,就没有拼接url地址 */
        } elseif($this->m_Page == $this->m_TotalFraction && $this->m_Total == 0) {
            $Html .= $HomeS.$PreviousS.$PageCoent.$NextS.$EndS;
        /* 当为第一页的时候 */
        } elseif($this->m_Page == 1) {
            $Html .= $HomeS.$PreviousS.$PageCoent.$Next.$End;
        /* 到尾部的时候 */
        } elseif($this->m_Page == $this->m_TotalFraction  && $this->m_TotalFraction > 1) {
            $Html .= $Home.$Previous.$PageCoent.$NextS.$EndS;
        /* 正常的时候 */
        } else {
                $Html .= $Home.$Previous.$PageCoent.$Next.$End;
        }
        $Html .= '<li>当前第<span class="pagepublic">'.$this->m_Page.'</span>页</li><li>共<span class="pagepublic">'.$this->m_TotalFraction.'</span>页</li><li>总有<span class="pagepublic">'.$this->m_Total.'</span>条数据</li></ul></div>';
        /* css代码 */
        $Css = '<style type="text/css">
            #pageingmodel { width:100%; }
            #pageing li { float:left; margin-right:15px; }
            #pageing li a { color:#0D93BF; }
            #pageing li a:hover { color:#F00; }
            .pageban { color:#999; }
            .pageingpagecoent, .pagepublic { font-weight:bold; }
            .pageingpagecoent { padding:4px 8px; background:#C4C5C5; color:#FFF; margin:0px 5px; -moz-border-radius:4px; -webkit-border-radius:4px; border-radius:4px; font-size:13px; }
            .pageingpagecoents, .pageingpagecoent:hover { background:#099ACF; }

        </style>';

        return $Html.$Css;
    }

 }

 //使用方法
 /* 调用分页模块 */
$Configuration = array(
    'total' => $StudentCount,  //数据总条数
    'traction' => $StudentPage,  //每页显示条数
    'pagecoent' => 3,  //分页显示的个数
    'url' => './StudentManagement',  //翻页的url地址
);
       //$_REQUEST : 如果当前查询有其它条件将会自动选择拼接起来
      //$Configuration : 配置项
$PageingObj = new PagingModel($_REQUEST, $Configuration);
$this->assign('pageing', $PageingObj->GetPagingHtmlInfo());

阅读全文

TOP