1. linux下安装及配置

1.1 编译connector-c

  1. $ tar zxvf mysql-connector-c-6.1.9-src.tar.gz
  2. $ cd mysql-connector-c-6.1.9-src
  3. $ mkdir build
  4. $ cd build
  5. $ cmake ..
  6. $ make

1.2 安装connector-c

  1. $ make install
  2. Install the project...
  3. -- Install configuration: "RelWithDebInfo"
  4. -- Installing: /usr/local/mysql/./COPYING
  5. -- Installing: /usr/local/mysql/./README
  6. -- Installing: /usr/local/mysql/docs/INFO_SRC
  7. -- Installing: /usr/local/mysql/docs/INFO_BIN
  8. -- Installing: /usr/local/mysql/docs/ChangeLog
  9. -- Installing: /usr/local/mysql/include/mysql.h
  10. -- Installing: /usr/local/mysql/include/mysql_com.h
  11. -- Installing: /usr/local/mysql/include/my_command.h
  12. -- Installing: /usr/local/mysql/include/mysql_time.h
  13. -- Installing: /usr/local/mysql/include/my_list.h
  14. -- Installing: /usr/local/mysql/include/my_alloc.h
  15. -- Installing: /usr/local/mysql/include/typelib.h
  16. -- Installing: /usr/local/mysql/include/binary_log_types.h
  17. -- Installing: /usr/local/mysql/include/my_dbug.h
  18. -- Installing: /usr/local/mysql/include/m_string.h
  19. -- Installing: /usr/local/mysql/include/my_sys.h
  20. -- Installing: /usr/local/mysql/include/my_xml.h
  21. -- Installing: /usr/local/mysql/include/mysql_embed.h
  22. -- Installing: /usr/local/mysql/include/my_thread.h
  23. -- Installing: /usr/local/mysql/include/my_thread_local.h
  24. -- Installing: /usr/local/mysql/include/decimal.h
  25. -- Installing: /usr/local/mysql/include/errmsg.h
  26. -- Installing: /usr/local/mysql/include/my_global.h
  27. -- Installing: /usr/local/mysql/include/my_getopt.h
  28. -- Installing: /usr/local/mysql/include/sslopt-longopts.h
  29. -- Installing: /usr/local/mysql/include/my_dir.h
  30. -- Installing: /usr/local/mysql/include/sslopt-vars.h
  31. -- Installing: /usr/local/mysql/include/sslopt-case.h
  32. -- Installing: /usr/local/mysql/include/sql_common.h
  33. -- Installing: /usr/local/mysql/include/keycache.h
  34. -- Installing: /usr/local/mysql/include/m_ctype.h
  35. -- Installing: /usr/local/mysql/include/my_compiler.h
  36. -- Installing: /usr/local/mysql/include/mysql_com_server.h
  37. -- Installing: /usr/local/mysql/include/my_byteorder.h
  38. -- Installing: /usr/local/mysql/include/byte_order_generic.h
  39. -- Installing: /usr/local/mysql/include/byte_order_generic_x86.h
  40. -- Installing: /usr/local/mysql/include/little_endian.h
  41. -- Installing: /usr/local/mysql/include/big_endian.h
  42. -- Installing: /usr/local/mysql/include/thr_cond.h
  43. -- Installing: /usr/local/mysql/include/thr_mutex.h
  44. -- Installing: /usr/local/mysql/include/thr_rwlock.h
  45. -- Installing: /usr/local/mysql/include/mysql_version.h
  46. -- Installing: /usr/local/mysql/include/my_config.h
  47. -- Installing: /usr/local/mysql/include/mysqld_ername.h
  48. -- Installing: /usr/local/mysql/include/mysqld_error.h
  49. -- Installing: /usr/local/mysql/include/sql_state.h
  50. -- Installing: /usr/local/mysql/include/mysql
  51. -- Installing: /usr/local/mysql/include/mysql/service_mysql_alloc.h
  52. -- Installing: /usr/local/mysql/include/mysql/service_my_snprintf.h
  53. -- Installing: /usr/local/mysql/include/mysql/psi
  54. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_mdl.h
  55. -- Installing: /usr/local/mysql/include/mysql/psi/psi_memory.h
  56. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_table.h
  57. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_memory.h
  58. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_idle.h
  59. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_sp.h
  60. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_statement.h
  61. -- Installing: /usr/local/mysql/include/mysql/psi/psi_base.h
  62. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_file.h
  63. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_thread.h
  64. -- Installing: /usr/local/mysql/include/mysql/psi/psi.h
  65. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_ps.h
  66. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_stage.h
  67. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_transaction.h
  68. -- Installing: /usr/local/mysql/include/mysql/psi/mysql_socket.h
  69. -- Installing: /usr/local/mysql/include/mysql/plugin_trace.h
  70. -- Installing: /usr/local/mysql/include/mysql/plugin_auth_common.h
  71. -- Installing: /usr/local/mysql/include/mysql/client_plugin.h
  72. -- Installing: /usr/local/mysql/include/mysql/client_authentication.h
  73. -- Installing: /usr/local/mysql/include/mysql/mysql_lex_string.h
  74. -- Installing: /usr/local/mysql/include/mysql/get_password.h
  75. -- Installing: /usr/local/mysql/include/mysql/client_plugin.h.pp
  76. -- Installing: /usr/local/mysql/lib/libmysqlclient.a
  77. -- Installing: /usr/local/mysql/lib/libmysqlclient.so.18.4.
  78. -- Installing: /usr/local/mysql/lib/libmysqlclient.so.18
  79. -- Installing: /usr/local/mysql/lib/libmysqlclient.so
  80. -- Installing: /usr/local/mysql/bin/my_print_defaults
  81. -- Installing: /usr/local/mysql/bin/perror
  82. -- Installing: /usr/local/mysql/bin/mysql_config

