最近,又在ubuntu上面搭建mysql环境,使用C语言接口对mysql数据库进行操作。安装mysql时分别用了命令行安装、源代码编译安装两种方法,在源码安装的时候出现了一点小插曲,mysql-5.7版本make的时候耗时非常长,在40%进度一直没有反应,无奈重新下载使用mysql5.6版本重新编译完成。大概是版本没有完全支持的原因,ubuntu15.04命令行安装mysql默认版本也是5.6。
1、安装mysql
命令行方式与源代码编译方式
1.1命令行方式安装mysql第一步检查是否安装mysql,命令行输入:
liang@ubuntu:~$ mysql
The program 'mysql' can be found in the following packages: * mysql-client-core-5.6 * mariadb-client-core-10.0 Try: sudo apt-get install <selected package>
liang@ubuntu:~$ sudo apt-get update然后,安装mysql,命令行输入:
liang@ubuntu:~$ sudo apt-get install mysql-client mysql-server
liang@ubuntu:~$ mysql -V打印如下信息即安装成功:
mysql Ver 14.14 Distrib 5.6.27, for debian-linux-gnu (x86_64) using EditLine wrapper最后,安装C语言连接mysql需要的API库,命令行输入
liang@ubuntu:~$ sudo apt-get install libmysqlclient18 libmysqlclient-dev
1.2源代码编译安装mysql
编译源代码需要安装gcc,命令行输入:sudo apt-get install build-essential下载最新版本的mysql源码包,命令行输入:
liang@ubuntu:~/mysql$ wget http://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.28.tar.gz解压源码包,命令行输入:
liang@ubuntu:~/mysql$ tar zxvf mysql-5.6.28e.tar.gz
liang@ubuntu:~/mysql$ cd mysql-5.6.28安装其他依赖包,命令行输入:
liang@ubuntu:~/mysql/mysql-5.6.28$ sudo apt-get install cmake m4 bison libncurses5-dev libmysqlclient×开始源码编译安装mysql,命令行输入:
liang@ubuntu:~/mysql/mysql-5.6.28$ sudo cmake .打印如下错误信息,提示安装boost库:
CMake Error at cmake/boost.cmake:76 (MESSAGE): You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory> This CMake script will look for boost in <directory>. If it is not there, it will download and unpack it (in that directory) for you. If you are inside a firewall, you may need to use an http proxy: export http_proxy=http://example.com:80 Call Stack (most recent call first): cmake/boost.cmake:228 (COULD_NOT_FIND_BOOST) CMakeLists.txt:435 (INCLUDE) -- Configuring incomplete, errors occurred! See also "/home/liang/mysql/mysql-5.6.28/CMakeFiles/CMakeOutput.log".
<pre name="code" class="plain"> liang@ubuntu:~/mysql/mysql-5.6.28$ cd ..
liang@ubuntu:~/mysql$ wget http://downloads.sourceforge.net/boost/boost_1_59_0.tar.bz2解压boost源码包,命令行输入:
liang@ubuntu:~/mysql$ tar xvf boost_1_59_0.tar.bz2进入boost目录,命令行输入:
liang@ubuntu:~/mysql$ cd boost_1_59_0/安装boost,命令行输入:
liang@ubuntu:~/mysql/boost_1_59_0$ sudo ./bootstrap.sh liang@ubuntu:~/mysql/boost_1_59_0$ sudo ./b2 install执行的时间比较长,耐心等候。。。。。。。。。
liang@ubuntu:~/mysql/boost_1_59_0$ ls /usr/local/include/打印输出存在boost目录,即安装成功。
boost dnet.h pcap-namedb.h pcre.h sfbpf.h
liang@ubuntu:~/mysql/mysql-5.6.28$ sudo cmake . liang@ubuntu:~/mysql/mysql-5.6.28$ sudo make && sudo make install
执行的时间比较长,耐心等候。。。。。。。。。
安装完成后,初始化mysql数据库,命令行输入:
liang@ubuntu:~/mysql/mysql-5.6.28$ cd /usr/local/mysql/scripts/ liang@ubuntu:/usr/local/mysql/scripts$ sudo ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data &
liang@ubuntu:/usr/local/mysql/scripts$ cd ../bin/
liang@ubuntu:/usr/local/mysql/bin$ sudo ./mysqld -uroot
2015-12-18 11:32:27 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2015-12-18 11:32:27 0 [Note] ./mysqld (mysqld 5.6.28) starting as process 6106 ... 2015-12-18 11:32:27 6106 [Note] Plugin 'FEDERATED' is disabled. 2015-12-18 11:32:27 6106 [Note] InnoDB: Using atomics to ref count buffer pool pages 2015-12-18 11:32:27 6106 [Note] InnoDB: The InnoDB memory heap is disabled 2015-12-18 11:32:27 6106 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2015-12-18 11:32:27 6106 [Note] InnoDB: Memory barrier is not used 2015-12-18 11:32:27 6106 [Note] InnoDB: Compressed tables use zlib 1.2.3 2015-12-18 11:32:27 6106 [Note] InnoDB: Using CPU crc32 instructions 2015-12-18 11:32:27 6106 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2015-12-18 11:32:27 6106 [Note] InnoDB: Completed initialization of buffer pool 2015-12-18 11:32:27 6106 [Note] InnoDB: Highest supported file format is Barracuda. 2015-12-18 11:32:27 6106 [Note] InnoDB: 128 rollback segment(s) are active. 2015-12-18 11:32:27 6106 [Note] InnoDB: Waiting for purge to start 2015-12-18 11:32:27 6106 [Note] InnoDB: 5.6.28 started; log sequence number 1625997 2015-12-18 11:32:27 6106 [Note] Server hostname (bind-address): '*'; port: 3306 2015-12-18 11:32:27 6106 [Note] IPv6 is available. 2015-12-18 11:32:27 6106 [Note] - '::' resolves to '::'; 2015-12-18 11:32:27 6106 [Note] Server socket created on IP: '::'. 2015-12-18 11:32:28 6106 [Note] Event Scheduler: Loaded 0 events 2015-12-18 11:32:28 6106 [Note] ./mysqld: ready for connections. Version: '5.6.28' socket: '/tmp/mysql.sock' port: 3306 Source distribution
打开另外一个命令行,连接mysql:
liang@ubuntu:~$ cd /usr/local/mysql/bin/
liang@ubuntu:/usr/local/mysql/bin$ ./mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.28 Source distribution Copyright (c) 2000, 2015, 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>
liang@ubuntu:~$ mysql -uroot -p回车,输入mysql的root密码,密码为空直接确认,进入mysql命令行;
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.6.27-0ubuntu1 (Ubuntu) Copyright (c) 2000, 2015, 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>
mysql> show databases;打印输出如下,系统默认创建三个数据库:
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec)
mysql> create database joy;打印输出如下,创建数据库成功:
Query OK, 1 row affected (0.00 sec)重新查询数据库,会存在新建数据库 joy
mysql> show databases;结果如下,存在四个数据库,包括 joy
+--------------------+ | Database | +--------------------+ | information_schema | | joy | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec)
mysql> use joy;出现如下结果,进入 就哦也数据库成功:
Database changed列出数据库中的全部表,命令行输入:
mysql> show tables;现在 joy 是一个新建的数据库,不存在数据表:
Empty set (0.00 sec)创建一个session数据表,命令行输入:
mysql> CREATE TABLE IF NOT EXISTS `session` ( `id` int(10) unsigned NOT NULL, `uid` int(10) unsigned NOT NULL, `sig` varchar(32) NOT NULL, `expired` int(10) unsigned NOT NULL ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;打印如下结果创建成功:
Query OK, 0 rows affected (0.19 sec)
mysql> show tables;打印如下结果,看到session数据表:
+---------------+ | Tables_in_joy | +---------------+ | session | +---------------+ 1 row in set (0.00 sec)验证session数据表的元素,命令行输入:
mysql> desc session;打印如下结果:
+---------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | | NULL | | | uid | int(10) unsigned | NO | | NULL | | | sig | varchar(32) | NO | | NULL | | | expired | int(10) unsigned | NO | | NULL | | +---------+------------------+------+-----+---------+-------+ 4 rows in set (0.04 sec)
mysql> INSERT INTO `session` (`id`, `uid`, `sig`, `expired`) VALUES(1, 1, '8460abc97bbd11e5acaf000c29c494bc', 1448440960);打印如下结果插入数据成功成功:
Query OK, 1 row affected (0.06 sec)查询session数据表的数据,命令行输入:
mysql> select * from session;打印如下结果,可以看到上一个插入的数据:
+----+-----+----------------------------------+------------+ | id | uid | sig | expired | +----+-----+----------------------------------+------------+ | 1 | 1 | 8460abc97bbd11e5acaf000c29c494bc | 1448440960 | +----+-----+----------------------------------+------------+ 1 row in set (0.00 sec)修改session数据表中的数据,修改id=1上的数据项sig的值为“0000000000000000000000000”,命令行输入:
mysql> update session set sig = "0000000000000000000000000" where id = "1";
打印如下结果即修改数据项成功:
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from session;id=1上的数据项sig的值已经发生改变:
+----+-----+---------------------------+------------+ | id | uid | sig | expired | +----+-----+---------------------------+------------+ | 1 | 1 | 0000000000000000000000000 | 1448440960 | +----+-----+---------------------------+------------+ 1 row in set (0.00 sec)
mysql> delete from session where id=1;出现如下结果,删除成功:
Query OK, 1 row affected (0.07 sec)再次查询session数据表,命令行输入:
mysql> select * from session;session表中的数据已经被删除:
Empty set (0.00 sec)
需要安装libmysql客户端支持,命令行输入:
liang@ubuntu:~/workspace/mysql$ sudo apt-get install libmysqlclient*
3.1连接数据库
连接mysql的API函数,包括:MYSQL操作对象、连接主机、用户(MYSQL)、密码、数据库名、端口等等,结果返回0即成功,其他为连接失败</span>
MYSQL * STDCALL mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned long clientflag);
3.2数据的增删查改
int STDCALL mysql_query(MYSQL *mysql, const char *q);
数据库操作API,使用该函数对mysql执行sql语句,包括了创建数据库、创建表、增删查改等等。参数:一个数据库操作对象、sql执行语句
下面的例子,第一次连接数据库后创建test数据库,第二次连接数据库连接到test数据库,并且创建joy数据表,插入两条数据到joy,然后遍历查询joy中的数据。
使用gcc编译:
liang@ubuntu:~/workspace/mysql$ gcc -g -o test -I/usr/include/mysql MysqlTest.c -L/usr/lib/mysql -lmysqlclient -lz生成test可执行文件:(前提是test数据库不存在)
liang@ubuntu:~/workspace/mysql$ ./test查看结果:
liang@ubuntu:~/workspace/mysql$ ./test select data success 1 1 8460abc97bbd11e5acaf000c29c494bc 1448440960 2 1 2a6e0d7f810911e5a8b2000c29c494bc 1449023207mysql客户端查看:
mysql> select * from session; +----+-----+----------------------------------+------------+ | id | uid | sig | expired | +----+-----+----------------------------------+------------+ | 1 | 1 | 8460abc97bbd11e5acaf000c29c494bc | 1448440960 | | 2 | 1 | 2a6e0d7f810911e5a8b2000c29c494bc | 1449023207 | +----+-----+----------------------------------+------------+ 2 rows in set (0.00 sec)
#include <stdio.h> #include <stdlib.h> #include <mysql.h> #define mysqlhost "127.0.0.1" #define mysqluser "root" #define mysqlpwd "" #define dbname "test" int main(int argc, char **argv) { MYSQL *conn,*conn1; MYSQL_RES *mysqlresult; MYSQL_ROW mysqlrow; int x, y, numrow, numcol,result; char sql_create_table[300]="CREATE TABLE IF NOT EXISTS session (id int(10) unsigned NOT NULL,uid int(10) unsigned NOT NULL,sig varchar(32) NOT NULL,expired int(10) unsigned NOT NULL)ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;"; char sql_insert_data[300]="INSERT INTO `session` (`id`, `uid`, `sig`, `expired`) VALUES(1, 1, '8460abc97bbd11e5acaf000c29c494bc', 1448440960),(2, 1, '2a6e0d7f810911e5a8b2000c29c494bc', 1449023207)"; char sql_select_data[300]="select * from session"; if( (conn = mysql_init(NULL))== NULL){ printf("mysql_init failed! exiting...\n"); return 1; } if( (conn1 = mysql_init(NULL))== NULL){ printf("mysql_init failed! exiting...\n"); return 1; } if (mysql_real_connect(conn1, mysqlhost, mysqluser, mysqlpwd, NULL, 0, NULL, 0) == NULL) { printf("mysql_real_connect error %u: %s\n", mysql_errno(conn1), mysql_error(conn1)); return 1; } if (mysql_query(conn1, "create database test")) { //创建test数据库 printf("mysql_query create database error %u: %s\n", mysql_errno(conn1), mysql_error(conn1)); return 1; } else{ printf("create databae success\n"); mysql_close(conn1); } if (mysql_real_connect(conn, mysqlhost, mysqluser, mysqlpwd, dbname, 0, NULL, 0) == NULL) { printf("mysql_real_connect error %u: %s\n", mysql_errno(conn), mysql_error(conn)); return 1; } if (mysql_query(conn,sql_create_table)) { //创建session表 printf("mysql_query create table error %u: %s\n", mysql_errno(conn), mysql_error(conn)); return 1; } else{ printf("create database success\n"); } if (mysql_query(conn,sql_insert_data)) { //插入数据 printf("mysql_query insert data error %u: %s\n", mysql_errno(conn), mysql_error(conn)); return 1; } else{ printf("insert data success\n"); } result = mysql_query(conn,"select * from test.session"); if (result) { //查询数据 printf("mysql_query select data error %u: %s\n", mysql_errno(conn), mysql_error(conn)); return 1; } else{ printf("select data success\n"); mysqlresult = mysql_store_result(conn); if(mysqlresult){ numrow = mysql_num_rows(mysqlresult); numcol = mysql_num_fields(mysqlresult); for(x=0; x<numrow; x++) { mysqlrow = mysql_fetch_row(mysqlresult); for(y=0; y<numcol; y++) { printf("%-8s ",mysqlrow[y]); } printf("\n"); } } } mysql_free_result(mysqlresult); mysql_close(conn); return 0; }4、其他补充
命令行输入,安装完成即可:
sudo apt-get install build-essential