一、mysql数据库的安装
分别在master 和slave上源码安装mysql数据库
1.1 安装相关包
1.1.1 cmake软件
cd /home/oldboy/tools/
tar xf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
#CMake has bootstrapped. Now run gmake.
gmake
gmake install
cd ../
1.1.2 依赖包
yum install ncurses-devel -y
1.2 开始安装mysql
1.2.1 创建用户和组
groupadd mysql
useradd mysql -s /sbin/nologin -M -g mysql
1.2.2 解压编译MySQL
tar zxf mysql-5.5.32.tar.gz
cd mysql-5.5.32
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql/mysql-5.5.40 \
-DMYSQL_DATADIR=/application/mysql/mysql-5.5.40/data \
-DMYSQL_UNIX_ADDR=/application/mysql/mysql-5.5.40/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_READLINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
#-- Build files have been written to: /home/oldboy/tools/mysql-5.5.32
提示,编译时可配置的选项很多,具体可参考结尾附录或官方文档:
make
#[100%] Built target my_safe_process
make install
ln -s /application/mysql-5.5.32/ /application/mysql
如果上述操作未出现错误,则MySQL5.5.32软件cmake方式的安装就算成功了。
1.3安装数据库成功后
1.3.1 修改mysql安装目录的权限,全部修改为mysql
chown -R mysql:mysql /application/mysql
1.3.2 初始化数据库
cd /application/mysql/scripts
[[email protected] scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/mysql --user=mysql
1.3.3 替换/etc/下的配置文件和数据库的启动文件
/application/mysql/support-files
cp -r my-small.cnf /etc/my.cnf
cp -r mysql.server /etc/init.d/mysql
1.4启动数据库
/etc/init.d/mysql start
/etc/init.d/mysql stop
/etc/init.d/mysql restart
二、mysql数据库分库分表的同步
2.1master配置文件
log-bin=mysql-bin
binlog_format=row
server-id = 15
binlog-do-db=sales
/etc/init.d/mysql restart(重启数据库)
2.2slave配置文件
server-id = 25
relay-log=relay-bin
read-only = 1
replicate-do-db = sales
replicate-do-db =user_info
replicate-ignore-db = information_schema
replicate-ignore-db = mysql
replicate-ignore-db = user_info
replicate-wild-do-table =sales.story // 所要同步的数据库的单个表
/etc/init.d/mysql restart(重启数据库)
2.3在master上创建同步的用户
grant replication client,replication slave on *.* to [email protected]‘172.27.1.%‘ identified by ‘password‘;
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000003
Position: 150
Binlog_Do_DB: sales,user_info
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
2.4在slave上执行如下语句,同步到master
change master to master_host=‘172.27.1.12‘(主库IP地址),master_user=‘rep‘,master_password=‘financial‘,master_log_file=‘mysql-bin.000003‘,master_log_pos=150;
start slave(启动从库)
stop slave(停止从库)
reset slave(重置从库)
启动数据库后查看slave的状态如下:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.27.1.12
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 150
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 269
Relay_Master_Log_File: mysql-bin.001603
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: sales
Replicate_Ignore_DB: information_schema,mysql,user_info
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: sales.story
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 150
Relay_Log_Space: 419
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: 15
1 row in set (0.00 sec)
ERROR:
No query specified
上述表示数据库master and slave 已配置好,可以进行同库单个表的配置
测试:
1.分别在master和slave创建数据库sales(row模式下需要手动创建数据库)。
2.use sales库下,在master上创建表
CREATE TABLE user_info (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
3.在master上创建表
CREATE TABLE story (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
查看slave同步状态:
a. story表同步成功
b. user_info表同步不成功
则mysql数据库单库,单表同步成功。
如果只是单库同步,则在slave的配置文件中去掉:
replicate-wild-do-table =sales.story // 所要同步的数据库的单个表
在单表同步中当出现(高版本5.6.2及以上)master----(低版本5.6.2以下)slave
启动slave后,会报如下的错误:
[ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: ‘Slave can not handle replication events with the checksum that master is configured to log;
后查询资料后发现:在5.6.2中,新增了一个参数binlog_checksum,而且在5.6.6及以后的版本中,该参数的值由NONE改为了CRC32,master为5.6.30的情况下,自然有该选项参数(为了验证binlog events的正确,指定通过CRC32算法来避免出现events corrupt,所以这个选项参数出现了。),并且其值为CRC32,但slave为5.5.32,此时的该选项参数是不存在的。
【解决方案】:
直接在master上执行,即可:
- mysql> SET GLOBAL binlog_checksum=none;
当然也要加到配置文件的[mysqld]中,避免重启失效:
- [mysqld]
- binlog-checksum = none
注意:在该参数值还未修改之前的binlog,似乎也是无法被io thread所接收的,故之前的binlog可能需要手动执行来补数据了。
在修改之后的binlog中的events,则可以被正确的传输到slave上。