MySQL5.6.10Innodb引擎数据库升级方法
数据库环境:
MySQL5.1.50(MyISAM、master)192.168.1.16主库
MySQL5.1.46(MyISAM、master)192.168.1.13主库
MySQL5.1.50(MyISAM、slave)192.168.1.1413从库
目标:升级192.168.1.16数据库,更换数据库引擎,完成后如下:
MySQL5.6.10(InnoDB、master)192.168.1.16主库
MySQL5.1.46(MyISAM、master)192.168.1.13主库
MySQL5.1.50(MyISAM、slave)192.168.1.1413从库
1.把1.16数据库从使用中分离出来(包括数据备份,原MySQL卸载等,省略)
2.编译安装MySQL5.6.10(1.16上面操作)
2.1检查所需软件包,上传MySQL安装程序
rpm-qagccgcc-c++makencurses-devel
软件安装程序存放目录:/usr/local/software
cmake-2.8.5.tar.gzmysql-5.6.10.tar.gz
2.2安装cmake
cd/usr/local/software
tarzxvfcmake-2.8.5.tar.gz
cdcmake-2.8.5
./configure
make
makeinstall
2.3安装Mysql5.6.10
cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.10\
-DMYSQL_DATADIR=/usr/local/mysql-5.6.10/data\
-DWITH_MYISAM_STORAGE_ENGINE=1\
-DWITH_INNOBASE_STORAGE_ENGINE=1\
-DWITH_ARCHIVE_STORAGE_ENGINE=1\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1\
-DENABLED_LOCAL_INFILE=1\
-DDEFAULT_CHARSET=utf8\
-DDEFAULT_COLLATION=utf8_general_ci
make
makeinstall
2.4配置MySQL(由于之前这台机器已经创建好数据库账户,此步省略)
groupadd-g3306mysql
useradd-u3306-gmysql-M-s/sbin/nologinmysql
2.5初始化数据库
chown-Rmysql:mysql/usr/local/mysql-5.6.10/data
chmod-R755/usr/local/mysql-5.6.10/data
/usr/local/mysql-5.6.10/scripts/mysql_install_db--user=mysql--basedir=/usr/local/mysql-5.6.10--datadir=/usr/local/mysql-5.6.10/data--socket=/usr/local/mysql-5.6.10/data/mysql.sock--pid-file=/usr/local/mysql-5.6.10/data/hk16.pid
2.6配置MySQL服务,启动MySQL
rm-rf/etc/my.cnf\\删除原有my.cnf配置文件
vim/etc/my.cnf\\直接编辑新的my.cnf
my.cnf配置文件内容如下:
[mysqld]
character-set-server=utf8
wait_timeout=31536000
max_connections=3000
max_connect_errors=10000
max_allowed_packet=32M
skip-name-resolve
lower_case_table_names=1
thread_cache=128
table_open_cache=1024
query_cache_type=1
query_cache_size=128M
join_buffer_size=128M
sort_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=2M
open_files_limit=30720
max_heap_table_size=128M
tmp_table_size=128M
tmpdir=/dev/shm
binlog_cache_size=12M
max_binlog_size=1G
expire_logs_days=7
binlog_checksum=none
sync_binlog=0
innodb_buffer_pool_size=15G
innodb_use_sys_malloc=1
innodb_log_file_size=256M
innodb_log_buffer_size=16M
innodb_flush_log_at_trx_commit=2
innodb_flush_method=O_DIRECT
innodb_lock_wait_timeout=50
innodb_read_io_threads=8
innodb_write_io_threads=8
innodb_thread_concurrency=0
innodb_rollback_segments=128
innodb_change_buffer_max_size=50
log-bin=hk16-bin
explicit_defaults_for_timestamp
basedir=/usr/local/mysql-5.6.10
datadir=/usr/local/mysql-5.6.10/data
port=3306
server-id=100
socket=/usr/local/mysql-5.6.10/data/mysql.sock
[safe_mysqld]
log-error=/usr/local/mysql-5.6.10/data/hk16.err
pid-file=/usr/local/mysql-5.6.10/data/hk16.pid
user=mysql
[mysql]
socket=/usr/local/mysql-5.6.10/data/mysql.sock
default-character-set=utf8
auto-rehash
[mysqldump]
quick
max_allowed_packet=512M
[myisamchk]
key_buffer_size=512M
sort_buffer_size=512M
read_buffer=80M
write_buffer=80M
[mysqlhotcopy]
interactive-timeout
cp/usr/local/mysql-5.6.10/support-files/mysql.server/etc/init.d/mysqld
chmod+x/etc/init.d/mysqld
chkconfig--addmysqld
chkconfigmysqldon
servicemysqldrestart
netstat-tnlp|grep3306\\查看监听端口
2.7配置环境变量,设置root密码
vim/etc/profile
MYSQL_HOME=/usr/local/mysql-5.6.10
PATH=$PATH:$MYSQL_HOME/bin
exportPATHMYSQL_HOME
source/etc/profile
/usr/local/mysql-5.6.10/bin/mysqladmin-urootpassword'xxxx’
3.配置数据库复制
3.1导出数据库表结构(1.13数据库上面操作)
mysqldump–uroot–pxxxx–danytracking4_1>anytracking4_1.sql
mysqldump–uroot–pxxxx–danytracking4_2>anytracking4_2.sql
mysqldump–uroot–pxxxx–danytracking4_3>anytracking4_3.sql
mysqldump–uroot–pxxxx–danytracking4_4>anytracking4_4.sql
3.2创建数据库,拷贝表结构,更改数据库引擎(1.16数据库上面操作)
mysql–uroot-pxxx
createdatabaseanytracking4_1;
createdatabaseanytracking4_2;
createdatabaseanytracking4_3;
createdatabaseanytracking4_4;
exit;
cd/usr/local/backup
scp[email protected]:/usr/local/backup/db/*.
vimanytracking4_1.sql替换引擎:%s/MyISAM/InnoDB/g
vimanytracking4_2.sql替换引擎:%s/MyISAM/InnoDB/g
vimanytracking4_3.sql替换引擎:%s/MyISAM/InnoDB/g
vimanytracking4_4.sql替换引擎:%s/MyISAM/InnoDB/g
3.3导入表结构,修改my.cnf配置(192.168.1.16上面操作)
cd/usr/local/backup
mysql–uroot–pxxxanytracking4_1
mysql–uroot–pxxxanytracking4_2
mysql–uroot–pxxxanytracking4_3
mysql–uroot–pxxxanytracking4_4
vim/etc/my.cnf\\只同步特定的数据库,在[mysqld]下添加
binlog-ignore-db=mysql
replicate-ignore-db=test
replicate-ignore-db=mysql
replicate-ignore-db=information-schema
replicate-ignore-db=performance_schema
replicate-do-db=anytracking4_1
replicate-do-db=anytracking4_2
replicate-do-db=anytracking4_3
replicate-do-db=anytracking4_4
保存,重启数据库servicemysqldrestart
4.配置数据库主主同步
4.1清空1.16上面的binlog日志,停止1.14从复制,防止数据丢失!
mysql–uroot–pxxxx(1.16上面操作)
RESETMASTER;
mysql–uroot–pxxxx(1.14上面操作)
stopslave;
4.2配置1.16从复制
flushtableswithreadlock;(1.13上面操作,全局锁表)
showmasterstatus;\\查询masterlog_file以及Postition,如下所示
mysql>showmasterstatus;
+------------------+-----------+--------------+------------------+
|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|
+------------------+-----------+--------------+------------------+
|mysql-bin.003581|341699908||mysql|
+------------------+-----------+--------------+------------------+
1rowinset(0.00sec)
mysql–uroot–pxxx(1.16上面操作,指定主库)
changemastertomaster_host='192.168.1.13',master_user='root',master_password='xxx',master_log_file='mysql-bin.003581',master_log_pos=341699908;
startslave;\\启动从复制线程
showslavestatus\G\\查看主从复制状态,如有错误,需看具体错误代码内容
mysql>showslavestatus\G
***************************1.row***************************
Slave_IO_State:Waitingformastertosendevent
Master_Host:192.168.1.13
Master_User:root
Master_Port:3306
Connect_Retry:60
Master_Log_File:mysql-bin.003581
Read_Master_Log_Pos:369453801
Relay_Log_File:HK16-relay-bin.000016
Relay_Log_Pos:265
Relay_Master_Log_File:mysql-bin.003581
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
Replicate_Do_DB:anytracking4_1,anytracking4_2,anytracking4_3,anytracking4_4
Replicate_Ignore_DB:test,mysql,information-schema,performance_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:0
Last_Error:
Skip_Counter:0
Exec_Master_Log_Pos:369453801
Relay_Log_Space:476
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:12
Master_UUID:
Master_Info_File:/usr/local/mysql-5.6.10/data/master.info
SQL_Delay:0
SQL_Remaining_Delay:NULL
Slave_SQL_Running_State:Slavehasreadallrelaylog;waitingfortheslaveI/Othreadtoupdateit
Master_Retry_Count:86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:0
1rowinset(0.00sec)
unlocktables;(1.13上面操作,解锁)
这样就完成了1.13与1.16不同版本、不同引擎数据库主从复制!!
4.3配置1.13主复制
mysql–uroot–pxxxx(1.16上面操作)
showmasterstatus;\\查询masterlog_file以及Postition,如下所示
mysql>showmasterstatus;
+-----------------+-----------+--------------+------------------+-------------------+
|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|
+-----------------+-----------+--------------+------------------+-------------------+
|hk16-bin.000003|818885460||mysql||
+-----------------+-----------+--------------+------------------+-------------------+
1rowinset(0.00sec)
mysql–uroot–pxxx(1.13上面操作)
changemastertomaster_host='192.168.1.16',master_user='root',master_password='xxx',master_log_file='hk16-bin.000003',master_log_pos=818885460;
startslave;\\启动从复制线程
showslavestatus\G\\查看主从复制状态,如有错误,需看具体错误代码内容
mysql>showslavestatus\G
***************************1.row***************************
Slave_IO_State:Waitingformastertosendevent
Master_Host:192.168.1.16
Master_User:root
Master_Port:3306
Connect_Retry:60
Master_Log_File:hk16-bin.000003
Read_Master_Log_Pos:823385525
Relay_Log_File:localhost-relay-bin.000023
Relay_Log_Pos:823385669
Relay_Master_Log_File:hk16-bin.000003
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
Replicate_Do_DB:anytracking4_1,anytracking4_2,anytracking4_3,anytracking4_4
Replicate_Ignore_DB:test,mysql,information-schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:0
Last_Error:
Skip_Counter:0
Exec_Master_Log_Pos:823385525
Relay_Log_Space:823385870
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:
1rowinset(0.00sec)
这样就完成了1.16与1.13不同版本、不同引擎数据库主主复制!!
注:同步过程中,出现的错误,在此不做说明!请查询相关错误代码解决。
5启动1.14从服务
mysql–uroot–pxxx(1.14上面操作)
startslave;\\启动从复制线程
showslavestatus\G\\查看主从复制状态,如下所示
mysql>showslavestatus\G
***************************1.row***************************
Slave_IO_State:Waitingformastertosendevent
Master_Host:192.168.1.13
Master_User:root
Master_Port:3306
Connect_Retry:60
Master_Log_File:mysql-bin.003581
Read_Master_Log_Pos:427635071
Relay_Log_File:hk14-relay-bin.000024
Relay_Log_Pos:427635216
Relay_Master_Log_File:mysql-bin.003581
Slave_IO_Running:Yes
Slave_SQL_Running:Yes
Replicate_Do_DB:anytracking4_1,anytracking4_2,anytracking4_3,anytracking4_4
Replicate_Ignore_DB:test,mysql,information-schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno:0
Last_Error:
Skip_Counter:0
Exec_Master_Log_Pos:427635071
Relay_Log_Space:427635413
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:
1rowinset(0.00sec)
到这里就已经完成整个升级操作,接下来就进行相应的测试下!
6写入数据测试复制集
方法一:手动插入数据到一个表里面,然后进行其他机器上继续查询看是否同步过来
方法二:写个批量插入程序,在此不做说明!!
7优化1.16数据库my.cnf配置
1.16服务器硬件配置如下:
机器型号:DELLPowerEdgeR7102U
CPU:4n16线程
内存:24GB
硬盘:1TB
操作系统版本:centos5.5
优化后的my.cnf配置文件参数示例如下:
[mysqld]
character-set-server=utf8
wait_timeout=31536000
max_connections=3000
max_connect_errors=10000
max_allowed_packet=32M
skip-name-resolve
lower_case_table_names=1
thread_cache=128
table_open_cache=1024
query_cache_type=1
query_cache_size=128M
join_buffer_size=128M
sort_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=2M
open_files_limit=30720
max_heap_table_size=128M
tmp_table_size=128M
tmpdir=/dev/shm
binlog_cache_size=12M
max_binlog_size=1G
expire_logs_days=7
binlog_checksum=none
sync_binlog=0
innodb_buffer_pool_size=15G
innodb_use_sys_malloc=1
innodb_log_file_size=256M
innodb_log_buffer_size=16M
innodb_flush_log_at_trx_commit=2
innodb_flush_method=O_DIRECT
innodb_lock_wait_timeout=50
innodb_read_io_threads=8
innodb_write_io_threads=8
innodb_thread_concurrency=0
innodb_rollback_segments=128
innodb_change_buffer_max_size=50
log-bin=hk16-bin
explicit_defaults_for_timestamp
basedir=/usr/local/mysql-5.6.10
datadir=/usr/local/mysql-5.6.10/data
port=3306
server-id=100
socket=/usr/local/mysql-5.6.10/data/mysql.sock
binlog-ignore-db=mysql
replicate-ignore-db=test
replicate-ignore-db=mysql
replicate-ignore-db=information-schema
replicate-ignore-db=performance_schema
replicate-do-db=anytracking4_1
replicate-do-db=anytracking4_2
replicate-do-db=anytracking4_3
replicate-do-db=anytracking4_4
[safe_mysqld]
log-error=/usr/local/mysql-5.6.10/data/hk16.err
pid-file=/usr/local/mysql-5.6.10/data/hk16.pid
user=mysql
[mysql]
socket=/usr/local/mysql-5.6.10/data/mysql.sock
default-character-set=utf8
auto-rehash
[mysqldump]
quick
max_allowed_packet=512M
[myisamchk]
key_buffer_size=512M
sort_buffer_size=512M
read_buffer=80M
write_buffer=80M
[mysqlhotcopy]
interactive-timeout
8预热数据表
有一种新的预热方法需要调参数:
8.1如果是正常关闭,可以使用5.6的快速预热备份!
你只需在my.cnf里,加入如下:
innodb_buffer_pool_dump_at_shutdown=1\\解释:在关闭时把热数据dump到本地磁盘。
innodb_buffer_pool_dump_now=1\\解释:采用手工方式把热数据dump到本地磁盘。
innodb_buffer_pool_load_at_startup=1\\解释:在启动时把热数据加载到内存。
innodb_buffer_pool_load_now=1\\解释:采用手工方式把热数据加载到内存。
在关闭MySQL时,会把内存中的热数据保存在磁盘里ib_buffer_pool文件中,位于数据目录下。
预热后,查看innodb_buffer_pool是不是变化较大
SHOWGLOBALSTATUSLIKE'%innodb_buffer%';
Innodb_buffer_pool_pages_data和Innodb_buffer_pool_pages_free是否变化较大!
8.2如果是异常关闭,就只能用5.1的手动预热方法了!
Innodb数据库数据手动预热方法:
SELECTtable_nameFROMinformation_schema.tablesWHEREtable_schemaNOTIN('mysql','information_schema','performance_schema')\\查询出所有的表
SELECTCONCAT('selectcount(1)from',table_schema,'.',table_name,';')FROMinformation_schema.tablesWHEREtable_schemaNOTIN('mysql','information_schema','performance_schema');\\组装预热表
SELECTCONCAT('select*from',table_schema,'.',table_name,'limit10;')FROMinformation_schema.tablesWHEREtable_schemaNOTIN('mysql','information_schema','performance_schema');\\组装预热表
将组装预热表查询出来的语句拷贝出来,然后在去数据库里执行!!可以只针对业务使用比较多的表进行预热。
编写者:李惟忠
公司:广州埃立方通信技术有限公司
日期:2013-09-26