部分人感觉用yum 安装的 rpm 包程序放到线上跑服务的时候会有些不稳定,或者一些原因,我也没有多少经验之谈,但是我相信如果我能把rpm管理和维护好,不让我的网站出问题,那么rpm他就是可靠的,也希望更多人能给我更好的解决办法......
现在我就利用还有40分钟的下班时间来写这篇文章;
环境配置:
centos6.4系统(目前最新版)
主(lxw02) 从(lxw03)
192.168.1.194 192.168.1.195
在(主192.168.1.194和从192.168.1.195)上做同样的操作
# vi cyzhang
# chmod +x cyzhang
脚本开始======
#!/bin/bash
iptables -F
service iptables stop
chkconfig iptables off  
# Add epel yum source 
url="http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm"
rpm -ivh $url
# Close selinux mode
sed -i -e "s/SELINUX=enforcing/SELINUX=disable/g" /etc/selinux/config
# Install basick tool and software, Update system
yum -y update  
yum -y install vim wget make gcc automake yum-utils unzip openssh-clients
yum -y install mysql mysql-server mysql-devel
mkdir -p /opt/mysql_data 
chown -R mysql.mysql /opt/mysql_data
chmod 766 /opt/mysql_data
======脚本结束
编辑mysql主配置文件(主192.168.1.194和从192.168.1.195上都按照下面编辑)
# cat /etc/my.cnf 
[mysqld]
character-set-server = utf8
datadir = /opt/mysql_data
socket = /var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
================================================================================================
解释:
datadir=/opt/mysql_data 把默认的路径修改成这样
socket = /var/lib/mysql/mysql.sock 这路径我不建议再更改,如果更改后登陆数据库的时候还需要指定一下mysql.sock文件路径“mysql -uroot -p -S /tmp/mysql.sock”
================================================================================================

修改保存主配置文件后启动mysqld服务
# service mysqld start && chkconfig mysqld on        ************如果这个地方不成功的话,要检查ipstables和selinux及主配置文件是否正确
创建同步数据库时用的用户:
# useradd cyzhang
# passwd cyzhang    此处密码我设置的是123.com
登陆数据库:
# mysql -uroot -p 回车
mysql> grant replication slave,reload,super on *.* to cyzhang@192.168.1.195 identified by '123.com'; 

Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to cyzhang@192.168.1.195 identified by '123.com';            给cyzhang用户赋予权限;

Query OK, 0 rows affected (0.00 sec)

mysql> create database portal;

Query OK, 1 row affected (0.00 sec)

mysql> show databases; select user,host from mysql.user;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| portal             |
| test               |
+--------------------+
4 rows in set (0.00 sec)


+---------+---------------+
| user    | host          |
+---------+---------------+
| root    | 127.0.0.1     |
| cyzhang | 192.168.1.194 |
| cyzhang | 192.168.1.195 |
|         | localhost     |
| root    | localhost     |
|         | lxw02         |
| root    | lxw02         |
+---------+---------------+
7 rows in set (0.00 sec)

在slave节点上尝试连接master,测试网络和用户的可用性:
[root@lxw03 ~]# mysql -ucyzhang -p  -h 192.168.1.194 -e "select user,host from mysql.user;show databases;"
Enter password: 此处输入密码‘123.com’
+---------+---------------+
| user    | host          |
+---------+---------------+
| root    | 127.0.0.1     |
| cyzhang | 192.168.1.194 |
| cyzhang | 192.168.1.195 |
|         | localhost     |
| root    | localhost     |
|         | lxw02         |
| root    | lxw02         |
+---------+---------------+
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| portal             |
| test               |
+--------------------+
在要同步的数据库中创建表和模拟数据(在master)

mysql> create table `caitable` (`id` int(5) NOT NULL auto_increment, `name` varchar(32) NOT NULL, `password` char(32) NOT NULL, PRIMARY KEY (`id`));

