Mysql主主
Ip分别为10.10.9.241和10.10.9.242
每个节点既是主也是从
安装前的准备:
# yum install gcc gcc-c++ ncurses-devel bison -y
# tar zxf /root/cmake-2.8.4.tar.gz -C /usr/local/src
# tar zxf /root/mysql-5.5.25.tar.gz -C /usr/local/src
# cd /usr/local/src/cmake-2.8.4/
# ./configure //检查安装环境,指定安装选项
# gmake
# make install
创建用户
# useradd -s /sbin/nologin mysql
创建保存数据的目录:
# mkdir /data/mysql -pv
# mkdir /data/mysql/binlog/ -pv
# chown mysql.mysql /data/mysql
# cd /usr/local/src/mysql-5.5.25
# cmake ./
# make && make install
复制启动脚本
# cp /usr/local/src/mysql-5.5.25/support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld
复制配置文件
# cp /etc/my.cnf /etc/my.cnf.bak
# cp /usr/local/src/mysql-5.5.25/support-files/my-medium.cnf /etc/my.cnf
# vim /etc/my.cnf
在[mysqld]下面加入以下内容
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql/
log-error = /data/mysql/mysql_error.log
pid-file = /data/mysql/mysql.pid
初始化数据库:
# cd /usr/local/src/mysql-5.5.25/scripts
# chmod +x mysql_install_db
# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/
#/etc/init.d/mysqld start
# echo "export PATH=$PATH:/usr/local/mysql/bin " >>/etc/profile
//将mysql的命令所在路径添加到path变量中
# source /etc/profile
# ln -s /usr/local/mysql/bin/mysql /bin/
# chkconfig --add mysqld
# chkconfig mysqld on (#chkconfig --level 35 mysql on)
10.10.9.241配置:
#vim /etc/my.cnf (根据当时情况变通)
server-id = 1 //这个要有了就不用加了
log-bin=/data/mysql/binlog/mysql-binlog //这个要有了也可以不改变,log-bin:表示开启二进制日志
binlog_format=mixed ////binlog日志格式,mysql默认采用statement,建议使用mixed
skip-name-resolve //grant时,必须使用ip不能使用主机名,快速登录跳过用户解析
skip-slave-start //加上这个进程,slave复制进程就不随mysql启动而启动
relay_log=/data/mysql/log/binlog/mysql-realy-binlog //中继日志
replicate-wild-ignore-table=mysql.% //复制时忽略数据库及表,尽量用这种方式过滤掉不想复制的库及表
mysql>grant replication slave on *.* to [email protected] identified by 'tongbu1';
mysql> show master status;
mysql>flush privileges;
mysql 新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表,否则会出现拒绝访问,还有一种方法,就是重新启动mysql服务器,来使新设置生效。
***** mysql> flush tables with read lock; //给所有的表加上读锁,为了保证数据的一致性
*****mysql> show variables like 'server_id'//查看server_id
*****mysql> set global server_id=2 //此处数值和my.cnf里设置的一样就行
***** # mysqladmin -uroot password '123' //给root设置密码
****** # mysqladmin -uroot -p123 password '456' //给root重新设置密码
***mysql在启动的时候会查找/etc/my.cnf,DATADIR/my.cnf,USER_HOME/my.cnf
10.10.9.242配置:
# vim /etc/my.cnf
server-id = 2 //把这个变成2,和主库不一样就好
skip-slave-start //加上这个进程,slave复制进程就不随mysql启动而启动
relay_log=/data/mysql/log/binlog/mysql-realy-binlog //中继日志
log-bin=/data/mysql/binlog/mysql-binlog //这个要有了也可以不改变,log-bin:表示开启二进制日志
binlog_format=mixed ////binlog日志格式,mysql默认采用statement,建议使用mixed
skip-name-resolve //grant时,必须使用ip不能使用主机名,快速登录跳过用户解析
replicate-wild-ignore-table=mysql.% //复制时忽略数据库及表,尽量用这种方式过滤掉不想复制的库及表
mysql> change master to master_host="10.10.9.241",master_port=3306,master_user="tongbu1",master_password="tongbu1",,master_log_file="mysql-bin.000003",master_log_pos=255;
mysql>slave start; //开启从服务器
mysql> show slave status\G; //确保这两行是Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
因为是主主,每个节点既是主也是从接下来执行:
10.10.9.242配置
mysql>grant replication slave on *.* to [email protected] identified by 'tongbu2';
mysql>flush privileges;
mysql> show master status;
10.10.9.241配置
mysql> change master to master_host="10.10.9.242",master_port=3306,master_user="tongbu2",master_password="tongbu2",,master_log_file="mysql-bin.000003",master_log_pos=255;
mysql>slave start; //开启从服务器
mysql> show slave status\G; //确保这两行是Yes
Slave_IO_Running: Yes
Slave_SQL_Running: Yes