1.3 安装mysql

  1. (1)解压tar
  2. $ tar zxvf mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
  3. (2)添加用户与组
  4. $ groupadd mysql
  5. $ useradd -r -g mysql mysql
  6. $ cd mysql-5.5.62-linux-glibc2.12-x86_64
  7. $ chown -R mysql:mysql ./
  8. (3)安装数据库
  9. $ cd mysql-5.5.62-linux-glibc2.12-x86_64
  10. $ ./scripts/mysql_install_db --user=mysql
  11. (4)配置文件
  12. $ cd support-files
  13. $ cp my-large.cnf /etc/my.cnf
  14. $ cp mysql.server /etc/init.d/mysql
  15. $ vim /etc/init.d/mysql
  16. 修改文件中的两个变更值
  17. basedir=/root/zhn/software/mysql-5.5.62-linux-glibc2.12-x86_64
  18. datadir=/root/zhn/software/mysql-5.5.62-linux-glibc2.12-x86_64/data
  19. (5)配置环境变量
  20. vim /etc/profile
  21. export MYSQL_HOME=/root/zhn/software/mysql-5.5.62-linux-glibc2.12-x86_64
  22. export PATH=$PATH:$MYSQL_HOME/bin
  23. source /etc/profile
  24. (6)添加自启动服务
  25. chkconfig --add mysql
  26. chkconfig mysql on
  27. (7)启动mysql
  28. $ service mysql start
  29. (8)登录mysql及改密码与配置远程访问
  30. 修改root用户密码(密码暂时是mdsoss)
  31. $ mysqladmin -u root password 'mdsoss'
  32. 登录mysql,需要输入密码
  33. $ mysql -u root -p
  34. 允许root用户远程访问
  35. $ mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mdsoss' WITH GRANT OPTION;
  36. 刷新权限
  37. $ mysql> FLUSH PRIVILEGES;
  38. (9)关闭,查看mysql
  39. $ service mysql stop
  40. $ service mysql status

1.4 c++连接测试

  • 通过命令创建database及table(sql语句结尾带分号)

    1. $ mysql -u root -p
    2. Enter password: mdsoss
    3. $ mysql> create database test_db;
    4. 切换到test_db数据库中
    5. $ mysql> use test_db;
    6. 创建一个叫test的表
    7. $ mysql> create table test(name varchar(255),num int(10));
    8. 显示刚才创建的表信息
    9. $ mysql> show create table test;
    10. $ mysql> select * from test;
  • 测试代码 ```

    include

    include

int main(int argc,char *argv[]) { MYSQL conn; int res; mysql_init(&conn); if(mysql_real_connect(&conn,”localhost”,”root”,”mdsoss”,”test_db”,0,NULL,CLIENT_FOUND_ROWS)) //“root”:数据库管理员 “mdsoss”:root密码 “test_db”:数据库的名字 { printf(“connect success!\n”); res=mysql_query(&conn,”insert into test values(‘user’,’123456’)”); if(res) { printf(“error\n”); } else { printf(“OK\n”); } mysql_close(&conn); } return 0; }

  1. - 编译:g++ mysql.cpp `mysql_config --cflags --libs` -o test
  2. - 执行:./test
  3. - 通过命令查询table

$ mysql> select * from test; +———+————+ | name | num | +———+————+ | user | 123456 | +———+————+

  1. <a name="r2ztV"></a>
  2. ## 1.5 navicat跳转配置

Securt-CRT配置 比如mysql在192.168.1.38上 (1)右键配置,选择Properties (2)选择Connection->Port Forwarding->Add… (3)Name:mysql Local:Port->7979 Remote:Hostname->192.168.1.38 Port->3306

Navicat配置 (1)主机:127.0.0.1 端口:7979 用户名:root 密码:mdsoss note:用户名/密码是msyql的密码 ```

2. 参考文献