当前位置:   article > 正文

Ubuntu12.04安装配置MySQL开发环境_虚拟机ubuntu12.04中设置mysql环境变量

虚拟机ubuntu12.04中设置mysql环境变量

首先安装mysql服务器和客户端:

$sudo apt-get install mysql-server mysql-client
期间可能需要设置root密码。根据需要自行设定。

接着,MySQL开发包:

$sudo apt-get install libmysqlclient-dev
首次登录数据库使用root:

$mysql -h localhost -u root -p
输入密码登录后,创建一个数据库n2nvpn_db(含表myclass)和用户eta(密码为etapublic,可远程登录),并使eta对n2nvpn_db有增删查改的权限:

  1. mysql>create database n2nvpn_db;
  2. mysql>create table myclass(id int, name char(20));
  3. mysql>grant select,insert,update,delete on n2nvpn_db.* to eta@"%" Identified by "etapublic";
  4. mysql>flush privileges;

为了让MySQL支持远程登录,需要修改配置文件/etc/mysql/my.cnf。将  "bind 127.0.0.1"这行用#号注释掉:

#bind 127.0.0.1

保存退出。重启mysql:

$sudo /etc/init.d/mysql restart

最后,写一段小程序来测试:

  1. /*
  2. this is a test for mysql.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <mysql/mysql.h>
  8. #define BUFFLEN 1024
  9. #define INSCNT 4
  10. int main(void)
  11. {
  12. MYSQL mysql;
  13. char sqlBuff[BUFFLEN]="";
  14. char *bigThree[] = {"Stalin","Roosevelt","Churchill","Obama"};
  15. mysql_init(&mysql);
  16. if(mysql_real_connect(&mysql, "127.0.0.1",
  17. "eta", "etapublic", "n2nvpn_db", MYSQL_PORT, NULL, 0)==NULL)
  18. {
  19. printf("cannot connect to MySql server!\n");
  20. exit(1);
  21. }
  22. /* 增 */
  23. memset(sqlBuff, 0x00, BUFFLEN);
  24. int i;
  25. for(i=0; i<INSCNT; i++)
  26. {
  27. snprintf(sqlBuff, BUFFLEN, "insert into myclass values(1, '%s');", bigThree[i]);
  28. if(mysql_real_query(&mysql, sqlBuff, strlen(sqlBuff)))
  29. {
  30. printf("insert %s err!\n", bigThree[i]);
  31. }
  32. }
  33. /* 删 */
  34. memset(sqlBuff, 0x00, BUFFLEN);
  35. snprintf(sqlBuff, BUFFLEN, "delete from myclass where name='%s';", bigThree[3]); //删除Obama.
  36. if(mysql_real_query(&mysql, sqlBuff, strlen(sqlBuff)))
  37. {
  38. printf("delete %s err!\n", bigThree[3]);
  39. }
  40. /* 查 */
  41. memset(sqlBuff, 0x00, BUFFLEN);
  42. snprintf(sqlBuff, BUFFLEN, "select * from myclass;");
  43. if(mysql_real_query(&mysql, sqlBuff, strlen(sqlBuff)))
  44. {
  45. printf("select err!\n");
  46. }
  47. char *my_res = mysql_store_result(&mysql);
  48. printf("QUERY:[%s]", my_res);
  49. /* 改 */
  50. memset(sqlBuff, 0x00, BUFFLEN);
  51. snprintf(sqlBuff, BUFFLEN, "update myclass set name='likai' where name='%s';", bigThree[1]); //修改Roosevelt.
  52. if(mysql_real_query(&mysql, sqlBuff, strlen(sqlBuff)))
  53. {
  54. printf("update %s err!\n", bigThree[1]);
  55. }
  56. }

编译运行(libmysqlclient.so的位置可能不尽相同):

gcc testMysql.c -o testMysql -L/usr/lib/i386-linux-gnu/ -lmysqlclient -g
./testMysql
然后登录mysql验证可以看到程序成功执行了增删查改。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/186135
推荐阅读
相关标签
  

闽ICP备14008679号