环境:
系统版本:centos6.6
mysql版本:mysql 5.6.11
ip | 状态 |
10.0.2.87 | 主 |
10.0.2.111 | 从 |
1. 数据备份
在10.0.2.87上
先停掉服务,使数据库停止数据更新,然后备份数据库
mysqldump -R -uroot -p nc_onldb > /root/nc_onldb_`date +%Y%m%d`.sql mysqldump -R -uroot -p rxrc > /root/rxrc_`date +%Y%m%d`.sql
如果数据较大,可以考虑直接拷贝库文件
2. 安装mysql 5.6
从库mysql版本与主库不一致,重新安装。
首先停掉原有的mysql:
/etc/init.d/mysqld stop
1. 从10.0.2.87拷贝mysql安装包
cd /usr/local/src/ scp root@10.0.2.87:/home/nc_onldb/mysql-5.6.11.tar.gz ./
2. 解压
tar xzf mysql-5.6.11.tar.gz cd mysql-5.6.11
3. 安装cmake
yum install cmake
4. 安装mysql
groupadd db useradd nc_onldb -g db
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/var/run/mysqld/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/home/nc_onldb/DATA -DMYSQL_USER=nc_onldb -DMYSQL_TCP_PORT=3306
可能会出现的错误:
1. -- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)
CMake Error at cmake/readline.cmake:82 (MESSAGE):
Curses library not found. Please install appropriate package,
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
====安装 ncurses-devel,然后删除CMakeCache.txt,重新编译即可
yum install ncurses-devel rm -f CMakeCache.txt
2. CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: Internal CMake error, TryCompile configure of cmake failed
-- Performing Test HAVE_PEERCRED - Failed
====缺少gcc相关的包,然后删除CMakeCache.txt,重新编译
yum install gcc gcc-c++ rm -f CMakeCache.txt
make && make install
5. 初始化数据库
/usr/local/mysql/scripts/mysql_install_db --user=nc_onldb --basedir=/usr/local/mysql --datadir=/home/nc_onldb/DATA --pid-file=/var/run/mysqld/mysqld.pid
初始化时可能会出现错误:
/usr/local/mysql/bin/mysqld: File ‘./mysql-bin.index‘ not found (Errcode: 13 - Permission denied)
2016-10-19 15:06:48 30553 [ERROR] Aborting
2016-10-19 15:06:48 30553 [Note] Binlog end
2016-10-19 15:06:48 30553 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
====先将原有的mysql服务停掉再进行初始化。
6. 复制服务启动脚本
由于有老版本的mysql启动脚本,复制到/etc/init.d时需注意区分
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
7. 修改配置文件
mv /etc/my.cnf /etc/my.cnf.bak vi /usr/local/mysql/my.cnf [mysqld] sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES datadir=/home/nc_onldb/DATA socket=/var/run/mysqld/mysql.sock user=nc_onldb # 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
启动mysql
/etc/init.d/mysql start
启动过程可能报错:
Starting MySQL.. ERROR! The server quit without updating PID file (/home/nc_onldb/DATA/xxx.pid).
====原因可能是配置的pid目录权限问题,此目录跟原有mysql的pid目录一样,但权限是原有mysql的用户,将/var/run/mysqld的所属用户和所属组改成nc_onldb
chown -R nc_onldb:nc_onldb /var/run/mysqld
3. 设置mysql主从
1. 配置主(10.0.2.87)
vi /usr/local/mysql/my.cnf 添加如下内容: server-id=87 log-bin=mysql-bin
重启mysql
/etc/init.d/mysql restart
设置权限
/usr/local/mysql/bin/mysql -uroot >grant replication slave on *.* to ‘repl‘@‘10.0.2.111‘ identified by ‘aabbcc‘; >flush privileges; >flush tables with read lock; #数据库加读取锁 >show master status; #查看主库状态,记住内容,配置从时会用到
2. 配置从(10.0.2.111)
vi /usr/local/mysql/my.cnf 添加如下内容: server-id=111
重启mysql
/etc/init.d/mysql restart
/usr/local/mysql/bin/mysql -uroot >stop slave; >change master to master_host=‘10.0.2.87‘, master_port=3306, master_user=‘repl‘, master_password=‘aabbcc‘, master_log_file=‘mysql-bin.xxxx‘, master_log_pos=xxxx; >start slave;
在10.0.2.87上:
unlock tables;
再到从上看mysql的状态
show slave status\G (\G表示以列的形式显示)
更新主从关系(bin-log发生变化):
在从上:
>stop slave; >reset slave; >start slave;
删除主从关系:
在从上:
>stop slave; >reset slave all;
4. mysql清理bin-log
(1)自动清理方法:编辑my.cnf
expire_logs_days = 7 // 表示日志保留7天,超过7天则设置为过期的
#/usr/local/mysql/bin/mysql -uroot >show binary logs; >show variables like ‘%log%‘; >set global expire_logs_days = 7;
(2)手动清理
如果没有主从,可以通过下面命令重置数据库日志:
>reset master;
如果有主从复制,应通过purge命令来清理:
/usr/local/mysql/bin/mysql -uroot >purge master logs to ‘mysql-bin.010‘; //清除mysql-bin.010日志以前的(不包括010) >purge master logs before ‘2016-02-28 13:00:00‘; //清除2016-02-28 13:00:00前的日志 >purge master logs before date_sub(now(), interval 3 day); //清除3天前的bin日志
注意,不要轻易手动去删除binlog,会导致binlog.index和真实存在的binlog不匹配,而导致expire_logs_day失效。
本文出自 “我不是我” 博客,请务必保留此出处http://wangwq.blog.51cto.com/8711737/1956819
原文地址:http://wangwq.blog.51cto.com/8711737/1956819