如何防止 MySQL 的 ibdata1 文件过大
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何防止 MySQL 的 ibdata1 文件过大相关的知识,希望对你有一定的参考价值。
参考技术A 1)备份数据库备份全部数据库,执行命令
#mysqldump -q -uroot -ppassword --add-drop-table --all-databases >/home/backup/all.sql
做完此步后,停止数据库服务。
#service mysqld stop
2)找到my.ini或my.cnf文件
linux下执行
# /usr/libexec/mysqld --verbose --help | grep -A 1 'Default options'
Default options are read from the following files in the given order:
/etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf
windows环境下可以:
mysqld --verbose --help > mysqlhelp.txt
notepad mysqlhelp.txt
在里面查找Default options,可以看到查找my.ini的顺序,以找到真实目录
3)修改mysql配置文件
打开my.ini或my.cnf文件
[mysqld]下增加下面配置
innodb_file_per_table=1
验证配置是否生效,可以重启mysql后,执行
#service mysqld restart
#mysql -uroot -ppassword
mysql> show variables like '%per_table%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_file_per_table | ON |
+-----------------------+-------+
1 row in set (0.00 sec)
mysql>
看看innodb_file_per_table变量是否为ON
MySQL ibdata1文件迁移
目的:主机系统/var目录快满了,经查询最大的文件是mysql的ibdata1文件,有17G大小,故需要迁移这个文件到其他目录下,以释放/var目录空间。
1.先备份下数据库是个好习惯
# mysqldump -q -uuser -ppassword --add-drop-table --all-databases > /usr2/backup/mysql_1013.sql
2.关闭mysql服务
# /etc/init.d/mysqld stop 停止 mysqld: [确定] # /etc/init.d/mysqld status mysqld 已停
3.移动ibdata1及其同目录下的文件,从/var/lib/mysql移动到/usr2/mysql
# pwd /var/lib/mysql # ls -lh 总用量 17G -rw-rw---- 1 mysql mysql 17G 10月 13 10:23 ibdata1 -rw-rw---- 1 mysql mysql 5.0M 10月 13 10:23 ib_logfile0 -rw-rw---- 1 mysql mysql 5.0M 10月 13 10:23 ib_logfile1 drwx------ 2 mysql mysql 4.0K 2月 19 2014 mysql srwxrwxrwx 1 mysql mysql 0 10月 13 10:23 mysql.sock drwx------ 2 mysql mysql 4.0K 2月 19 2014 test drwx------ 2 mysql mysql 4.0K 2月 26 2014 zabbix # mkdir /usr2/mysql # chown mysql:mysql /usr2/mysql # su - mysql $id uid=27(mysql) gid=27(mysql) 组=27(mysql) $ mv /var/lib/mysql/* /usr2/mysql/ $
4.修改mysql配置文件/etc/my.cnf,修改datadir和socket路径指向
修改前: # more /etc/my.cnf [mysqld] datadir=/var/lib/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 修改后: # more /etc/my.cnf [mysqld] datadir=/usr2/mysql socket=/usr2/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
5.启动mysql服务
# /etc/init.d/mysqld start 正在启动 mysqld: [确定] # /etc/init.d/mysqld status mysqld (pid 11907) 正在运行...
6.连接mysql,根据报错提示创建mysql.sock的软连接,验证库表数据,完成迁移工作。
# mysql -uuser -ppassword ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2) # ln -s /usr2/mysql/mysql.sock /var/lib/mysql/mysql.sock # mysql -uuser -ppassword Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 21 Server version: 5.1.66 Source distribution Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | | zabbix | +--------------------+ 3 rows in set (0.00 sec) mysql> use zabbix Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-----------------------+ | Tables_in_zabbix | +-----------------------+ | acknowledges | | actions | | alerts | ……
以上是关于如何防止 MySQL 的 ibdata1 文件过大的主要内容,如果未能解决你的问题,请参考以下文章
Zabbix-Server数据库mysql的libdata1 mysqllog文件过大