问题描述:工作中要部署一个linux+django+mysql+apache框架的后台网站测试环境,给业务测试人员使用
环境工具:目标服务器工具(虚拟机centOS6.0,python2.6,MySQL5.1,django1.2.4,Apache2.2)
目标服务器空间
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 31G 18G 12G 66% /
tmpfs tmpfs 499M 0 499M 0% /dev/shm
/dev/sda1 ext4 485M 30M 430M 7% /boot
/dev/mapper/VolGroup-lv_home ext4 31G 144M 29G 2% /home
源服务器工具(Debian 6.0,python2.6,MySQL5.1,django1.2.4,Apache2.2)
解决过程:1. 由于正式环境中的数据库有10多个库,800多张表,但是测试环境只用cada1,cada2两个库(200张表左右),django应用中首先要能检查到所有库和表的存在,因此要将无数据的数据库结构倒出来到目标MySQL服务器中
使用
mysqldump -u root -p -d --add-drop-table -all-databases > /home/haokuan/datastruct.sql
sql文件大小为4.34M,将结果在目标上source即可。
2. 导出cada1库中所有表的具体数据
使用
mysqldump -u root -p --add-drop-table cada1 > /home/haokuan/cada1.sql
sql文件大小为1.5G左右;
导出cada2库中80张表(大概占1/3,其余为备份表)的具体数据
使用
mysqldump -u root -p --add-drop-table --exclude=cada2.table1 --exclude=cada2.table2 --exclude=cada2.table3 ... cada2 > /home/haokuan/cada2.sql
sql文件大小为19G左右
3. 在源服务器上,计算/var/lib/mysql/cada1,/var/lib/mysql/cada2的大小
使用
du -sh /var/lib/mysql/cada1 du -sh /var/lib/mysql/cada2
cada1文件夹下的*.frm,*.MYD,*.MYI文件接近2G,cada2文件夹大小接近21G
4. 目标服务器的MySQL datapath=/var/lib/mysql,但是挂载在 / 的空间只剩下12G,空间不够,而挂载在/home的空间有29G,比较充足,但是通过scp命令将数据sql文件从源服务器放到目标服务器上面,也需要20G左右的空间,经过检查发现源服务器上挂载在 / 的空间,可以清理垃圾,
使用tar命令将/目录下的一些数据文件压缩,rm -rf将垃圾清理掉,使用vi将my.cnf中的
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
改为
datadir=/home/mysql
socket=/home/mysql/mysql.sock
使用mv将/var/lib/mysql下面的东西转移到/home/mysql
使用scp [email protected]:/home/haokuan/data.tar.gz /var/lib/mysql/
5. 重启mysql,报错
130729 13:34:15 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
130729 13:34:15 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
130729 13:34:15 mysqld_safe mysqld from pid file /usr/local/mysql/var/mail.bmitwap.com.pid ended
130729 13:38:35 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/var
datadir=/home/mysql
转移sql datapath时候,造成文件缺失
使用/usr/bin/mysql_install_db --user=mysql --datadir=/home/mysql重新生成相关文件
使用/usr/bin/mysqld_safe --datadir=/home/mysql --socket=/home/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql 启动mysql 成功
但是mysql命令连接服务器时候报错
Can 't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock '
在修改my.cnf时候,已经修改了socket路径,不知道为什么报错,但是可以使用
ln -s /home/mysql/mysql.sock /var/lib/mysql/mysql.sock 做一个软链接
6. 导入数据之前,想起头一次导入整个库的数据,应该注意字符集问题
源服务器上
mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
目标服务器上
mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | latin1_general_ci |
| collation_database | latin1_general_ci |
| collation_server | latin1_general_ci |
+----------------------+-----------------+
mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | latin1 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
源服务器的存储字符集和连接字符集都是utf8,目标服务器需要修改配置,修改my.cnf文件,
[mysqld]
datadir=/home/mysql
socket=/home/mysql/mysql.sock
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
default-character-set=utf8
--------------------------------------------------------------
改为
[mysqld]
datadir=/home/mysql
socket=/home/mysql/mysql.sock
user=mysql
default-character-set=utf8
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
default-character-set=utf8
[client]
default-character-set=utf8
--------------------------------------------------------------
其中default-character-set=utf8将character_set_server和character_set_system改为utf8,其余的是Mysql连接和查询字符集,由default-character-set=utf8决定,字符集改完了,使用screen做后台服务导数据
7. 期间,由于空间不足问题,我新增了一块16G的硬盘,重启linux后,fdisk看到新增设备sdb
使用mkfs -t ext4 /dev/sdb
使用mount /dev/sdb /home/mysql
使用df -lhT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 31G 28G 1.3G 96% /
tmpfs tmpfs 499M 0 499M 0% /dev/shm
/dev/sda1 ext4 485M 30M 430M 7% /boot
/dev/mapper/VolGroup-lv_home ext4 31G 23G 6.7G 77% /home
/dev/sdb ext4 16G 172M 15G 2% /home/mysql
最后想起/home/mysql如果满了,也不会向上扩展使用父文件夹/home挂载的设备的空间,用不上,
使用umount /dev/sdb卸载掉