1 myisam 与 innodb的区别进行总结,并讨论在什么情况下用什么ENGINE!
如果你的大数据表更新很频繁,建议用innodb,这个是基于行锁定的。myisam是基于表锁定的。这意味着如果你的UPDATE,INSERT并发很多的话,会有性能问题。myisam是针对90%以上是读的情况下设计的。在并发量大的时候还是用INNODB来的好。
2 创建表,表内的数据由/etc/passwd的内容(name,uid,gid,home directory,bashh)来提供自动导入进来
首先将/etc/passwd中的username,uid,gid,家目录,shell导到/etc/wu。
[root@localhost ~]# cd /etc/
[root@localhost etc]# touch wu
[root@localhost etc]# cat wu
[root@localhost etc]#
[root@localhost etc]# awk -F: '{print $1,$3,$4,$6,$7}' /etc/passwd >/etc/wu
[root@localhost etc]# cat wu
root 0 0 /root /bin/bash
bin 1 1 /bin /sbin/nologin
daemon 2 2 /sbin /sbin/nologin
adm 3 4 /var/adm /sbin/nologin
lp 4 7 /var/spool/lpd /sbin/nologin
sync 5 0 /sbin /bin/sync
shutdown 6 0 /sbin /sbin/shutdown
halt 7 0 /sbin /sbin/halt
mail 8 12 /var/spool/mail /sbin/nologin
news 9 13 /etc/news
uucp 10 14 /var/spool/uucp /sbin/nologin
operator 11 0 /root /sbin/nologin
games 12 100 /usr/games /sbin/nologin
gopher 13 30 /var/gopher /sbin/nologin
ftp 14 50 /var/ftp /sbin/nologin
nobody 99 99 / /sbin/nologin
nscd 28 28 / /sbin/nologin
vcsa 69 69 /dev /sbin/nologin
rpc 32 32 / /sbin/nologin
mailnull 47 47 /var/spool/mqueue /sbin/nologin
smmsp 51 51 /var/spool/mqueue /sbin/nologin
pcap 77 77 /var/arpwatch /sbin/nologin
ntp 38 38 /etc/ntp /sbin/nologin
dbus 81 81 / /sbin/nologin
avahi 70 70 / /sbin/nologin
sshd 74 74 /var/empty/sshd /sbin/nologin
rpcuser 29 29 /var/lib/nfs /sbin/nologin
nfsnobody 65534 65534 /var/lib/nfs /sbin/nologin
haldaemon 68 68 / /sbin/nologin
avahi-autoipd 100 101 /var/lib/avahi-autoipd /sbin/nologin
xfs 43 43 /etc/X11/fs /sbin/nologin
gdm 42 42 /var/gdm /sbin/nologin
sabayon 86 86 /home/sabayon /sbin/nologin
mysql 27 27 /var/lib/mysql /bin/bash
apache 48 48 /var/www /sbin/nologin
[root@localhost etc]#
进入mysql:
[root@localhost ~]# mysql
mysql> use test
Database changed
mysql>
创建表passwd:
mysql> create table passwd (name char(10),uid int(10),gid int(10),home char(200),shell char(200));
Query OK, 0 rows affected (0.01 sec)
导入数据:
mysql> load data infile '/etc/wu' into table passwd fields terminated by " ";
Query OK, 35 rows affected, 1 warning (0.01 sec)
Records: 35 Deleted: 0 Skipped: 0 Warnings: 1
查看passwd中的内容:
mysql> select * from passwd;
+------------+-------+-------+------------------------+----------------+
| name
| uid | gid | home | shell |
+------------+-------+-------+------------------------+----------------+
| root
| 0 | 0 | /root | /bin/bash |
| bin
| 1 | 1 | /bin | /sbin/nologin |
| daemon
| 2 | 2 | /sbin | /sbin/nologin |
| adm
| 3 | 4 | /var/adm | /sbin/nologin |
| lp
| 4 | 7 | /var/spool/lpd | /sbin/nologin |
| sync
| 5 | 0 | /sbin | /bin/sync |
| shutdown
| 6 | 0 | /sbin | /sbin/shutdown |
| halt
| 7 | 0 | /sbin | /sbin/halt |
| mail
| 8 | 12 | /var/spool/mail | /sbin/nologin |
| news
| 9 | 13 | /etc/news | |
| uucp
| 10 | 14 | /var/spool/uucp | /sbin/nologin |
| operator
| 11 | 0 | /root | /sbin/nologin |
| games
| 12 | 100 | /usr/games | /sbin/nologin |
| gopher
| 13 | 30 | /var/gopher | /sbin/nologin |
| ftp
| 14 | 50 | /var/ftp | /sbin/nologin |
| nobody
| 99 | 99 | / | /sbin/nologin |
| nscd
| 28 | 28 | / | /sbin/nologin |
| vcsa
| 69 | 69 | /dev | /sbin/nologin |
| rpc
| 32 | 32 | / | /sbin/nologin |
| mailnull
| 47 | 47 | /var/spool/mqueue | /sbin/nologin |
| smmsp
| 51 | 51 | /var/spool/mqueue | /sbin/nologin |
| pcap
| 77 | 77 | /var/arpwatch | /sbin/nologin |
| ntp
| 38 | 38 | /etc/ntp | /sbin/nologin |
| dbus
| 81 | 81 | / | /sbin/nologin |
| avahi
| 70 | 70 | / | /sbin/nologin |
| sshd
| 74 | 74 | /var/empty/sshd | /sbin/nologin |
| rpcuser
| 29 | 29 | /var/lib/nfs | /sbin/nologin |
| nfsnobody | 65534 | 65534 | /var/lib/nfs
| /sbin/nologin |
| haldaemon |
68 | 68 | / | /sbin/nologin |
| avahi-auto |
100 | 101 | /var/lib/avahi-autoipd | /sbin/nologin |
| xfs
| 43 | 43 | /etc/X11/fs | /sbin/nologin |
| gdm
| 42 | 42 | /var/gdm | /sbin/nologin |
| sabayon
| 86 | 86 | /home/sabayon | /sbin/nologin |
| mysql
| 27 | 27 | /var/lib/mysql | /bin/bash |
| apache
| 48 | 48 | /var/www | /sbin/nologin |
+------------+-------+-------+------------------------+----------------+
35 rows in set (0.00 sec)
导出数据:
mysql> select * from passwd into outfile '/tmp/wu' fields terminated by " ";
ERROR 1086 (HY000): File '/tmp/wu' already exists
mysql> select * from passwd into outfile '/tmp/wu' fields terminated by " ";
Query OK, 35 rows affected (0.00 sec)
mysql>
查看导出内容:
[root@localhost tmp]# cat wu
root 0 0 /root /bin/bash
bin 1 1 /bin /sbin/nologin
daemon 2 2 /sbin /sbin/nologin
adm 3 4 /var/adm /sbin/nologin
lp 4 7 /var/spool/lpd /sbin/nologin
sync 5 0 /sbin /bin/sync
shutdown 6 0 /sbin /sbin/shutdown
halt 7 0 /sbin /sbin/halt
mail 8 12 /var/spool/mail /sbin/nologin
news 9 13 /etc/news
uucp 10 14 /var/spool/uucp /sbin/nologin
operator 11 0 /root /sbin/nologin
games 12 100 /usr/games /sbin/nologin
gopher 13 30 /var/gopher /sbin/nologin
ftp 14 50 /var/ftp /sbin/nologin
nobody 99 99 / /sbin/nologin
nscd 28 28 / /sbin/nologin
vcsa 69 69 /dev /sbin/nologin
rpc 32 32 / /sbin/nologin
mailnull 47 47 /var/spool/mqueue /sbin/nologin
smmsp 51 51 /var/spool/mqueue /sbin/nologin
pcap 77 77 /var/arpwatch /sbin/nologin
ntp 38 38 /etc/ntp /sbin/nologin
dbus 81 81 / /sbin/nologin
avahi 70 70 / /sbin/nologin
sshd 74 74 /var/empty/sshd /sbin/nologin
rpcuser 29 29 /var/lib/nfs /sbin/nologin
nfsnobody 65534 65534 /var/lib/nfs /sbin/nologin
haldaemon 68 68 / /sbin/nologin
avahi-auto 100 101 /var/lib/avahi-autoipd /sbin/nologin
xfs 43 43 /etc/X11/fs /sbin/nologin
gdm 42 42 /var/gdm /sbin/nologin
sabayon 86 86 /home/sabayon /sbin/nologin
mysql 27 27 /var/lib/mysql /bin/bash
apache 48 48 /var/www /sbin/nologin
[root@localhost tmp]#
导出成功。
3 使用mydumper,myload进行备份还原,并与mysqldump进行速度比较
使用mydumper,myload进行备份还原,并与mysqldump进行速度比较.
备份前准备:
[root@localhost~]# seq 1 100000000 > /tmp/bigfile
[root@localhost tmp]# ll -h bigfile
-rw-r--r-- 1 root root 1.2G 01-30 18:34 bigfile
[root@localhost tmp]#
[root@localhost ~]# /etc/init.d/mysqld start
[root@localhost ~]# mysql
mysql> show databases;
mysql> use test
Database changed
mysql> create table wu (a int);
一、Mydumper、myload。
安装软件包:
[root@localhost ~]# yum install gcc gcc-c++ glib2-devel mysql-devel zlib-devel pcre-devel –y
[root@localhost ~]# rpm -ivh cmake-2.6.4-7.el5.i386.rpm
[root@localhost ~]# tar fvxz mydumper-0.2.3.tar.gz
[root@localhost ~]# cd mydumper-0.2.3
[root@localhost mydumper-0.2.3]# cmake .
[root@localhost mydumper-0.2.3]# make && make install
创建备份目录:
[root@localhost ~]# mkdir -pv /tmp/sqlbak
mkdir: 已创建目录 “/tmp/sqlbak”
[root@localhost ~]#
备份:
[root@localhost tmp]# time mydumper -B test -o /tmp/sqlbak -r 100000
real
0m59.162s
user
0m10.909s
sys
0m4.783s
[root@localhost ~]# echo $?
0
[root@localhost ~]#
还原:
[root@localhost ~]# myloader -u root -d /tmp/sqlbak -B test
二、mysqldump的备份与还原。
[root@localhost tmp]# touch mysqlbak
备份:
[root@localhost tmp]# time mysqldump test > /tmp/mysqlbak
real
1m6.353s
user
0m7.672s
sys
0m12.130s
[root@localhost tmp]#
还原:
mysql> show databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| mysql
|
| test
|
+--------------------+
3 rows in set (0.00 sec)
删除test库:
mysql> drop database test;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| mysql
|
+--------------------+
2 rows in set (0.00 sec)
创建test库:
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
[root@localhost tmp]# mysql -u root -p test < /tmp/mysqlbak
Enter password:
[root@localhost tmp]#
通过实验比较可得出:mydumper比mysqldump的备份速度快。
4 全备份结合binlog来一个备份实验
[root@localhost tmp]# vim /etc/my.cnf
[mysqld]
log-bin=binlog
log-bin-index=binlog.index
sync_binlog=0
[root@localhost tmp]# service mysqld restart
停止 MySQL:
[确定]
启动 MySQL:
[确定]
[root@localhost tmp]# cd /var/lib/mysql/
[root@localhost mysql]# ls
binlog.000001 ibdata1
ib_logfile1 mysql.sock
binlog.index
ib_logfile0 mysql test
[root@localhost mysql]# mysqlbinlog binlog.000001
mysql> create database ww;
Query OK, 1 row affected (0.02 sec)
mysql> use ww
Database changed
mysql> create table a (a int);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into a values (1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values (2);
Query OK, 1 row affected (0.00 sec)
mysql> insert into a values (3);
Query OK, 1 row affected (0.00 sec)
mysql> select * from a;
+------+
| a
|
+------+
|
1 |
|
2 |
|
3 |
+------+
3 rows in set (0.00 sec)
mysql>
mysql> drop table a;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql>
[root@localhost mysql]# mysqlbinlog binlog.000001
Binglog.00001中记录了数据库增删改的信息。表a删除后,进行恢复:
[root@localhost mysql]# mysqlbinlog --start-position 177 --stop-position 515 binlog.000001 |mysql -u root -p
Enter password:
[root@localhost mysql]#
5 数据库的大小,备份速度和什么相关?自行查找
对于一个数据库完整备份来说,备份的速度很大程度上取决于下面两个因素:读磁盘数据、日志文件的吞吐量,写磁盘数据文件的吞吐量。
读吞吐量的大小取决于磁盘读取数据的速度,而磁盘读取的速度又取决于数据文件在磁盘中的位置。因此,位于不同盘符上不同数据库文件的读取速度都不相同。
提高读吞吐量:
- 使用更快的磁盘。
- 把多个数据库文件存储在不同的物理磁盘上
- 减少数据库文件碎片级别
-
- 提高写吞吐量:
- 使用更快的磁盘进行备份
- 把备份文件分割成多个文件(在相同或不同的物理磁盘上,这取决于磁盘的吞吐量)
- 使用备份压缩工具。假如压缩速度非常好的话,那么就会减少写到磁盘上的数据量,从而加大写吞吐量。一般情况执行这种压缩程序都会消耗大量的CPU资源