我已经从这里下载了c ++ mysql连接器(版本:8.3.0):mysql连接器c ++
我还在linux中安装了该库:libmysqlcppconn-dev
我安装了xampp。我可以通过localhost访问phpmyadmin,并且可以访问mysql:
sudo mysql -u root -p
我给了 root 用户一个密码并刷新了权限。
我有这个示例代码:
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' »
AS _message'..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "the_password");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column data by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " »
<< __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
但是我有这个错误:
# ERR: Access denied for user 'root'@'localhost' (using password: YES) (MySQL error code: 1045, SQLState: 28000 )
实际上,我真正想要的是与一个名为 admin 的用户连接,但我不能这样做:
sudo mysql -u admin -p
,因为我有错误:ERROR 1045 (28000): Access denied for user 'admin'@'localhost' (using password: YES)
。但是 admin 拥有所有权限,并且已分配主机 localhost。
另一种连接方式是:
mysqlx::Session session("tcp://127.0.0.1:3306", "root", "the password");
mysqlx::Schema schema = session.getSchema("test");
但是也无法连接。
我该怎么做?
提前致谢
我看到了这个链接:MYSQL用户,如果我输入地址127.0.0.1而不是本地主机,它就会连接