往里面插入一些数据,编写一个小脚本
# vim insert
#!/bin/bash
for ((i=1; i<=10; i++))
do
mysql -uroot -e "insert into caidb.caitable(id,name,password) value ('$i','user$i',md5('usercai$i'));"
done
mysql -uroot -e "select * from caidb.caitable;"
保存退出!
# chmod +x insert
[root@lxw02 ~]#./insert
+----+--------+----------------------------------+
| id | name   | password                         |
+----+--------+----------------------------------+
|  1 | user1  | 848bbbf06aeba9495a47578569cee381 |
|  2 | user2  | 4b1700e322bf3619376ef6df33d4a68d |
|  3 | user3  | 6238fe4a6ae91fb4e4c364dab46f3570 |
|  4 | user4  | e695fa61724b69ef58b3ff38d6d26322 |
|  5 | user5  | f0bab2bf65014192d9bc6e139ba5d520 |
|  6 | user6  | be56605958ccaabfa9eeb6d5d33dd075 |
|  7 | user7  | fec2992b831f761e61c1f406f1c0e7ec |
|  8 | user8  | 4ab3f2d0a35ca849e86ddc9a7856500a |
|  9 | user9  | 77998b564bf7cd7db0df7b57538e93f8 |
| 10 | user10 | d7d61d43582dbae4395dc298bc8c6d80 |
+----+--------+----------------------------------+
主数据库(192.168.1.194)中的portal 备份一份出来:
# mysqldump -uroot portal > portal.sql
# scp portal.sql root@192.168.1.195:~            ***提示输入密码输入登陆192.168.1.195的系统用户密码
从数据库(192.168.1.195)中把刚从主复制过来的数据库导入进去:
# mysql -uroot
mysql> create database portal;
Query OK, 1 row affected (0.00 sec)

mysql> exit
Bye
# mysql -uroot portal < portal.sql
mysql -uroot -e "select * from portal.caitable;"
+----+--------+----------------------------------+
| id | name | password |
+----+--------+----------------------------------+
| 1 | user1 | 848bbbf06aeba9495a47578569cee381 |
| 2 | user2 | 4b1700e322bf3619376ef6df33d4a68d |
| 3 | user3 | 6238fe4a6ae91fb4e4c364dab46f3570 |
| 4 | user4 | e695fa61724b69ef58b3ff38d6d26322 |
| 5 | user5 | f0bab2bf65014192d9bc6e139ba5d520 |
| 6 | user6 | be56605958ccaabfa9eeb6d5d33dd075 |
| 7 | user7 | fec2992b831f761e61c1f406f1c0e7ec |
| 8 | user8 | 4ab3f2d0a35ca849e86ddc9a7856500a |
| 9 | user9 | 77998b564bf7cd7db0df7b57538e93f8 |
| 10 | user10 | d7d61d43582dbae4395dc298bc8c6d80 |
+----+--------+----------------------------------+
在主数据库(192.168.1.194)的/etc/my.conf中[mysqld]下添加
[root@lxw02 ~]# cat /etc/my.cnf 
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = portal
binlog_ignore_db = mysql
character-set-server = utf8
datadir = /opt/mysql_data
socket = /var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
============================================================================
解释:
log_bin = /var/log/mysql/mysql-bin.log    这个路径在系统中没有
需要自己创建:
# mkdir /var/log/mysql
# chown -R mysql:mysql /var/log/mysql
=============================================================================
重新启动主数据库
# service mysqld restart 

在从数据库(192.168.1.195)的/etc/my.conf中的[mysqld]部分添加
[root@lxw03 ~]# cat /etc/my.cnf 
[mysqld]
server-id = 2
master-host = 192.168.1.194
master-user = cyzhang
master-password = 123.com
master-port = 3306
master-connect-retry = 60           ***意思是当主的60秒联系不上时主动存储数据
replicate-do-db = portal                ***主要复制的数据库
replicate-ignore-db = mysql            ***不复制的数据库
character-set-server=utf8
datadir=/opt/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
===================================
重新启动从数据库的服务
# service mysqld restart 

