1. linux下安装及配置
1.1 编译connector-c
$ tar zxvf mysql-connector-c-6.1.9-src.tar.gz
$ cd mysql-connector-c-6.1.9-src
$ mkdir build
$ cd build
$ cmake ..
$ make
1.2 安装connector-c
$ make install
Install the project...
-- Install configuration: "RelWithDebInfo"
-- Installing: /usr/local/mysql/./COPYING
-- Installing: /usr/local/mysql/./README
-- Installing: /usr/local/mysql/docs/INFO_SRC
-- Installing: /usr/local/mysql/docs/INFO_BIN
-- Installing: /usr/local/mysql/docs/ChangeLog
-- Installing: /usr/local/mysql/include/mysql.h
-- Installing: /usr/local/mysql/include/mysql_com.h
-- Installing: /usr/local/mysql/include/my_command.h
-- Installing: /usr/local/mysql/include/mysql_time.h
-- Installing: /usr/local/mysql/include/my_list.h
-- Installing: /usr/local/mysql/include/my_alloc.h
-- Installing: /usr/local/mysql/include/typelib.h
-- Installing: /usr/local/mysql/include/binary_log_types.h
-- Installing: /usr/local/mysql/include/my_dbug.h
-- Installing: /usr/local/mysql/include/m_string.h
-- Installing: /usr/local/mysql/include/my_sys.h
-- Installing: /usr/local/mysql/include/my_xml.h
-- Installing: /usr/local/mysql/include/mysql_embed.h
-- Installing: /usr/local/mysql/include/my_thread.h
-- Installing: /usr/local/mysql/include/my_thread_local.h
-- Installing: /usr/local/mysql/include/decimal.h
-- Installing: /usr/local/mysql/include/errmsg.h
-- Installing: /usr/local/mysql/include/my_global.h
-- Installing: /usr/local/mysql/include/my_getopt.h
-- Installing: /usr/local/mysql/include/sslopt-longopts.h
-- Installing: /usr/local/mysql/include/my_dir.h
-- Installing: /usr/local/mysql/include/sslopt-vars.h
-- Installing: /usr/local/mysql/include/sslopt-case.h
-- Installing: /usr/local/mysql/include/sql_common.h
-- Installing: /usr/local/mysql/include/keycache.h
-- Installing: /usr/local/mysql/include/m_ctype.h
-- Installing: /usr/local/mysql/include/my_compiler.h
-- Installing: /usr/local/mysql/include/mysql_com_server.h
-- Installing: /usr/local/mysql/include/my_byteorder.h
-- Installing: /usr/local/mysql/include/byte_order_generic.h
-- Installing: /usr/local/mysql/include/byte_order_generic_x86.h
-- Installing: /usr/local/mysql/include/little_endian.h
-- Installing: /usr/local/mysql/include/big_endian.h
-- Installing: /usr/local/mysql/include/thr_cond.h
-- Installing: /usr/local/mysql/include/thr_mutex.h
-- Installing: /usr/local/mysql/include/thr_rwlock.h
-- Installing: /usr/local/mysql/include/mysql_version.h
-- Installing: /usr/local/mysql/include/my_config.h
-- Installing: /usr/local/mysql/include/mysqld_ername.h
-- Installing: /usr/local/mysql/include/mysqld_error.h
-- Installing: /usr/local/mysql/include/sql_state.h
-- Installing: /usr/local/mysql/include/mysql
-- Installing: /usr/local/mysql/include/mysql/service_mysql_alloc.h
-- Installing: /usr/local/mysql/include/mysql/service_my_snprintf.h
-- Installing: /usr/local/mysql/include/mysql/psi
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_mdl.h
-- Installing: /usr/local/mysql/include/mysql/psi/psi_memory.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_table.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_memory.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_idle.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_sp.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_statement.h
-- Installing: /usr/local/mysql/include/mysql/psi/psi_base.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_file.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_thread.h
-- Installing: /usr/local/mysql/include/mysql/psi/psi.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_ps.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_stage.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_transaction.h
-- Installing: /usr/local/mysql/include/mysql/psi/mysql_socket.h
-- Installing: /usr/local/mysql/include/mysql/plugin_trace.h
-- Installing: /usr/local/mysql/include/mysql/plugin_auth_common.h
-- Installing: /usr/local/mysql/include/mysql/client_plugin.h
-- Installing: /usr/local/mysql/include/mysql/client_authentication.h
-- Installing: /usr/local/mysql/include/mysql/mysql_lex_string.h
-- Installing: /usr/local/mysql/include/mysql/get_password.h
-- Installing: /usr/local/mysql/include/mysql/client_plugin.h.pp
-- Installing: /usr/local/mysql/lib/libmysqlclient.a
-- Installing: /usr/local/mysql/lib/libmysqlclient.so.18.4.
-- Installing: /usr/local/mysql/lib/libmysqlclient.so.18
-- Installing: /usr/local/mysql/lib/libmysqlclient.so
-- Installing: /usr/local/mysql/bin/my_print_defaults
-- Installing: /usr/local/mysql/bin/perror
-- Installing: /usr/local/mysql/bin/mysql_config
1.3 安装mysql
(1)解压tar包
$ tar zxvf mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
(2)添加用户与组
$ groupadd mysql
$ useradd -r -g mysql mysql
$ cd mysql-5.5.62-linux-glibc2.12-x86_64
$ chown -R mysql:mysql ./
(3)安装数据库
$ cd mysql-5.5.62-linux-glibc2.12-x86_64
$ ./scripts/mysql_install_db --user=mysql
(4)配置文件
$ cd support-files
$ cp my-large.cnf /etc/my.cnf
$ cp mysql.server /etc/init.d/mysql
$ vim /etc/init.d/mysql
修改文件中的两个变更值
basedir=/root/zhn/software/mysql-5.5.62-linux-glibc2.12-x86_64
datadir=/root/zhn/software/mysql-5.5.62-linux-glibc2.12-x86_64/data
(5)配置环境变量
vim /etc/profile
export MYSQL_HOME=/root/zhn/software/mysql-5.5.62-linux-glibc2.12-x86_64
export PATH=$PATH:$MYSQL_HOME/bin
source /etc/profile
(6)添加自启动服务
chkconfig --add mysql
chkconfig mysql on
(7)启动mysql
$ service mysql start
(8)登录mysql及改密码与配置远程访问
修改root用户密码(密码暂时是mdsoss)
$ mysqladmin -u root password 'mdsoss'
登录mysql,需要输入密码
$ mysql -u root -p
允许root用户远程访问
$ mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mdsoss' WITH GRANT OPTION;
刷新权限
$ mysql> FLUSH PRIVILEGES;
(9)关闭,查看mysql
$ service mysql stop
$ service mysql status
1.4 c++连接测试
通过命令创建database及table(sql语句结尾带分号)
$ mysql -u root -p
Enter password: mdsoss
$ mysql> create database test_db;
切换到test_db数据库中
$ mysql> use test_db;
创建一个叫test的表
$ mysql> create table test(name varchar(255),num int(10));
显示刚才创建的表信息
$ mysql> show create table test;
$ 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; }
- 编译:g++ mysql.cpp `mysql_config --cflags --libs` -o test
- 执行:./test
- 通过命令查询table
$ mysql> select * from test; +———+————+ | name | num | +———+————+ | user | 123456 | +———+————+
<a name="r2ztV"></a>
## 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的密码 ```