在 Ubuntu 12.04 下搭建 MySQL C 开发环境
准备工作
1、安装MYSQL
1 | $ sudo apt-get install mysql-server mysql-workbench |
2、安装MYSQL C开发包
1 | $ sudo apt-get install libmysqlclient-dev |
注:安装MySQL需要设置管理员root帐号密码(非Linux账户root密码)。
建立数据表
MySQL安装完毕后,使用以下命令操作:
1 | $ mysql -u root -p |
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.5.24-0ubuntu0.12.04.1 (Ubuntu)
Copyright (c) 2000, 2011, 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>
1、添加一般用户
mysql> CREATE USER young@localhost IDENTIFIED BY 'young';
Query OK, 0 rows affected (0.02 sec)
mysql> GRANT ALL ON *.* to young@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql>
2、使用一般用户登录
1 | $ mysql -u young -p |
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 39
Server version: 5.5.24-0ubuntu0.12.04.1 (Ubuntu)
Copyright (c) 2000, 2011, 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>
3、建立数据库
mysql> CREATE DATABASE testdb;
Query OK, 1 row affected (0.00 sec)
察看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.01 sec)
4、建立数据表
mysql> use testdb;
Database changed
mysql> CREATE TABLE student(stuid CHAR(11), name VARCHAR(20), sex CHAR(1), birth DATE, major VARCHAR(30), PRIMARY KEY(stuid));
Query OK, 0 rows affected (0.33 sec)
5、导入数据
编写一个student.txt的文件,文件使用TAB分隔,内容如下:
20111110001 Jason m 1987-01-01 ComputerScience
20111110002 Peter m 1989-10-23 Mathematics
20111110003 Marry f 1989-11-11 Art
注:需将student.txt文件放置在/var/lib/mysql目录下,否则导入数据时无法找到文件。
将数据导入testdb数据库:
mysql> load data INFILE './student.txt' into table student;
Query OK, 3 rows affected (0.18 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql> use testdb;
Database changed
mysql> select * from student;
+-------------+-------+------+------------+-----------------+
| stuid | name | sex | birth | major |
+-------------+-------+------+------------+-----------------+
| 20111110001 | Jason | m | 1987-01-01 | ComputerScience |
| 20111110002 | Peter | m | 1989-10-23 | Mathematics |
| 20111110003 | Marry | f | 1989-11-01 | Art |
+-------------+-------+------+------------+-----------------+
3 rows in set (0.00 sec)
测试连接Mysql
编辑一个db-demo.c程序(来源自mysqlcapitutorial):
1 |
|
使用以下命令编译:
1 | $ gcc db-demo.c -lmysqlclient -o db-demo |
运行:
1 | $ ./db-demo |
20111110001 Jason m 1987-01-01 ComputerScience
20111110002 Peter m 1989-10-23 Mathematics
20111110003 Marry f 1989-11-01 Art
数据库导出:
1 | $ mysqldump -u user -p database > db.sql |
eg:
1 | $ mysqldump -u young -p testdb > /home/mutse/testdb.sql |
数据表导出:
1 | $ mysqldump -u user -p database table > table.sql |
eg:
1 | $ mysqldump -u young -p testdb student > /home/mutse/student.sql |
参考资料: