时间:2021-07-01 10:21:17 帮助过:3人阅读
# cat myuser.txt zhao 25 8 2015-1-1 qian 22 4 2014-5-6 sun 31 1 2013-12-7 li 40 6 2014-12-12 zhou 45 3 2015-2-8 wu 18 1 2014-9-12 zheng 44 9 2012-10-12 wang 29 12 2015-3-62. 然后创建一张目标数据库表,表的数据结构要和文本文件一一对应,如下:
mysql> use db_users; 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> create table user_import ( name varchar(32) not null primary key, age int, level int, login_date date ); Query OK, 0 rows affected (0.02 sec) mysql> desc user_import; +------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+-------+ | name | varchar(32) | NO | PRI | NULL | | | age | int(11) | YES | | NULL | | | level | int(11) | YES | | NULL | | | login_date | date | YES | | NULL | | +------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql>3.导入数据,mysqlimport是MySQL提供的导入工具,该工具可以把文本文件导入到用户指定的数据表中。
# cp /home/allen/user_import.txt /var/lib/mysql/db_users/ # mysqlimport -uroot -pxxx db_users user_import.txt db_users.user_import: Records: 8 Deleted: 0 Skipped: 0 Warnings: 0 # mysqlimport -uroot -p db_users user_import.txt Enter password: mysqlimport: Error: 1062, Duplicate entry 'zhao' for key 'PRIMARY', when using table: user_import # mysqlimport -d -uroot -p db_users user_import.txt Enter password: db_users.user_import: Records: 8 Deleted: 0 Skipped: 0 Warnings: 0然后查看数据导入后的情况如下:
# mysql -u root -p Enter password: ...... mysql> use db_users; 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> select * from user_import; +-------+------+-------+------------+ | name | age | level | login_date | +-------+------+-------+------------+ | zhao | 25 | 8 | 2015-01-01 | | qian | 22 | 4 | 2014-05-06 | | sun | 31 | 1 | 2013-12-07 | | li | 40 | 6 | 2014-12-12 | | zhou | 45 | 3 | 2015-02-08 | | wu | 18 | 1 | 2014-09-12 | | zheng | 44 | 9 | 2012-10-12 | | wang | 29 | 12 | 2015-03-06 | +-------+------+-------+------------+ 8 rows in set (0.00 sec)有时候数据源的间隔符可能不是默认tab键,有可能是逗号,这时可以加入参数--field-terminatied-by=str, 导入的命令为:
# mysqlimport -uroot -pxxx --fields-terminated-by=, db_users user_import2.txt db_users.user_import2: Records: 8 Deleted: 0 Skipped: 0 Warnings: 0数据导出是将数据库中已存的数据导出到固定文本记录,mysqldump是MySQL中专门仅数据导出服务的工具,它可以将一个数据库、表,设置存储过程一SQL语句的形式导出,另外在数据备份中也会使用该工具。其使用格式为: mysqldump [-r/...] databsesname < data.sql [-r/...]为可选参数;
# mysqldump -u root -p db_users > db_userr.sql Enter password: # ls -al db_userr.sql -rw-r--r--. 1 root root 6366 Jun 10 22:52 db_userr.sql # vim db_userr.sql -- MySQL dump 10.13 Distrib 5.1.66, for redhat-linux-gnu (i386) -- -- Host: localhost Database: db_users -- ------------------------------------------------------ -- Server version 5.1.66 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `password` -- DROP TABLE IF EXISTS `password`; /*!40101 SET @saved_cs_client = @@character_set_client */; "db_userr.sql" 163L, 6366C 1,1 Top -- MySQL dump 10.13 Distrib 5.1.66, for redhat-linux-gnu (i386) -- -- Host: localhost Database: db_users -- ------------------------------------------------------ -- Server version 5.1.66 ......数据导出成功。 数据库导出文件可以通过如下几种方式来恢复到数据库中: 1 利用mysql命令执行数据的恢复操作:
# mysql -u root db_users2 < db_userr.sql -p Enter password: [root@localhost allen]# mysql -u root -p Enter password: mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db_users | | db_users2 | | mysql | | test | +--------------------+ 5 rows in set (0.00 sec) mysql>2. sql语句source 来导入。
mysql> create database db_users3; Query OK, 1 row affected (0.00 sec) mysql> use db_users3; Database changed mysql> source db_userr.sql Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec)对于单个表示的数据备份可以采用select into ... outfile ... 进行数据导出,利用load data ... 方式进行数据导入。 数据表备份执行如下:
mysql> select * into outfile 'tbbk_users' from tb_users; Query OK, 13 rows affected (0.00 sec)数据表执行如下:
mysql> load data infile 'tbbk_users' into table tb_users; Query OK, 13 rows affected, 13 warnings (0.00 sec) Records: 13 Deleted: 0 Skipped: 0 Warnings: 13
搞定linux上MySQL编程(五):数据导入导出和备份
标签:mysql