Simple operation of installing MySQL in Ubuntu

Keywords: MySQL Database sudo Ubuntu

Installing mysql on ubuntu is very simple and requires only a few commands.

 sudo apt-get install mysql-server mysql-client // mount this database
 sudo apt-get instal libmysqlclient-dev       //Package C language API for database installation

In the installation process, you will be prompted to set the password or something. Don't forget to set it up. After installation, you can use the following commands to check whether the installation is successful.

 sudo netstat -tap | grep mysql

After checking through the above commands, if you see that the socket with mysql is in listen state, the installation is successful.

To login to mysql database, you can use the following commands:

 mysql -u root -p 

- u denotes the user name to choose to log in, and - p denotes the user password to log in. After the above command is entered, the user will be prompted to enter the password. At this time, the user can log in to mysql by entering the password.
Then you can view the current database by showing databases.
We select the mysql database to perform the next step, using the use mysql command to display the form of the current database: show tables
Write a simple program to access the database and implement the show tables function:

#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[] = "root";
    char database[] = "mysql";
    conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0)) 
        fprintf(stderr, "%s\n", mysql_error(conn));
    if (mysql_query(conn, "show tables")) 
        fprintf(stderr, "%s\n", mysql_error(conn));
    res = mysql_use_result(conn);
    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) != NULL)
        printf("%s \n", row[0]);
    printf("finish! \n");
    return 0;

When compiling the code, you need to link the library of mysql, which can be compiled as follows:

  g++ -Wall mysql_test.cpp -o mysql_test -lmsqlclient

Then run the compiled code:

Posted by rcatal02 on Wed, 09 Oct 2019 04:05:12 -0700