下载tar包到目录dl/
mysql-5.1.73.tar.gz
链接:https://pan.baidu.com/s/1HEAotvd_8SM4IEr03-DeRw 密码:1yd5
ncurses-5.9.tar.gz
链接:https://pan.baidu.com/s/1rLgoKY9BLGEplIeI8rCjuQ 密码:c7wp
1.cd /work/openwrt-hiwooya-stable-master/
2.make menuconfig
搜索mysql,找到后打上“*”号
mysql-server的附属包有:
打上“*”之后开始编译。
编译成功后烧进路由器,运行路由器看到/usr/bin下有mysqld这个服务,说明我们已经成功编译mysql进去了。
Package mysql-server is missing dependencies for the following libraries:
libncurses.so.5
未解决。。。
修改配置文件/etc/my.conf,找到
datadir =/mnt/data/mysql
tmpdir =/mnt/data/tmp
更改为你想存放数据库的地方:
datadir =/mnt/sda3/data/mysql #将数据库放置到U盘对应分区上存储
tmpdir =/mnt/sda3/data/tmp #将数据库临时文件夹存放到U盘对应分区上
找到bind-address属性
bind-address =127.0.0.1
更改为:
bind-address =0.0.0.0
修改的目的:允许远程访问。
mysql_install_db --force
/etc/init.d/mysqld start
mysqladmin -u root password "1234"
killall mysqld
mysql -u root -p
官方文档:https://dev.mysql.com/doc/refman/5.7/en/c-api.html
#include <mysql/mysql.h>
MYSQL *mysql_init(MYSQL * mysql);
int mysql_options(MYSQL *mysql, enum mysql_option option, const char *arg);
MYSQL * STDCALL mysql_real_connect(MYSQL *mysql,
const char *host, //MySQL的IP,远程登录输入远程登录的IP地址
const char *user, //登录用户
const char *passwd, //登录密码
const char *db, //使用的数据库名称
unsigned int port, //MySQL端口
const char *unix_socket, //是否使用socket机制登录,NULL:否
unsigned long clientflag); //连接标志,通常为0
/****************
*SQLBase.h
*****************/
class CSQLBase
{
public:
CSQLBase(string strHost = "127.0.0.1", string strUser = "root", string strPwd = "1234", string strDb = "project");
virtual ~CSQLBase();
bool connect(string strHost = "127.0.0.1", string strUser = "root", string strPwd = "1234", string strDb = "project");
void close(void) ;
bool query(string strSql) const;
bool fetchRow(void (*func)(char *row[], void *pAgr), void *pAgr) const;
protected:
private:
MYSQL *m_pMysql;
string m_strHost;
string m_strUser;
string m_strPassword;
string m_strDatabase;
};
/****************
*SQLBase.cpp
*****************/
#include "SQLBase.h"
CSQLBase::CSQLBase(string strHost, string strUser, string strPwd, string strDb)
{
this->m_pMysql = mysql_init(nullptr);
if(this->m_pMysql)
{
this->connect(strHost, strUser, strPwd, strDb);
}
}
CSQLBase::~CSQLBase()
{
this->close();
}
bool CSQLBase::connect(string strHost, string strUser, string strPwd, string strDb)
{
this->m_strHost = strHost;
this->m_strUser = strUser;
this->m_strPassword = strPwd;
this->m_strDatabase = strDb;
if(mysql_real_connect(this->m_pMysql, strHost.c_str(), strUser.c_str(), strPwd.c_str(), strDb.c_str(), 0, nullptr, 0) == nullptr)
{
return false;
}
mysql_set_character_set(this->m_pMysql, "utf8");
return true;
}
void CSQLBase::close(void)
{
if(this->m_pMysql)
{
mysql_close(this->m_pMysql);
}
}
int STDCALL mysql_query(MYSQL *mysql, //MySQL操作结构体
const char *q); //操作命令
const char *mysql_error(MYSQL *mysql);
int mysql_affected_rows(MYSQL *mysql);
try
{
…………………
string sql = "SELECT * from zx_users;";
if (mysql_query(&mysql, sql.c_str()))
{
string err_string = mysql_error(&mysql);
if(err_string.empty())
throw string("MySQL query is error!");
else
throw err_string;
}
………………
}
catch (string &error_msg)
{
cout << error_msg << endl;
}
catch (...)
{
cout << "MySQL operation is error!" << endl;
}
mysql_close(&mysql);
void mysql_close(MYSQL *connection);
MYSQL_RES *mysql_use_result(MYSQL *mysql);
MYSQL_RES *mysql_store_result(MYSQL *mysql);
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
try
{
…………………………………
//MySQL命令
string sql = "SELECT * from zx_users;";
if (mysql_query(&mysql, sql.c_str()))
{
string err_string = mysql_error(&mysql);
if(err_string.empty())
throw string("MySQL query is error!");
else
throw err_string;
}
cout << "MySQL : " << sql << endl;
MYSQL_RES *result = mysql_store_result(&mysql);
if (!result)
throw string("MySQL not result!");
//获取字段数量
int num_fields = mysql_num_fields(result);
if(0 == num_fields)
throw string("MySQL fields number is 0!");
//获取字段名
MYSQL_FIELD *fields = mysql_fetch_fields(result);
if (!fields)
throw string("MySQL fields fetch is error!");
for (int i = 0; i < num_fields; i++)
{
cout << " " << fields[i].name;
}
cout << endl;
while (MYSQL_ROW row = mysql_fetch_row(result)) //获取整条数据内容
{
for (int i = 0; i < num_fields; i++)
{
if (NULL == row[i])
{
cout << " NULL";
}
else
{
cout << " " << row[i];
}
}
cout << endl;
}
mysql_free_result(result);
cout << "MySQL is OK." << endl;
}
catch (string &error_msg)
{
cout << error_msg << endl;
}
catch (...)
{
cout << "MySQL operation is error!" << endl;
}