一、安装MySQL数据库

  1.查询可用包

apt-cache search mysql-server

  2.安装mysql-server

sudo apt-get update
sudo apt-get install mysql-server

  3.设定初始化配置

sudo mysql_secure_installation

  4.查看MySQL状态

systemctl status mysql.service

二、安装MySQL工具

  1.下载mysql workbench

    workbench连接可能出现"access denied for  user 'root@localhost'问题的解决方法

  2. 安装MySQL C API

apt-cache search libmysqlclinet
sudo apt-get update
sudo apt-get install libmysqlclient-dev

  3.测试C API

#include<mysql/mysql.h>
#include<stdio.h>
#include<stdlib.h>

int main() {
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;

    char *server = "localhost";
    char *user = "root";
    char *password = "zjq588";
    char *database = "mysql";

    conn = mysql_init(NULL);

    /* Connect to database */
    if (!mysql_real_connect(conn, server,
        user, password, database, 0, NULL, 0)) {
    fprintf(stderr, "%s\n", mysql_error(conn));
    exit(1);
    }

    /* send SQL query */
    if (mysql_query(conn, "show tables")) {
    fprintf(stderr, "%s\n", mysql_error(conn));
    exit(1);
    }

    res = mysql_use_result(conn);

    /* output table name */
    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) != NULL)
    printf("%s \n", row[0]);

    /* close connection */
    mysql_free_result(res);
    mysql_close(conn);

    return 0;
}

  编译:

gcc connect_test.cc -lmysqlclient -o connect_test

  执行:

MySQL Tables in mysql database:
columns_priv
db
engine_cost
event
func
general_log
gtid_executed
help_category
help_keyword
help_relation
help_topic
innodb_index_stats
innodb_table_stats
ndb_binlog_index
plugin
proc
procs_priv
proxies_priv
server_cost
servers
slave_master_info
slave_relay_log_info
slave_worker_info
slow_log
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user

  

       

  

12-25 10:49