到主服务器中查看master状态:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 106 | portal | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记住file字段的binlog的名字
在从数据库中进行一次手动同步:
mysql> change master to master_host='192.168.1.194',master_user='cyznag',master_password='123.com',master_log_file='mysql-bin.000001',master_log_pos=106;

Query OK, 0 rows affected (0.03 sec)

mysql> slave start; 

Query OK, 0 rows affected (0.00 sec)

在从数据库服务器上查看从数据库状态;

# mysql -uroot -e "show slave status"

Slave_IO_Running Yes

Slave_SQL_Running Yes

========================================================================================
验证是否成功:
查看现在表里都有的数据:
mysql> select * from portal;
+----+--------+----------------------------------+
| id | name | password |
+----+--------+----------------------------------+
| 1 | user1 | 848bbbf06aeba9495a47578569cee381 |
| 2 | user2 | 4b1700e322bf3619376ef6df33d4a68d |
| 3 | user3 | 6238fe4a6ae91fb4e4c364dab46f3570 |
| 4 | user4 | e695fa61724b69ef58b3ff38d6d26322 |
| 5 | user5 | f0bab2bf65014192d9bc6e139ba5d520 |
| 6 | user6 | be56605958ccaabfa9eeb6d5d33dd075 |
| 7 | user7 | fec2992b831f761e61c1f406f1c0e7ec |
| 8 | user8 | 4ab3f2d0a35ca849e86ddc9a7856500a |
| 9 | user9 | 77998b564bf7cd7db0df7b57538e93f8 |
| 10 | user10 | d7d61d43582dbae4395dc298bc8c6d80 |
+----+--------+----------------------------------+
10 rows in set (0.00 sec)

Empty set (0.00 sec)

mysql> insert into portal.caitable(id,name,password) value ('17','user14',md5('usercai14'));
Query OK, 1 row affected (0.00 sec)

mysql> insert into portal.caitable(id,name,password) value ('12','user14',md5('usercai14'));
Query OK, 1 row affected (0.00 sec)

mysql> insert into portal.caitable(id,name,password) value ('11','user14',md5('usercai14'));
Query OK, 1 row affected (0.00 sec)

mysql> create table `caitest` (`id` int(5) NOT NULL auto_increment, `name` varchar(32) NOT NULL, `password` char(32) NOT NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.02 sec)


然后去slave上查看
# mysql -uroot -e "select * from caidb.caitable; use caidb; show tables"
+----+--------+----------------------------------+
| id | name | password |
+----+--------+----------------------------------+
| 1 | user1 | 848bbbf06aeba9495a47578569cee381 |
| 2 | user2 | 4b1700e322bf3619376ef6df33d4a68d |
| 3 | user3 | 6238fe4a6ae91fb4e4c364dab46f3570 |
| 4 | user4 | e695fa61724b69ef58b3ff38d6d26322 |
| 5 | user5 | f0bab2bf65014192d9bc6e139ba5d520 |
| 6 | user6 | be56605958ccaabfa9eeb6d5d33dd075 |
| 7 | user7 | fec2992b831f761e61c1f406f1c0e7ec |
| 8 | user8 | 4ab3f2d0a35ca849e86ddc9a7856500a |
| 9 | user9 | 77998b564bf7cd7db0df7b57538e93f8 |
| 10 | user10 | d7d61d43582dbae4395dc298bc8c6d80 |
| 12 | user14 | 335007a617140b0431d5c8dd51830e68 |
| 11 | user14 | 335007a617140b0431d5c8dd51830e68 |
| 17 | user14 | 335007a617140b0431d5c8dd51830e68 |
| 14 | user14 | 335007a617140b0431d5c8dd51830e68 |
+----+--------+----------------------------------+
+-----------------+
| Tables_in_caidb |
+-----------------+
| caitable |
| caitest |
+-----------------+

到这里主从复制就部署好了!