本文為精簡版,核心步驟與 ID 171935 相同,僅保留關鍵指令與驗證碼。
sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install libmysqlclient-dev
sudo netstat -tap | grep mysql # 出現 LISTEN 即成功
mysql -u root -p
SHOW DATABASES;
USE mysql;
SHOW TABLES;
#include
#include
#include
int main() {
MYSQL *conn = mysql_init(NULL);
if (!mysql_real_connect(conn, "localhost", "root", "mima", "mysql", 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "SHOW TABLES")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
MYSQL_RES *res = mysql_use_result(conn);
MYSQL_ROW row;
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("%s \n", row[0]);
}
mysql_free_result(res);
mysql_close(conn);
printf("finish! \n");
return 0;
}
g++ -Wall mysql_test.cpp -o mysql_test -lmysqlclient
./mysql_test