1、MySQL安裝及簡單設置
(1)安裝:在OSX系統下,可以使用萬能的“brew install”命令來進行安裝:brew isntall mysql(默認安裝最新版的MySQL)
(2)啟動:brew services start mysql
(3)修改密碼:update user set authentication_string = password('password'), password_expired = 'N', password_last_changed = now() where user = 'root';
-->flush privileges;(讓修改后的密碼生效)
(4)允許遠程訪問:update mysql.user set host = '%' where user = 'root';
2、MySQL Connector/C++安裝
(1)下載:MySQL Connector/C++源碼可以從這里下載
(2)安裝:解壓后將“include”目錄下的文件復制到“/usr/local/include”目錄下,“lib”目錄下的文件復制到“/usr/local/lib”目錄下即可
3、示例代碼(基於單例模式的懶漢模型)
CConnPool.h
/* * CConnPool.h * * Created on: Mar 15, 2018 * Author: root */ #ifndef SRC_CCONNPOOL_H_ #define SRC_CCONNPOOL_H_ #include <list> #include <string> #include <pthread.h> #include <mysql_connection.h> #include <mysql_driver.h> #include <cppconn/exception.h> #include <cppconn/driver.h> #include <cppconn/connection.h> #include <cppconn/resultset.h> #include <cppconn/prepared_statement.h> #include <cppconn/statement.h> using namespace sql; using namespace std; class CConnPool { public: ~CConnPool(); void InitConnpool(string url, string user, string password, int maxSize); Connection* GetConnection(); void ReleaseConnection(Connection* conn); static CConnPool *GetInstance(); private: CConnPool(); Connection*CreateConnection(); //創建一個連接 void InitConnection(int iInitialSize); //初始化數據庫連接池 void DestoryConnection(Connection *conn); //銷毀數據庫連接對象 void DestoryConnPool(); //銷毀數據庫連接池 CConnPool(string url, string user, string password, int maxSize); //構造方法 private: int curSize; //當前已建立的數據庫連接數量 int maxSize; //連接池中定義的最大數據庫連接數 string user; string password; string url; list<Connection*> connList; //連接池的容器隊列 STL list 雙向鏈表 pthread_mutex_t lock; //線程鎖 static CConnPool *connPool; Driver*driver; }; #endif /* SRC_CCONNPOOL_H_ */
CConnPool.cpp
/* * CConnPool.cpp * * Created on: Mar 15, 2018 * Author: root */ #include <stdexcept> #include <exception> #include <cstdio> #include "CConnPool.h" CConnPool *CConnPool::connPool = NULL; //CConnPool* CConnPool::connPool = new CConnPool(); CConnPool::CConnPool() { // TODO Auto-generated constructor stub } void CConnPool::InitConnpool(string url, string user, string password, int maxSize) { this->maxSize = maxSize; this->curSize = 0; this->user = user; this->password = password; this->url = url; try { this->driver = sql::mysql::get_driver_instance(); } catch (sql::SQLException &e) { perror("驅動連接出錯;\n"); } catch (std::runtime_error &e) { perror("運行出錯了\n"); } this->InitConnection(maxSize / 2); pthread_mutex_init(&lock, NULL); } CConnPool::CConnPool(string url, string user, string password, int maxSize) { this->maxSize = maxSize; this->curSize = 0; this->user = user; this->password = password; this->url = url; try { this->driver = sql::mysql::get_driver_instance(); } catch (sql::SQLException &e) { perror("驅動連接出錯;\n"); } catch (std::runtime_error &e) { perror("運行出錯了\n"); } this->InitConnection(maxSize / 2); pthread_mutex_init(&lock, NULL); } CConnPool *CConnPool::GetInstance() { if (connPool == NULL) connPool = new CConnPool("tcp://127.0.0.1:3306", "root", "123456", 10); return connPool; } void CConnPool::InitConnection(int num) { Connection *conn; pthread_mutex_lock(&lock); for (int i = 0; i < num; ++i) { conn = CreateConnection(); if (conn) { connList.push_back(conn); ++curSize; } else { perror("創建CONNECTION出錯"); } } pthread_mutex_unlock(&lock); } Connection *CConnPool::CreateConnection() { Connection *conn; try { conn = driver->connect(url, user, password); //建立連接 return conn; } catch (sql::SQLException &e) { perror(e.what()); return NULL; } catch (std::runtime_error &e) { perror(e.what()); return NULL; } } Connection *CConnPool::GetConnection() { Connection *conn; pthread_mutex_lock(&lock); if (connList.size() > 0) { conn = connList.front(); connList.pop_front(); if (conn->isClosed()) { delete conn; conn = CreateConnection(); } if (conn == NULL) --curSize; pthread_mutex_unlock(&lock); return conn; } else { if (curSize < maxSize) { conn = CreateConnection(); if (conn) { ++curSize; pthread_mutex_unlock(&lock); return conn; } else { pthread_mutex_unlock(&lock); return NULL; } } else { pthread_mutex_unlock(&lock); return NULL; } } } void CConnPool::ReleaseConnection(Connection *conn) { if (conn) { pthread_mutex_lock(&lock); connList.push_back(conn); pthread_mutex_unlock(&lock); } } CConnPool::~CConnPool() { this->DestoryConnPool(); } void CConnPool::DestoryConnPool() { list<Connection *>::iterator iter; pthread_mutex_lock(&lock); for (iter = connList.begin(); iter != connList.end(); ++iter) this->DestoryConnection(*iter); curSize = 0; connList.clear(); pthread_mutex_unlock(&lock); } void CConnPool::DestoryConnection(Connection *conn) { if (conn) { try { conn->close(); } catch (sql::SQLException &e) { perror(e.what()); } catch (std::exception &e) { perror(e.what()); } delete conn; } }
main.cpp
#include <iostream> #include <string> #include "CConnPool.h" using std::cout; using std::endl; using std::string; CConnPool *connpool = CConnPool::GetInstance(); int main(int argc, char *argv[]) { Connection *conn; Statement *state; ResultSet *result; conn = connpool->GetConnection(); state = conn->createStatement(); state->execute("use mysql"); result = state->executeQuery("select host,user from user"); while (result->next()) { try { string user = result->getString("user"); string host = result->getString("host"); cout << user << "@" << host << endl; } catch (sql::SQLException &e) { cout << e.what() << endl; } } delete result; delete state; connpool->ReleaseConnection(conn); getchar(); return 0; }