1. install mysql-server, install the mysql-server-5.1 under Ubuntu10.04, The mysql-client_5.1sudoapt-getinstallmysql-server-5.12.CAPIs is automatically installed and included in the mysqlclient library file with the MySQL source code, for connecting to the database and performing database queries, therefore, libmysqlclient-dev must be installed.
1. Install mysql-server, install mysql-server-5.1 under Ubuntu 10.04, will automatically install mysql-client_5.1
Sudo apt-get install mysql-server-5.1
2. C APIs is included in the mysqlclient library file and is released with the MySQL source code to connect to the database and execute database queries. Therefore, libmysqlclient-dev must be installed.
Sudo apt-get install libmysqlclient-dev
If the installation is successful, the related files are as follows:
The header file is in the/usr/include/mysql directory;
The library file is in the/usr/lib/mysql and/usr/lib directories.
3. Start and close mysql
Start mysql: sudo start mysql
Close mysql: sudo stop mysql
Use ps aux | grep mysql to view mysql startup status
The ps command is used to view information about processes running in the current system. Command Format: ps [Option]
Common parameters:
-A: displays all processes in the system, including other user processes.
-E: displays information about all processes.
-F: displays all process information.
-L display process information in long format
-R only displays running programs
-U displays process information in user format
-X: displays process information on all terminals.
The most common method is ps aux, and then a pipeline symbol is used to direct to grep to find a specific process and then operate on the specific process. For example, the previously used ps aux | grep mysql
4. When you connect to a database, you need library files, static library files, dynamic library files, static library files with the same name, and dynamic library files. How to distinguish them:
. O,. a,. so,. lo,. la suffix file meaning
. O: compiled target file
. A: static library, in fact, is to pack several o files
. So: dynamic link library (Shared Library)
. Lo: the target file compiled using libtool is actually added to the o file.
. La: library files compiled using libtool are actually text files that record information about dynamic and static libraries with the same name.
La is used only when libtool is used to compile applications or libraries. That is, the developer only specifies the la file. As for the final link a or so, it is determined by libtool, and la cannot be used for mini programs that do not use libtool.
5. Connect to MySql
A. Connect to MYSQL on the local machine:
First, open the terminal window, type the mysql-u root-p command, and press enter to prompt you to enter the password. If MYSQL has just been installed, the Super User root has no password, press enter to enter
To MYSQL, MYSQL prompt is: mysql>
B. Change the password. Format: mysqladmin-u username-p old password New password
Example 1: Add a password ab12 to the root user. First open the terminal window, and then type the following command: mysqladmin-uroot password ab12. (Note: because the root has no password at the beginning, the old-p password can be omitted ).
Example 2: Change the root password to djg345. Mysqladmin-uroot-pab12 password djg345 ).
C. exit MYSQL command: exit
6. Add new users (Note: Unlike the above, the following commands in the MYSQL environment are followed by a semicolon as the command Terminator)
Format: grant select on database. * to username @ login host identified by "password"
Example 1: Add a user named "test1" with the password "abc" so that the user can log on to any host and have the permission to query, insert, modify, and delete all databases.
First, use the root user to connect to MYSQL, and then type the following command: grant select, insert, update, delete on *. * to test1 @ "%" Identified by "abc"; however, the added users in Example 1 are very dangerous,
If someone knows the password of test1, he can log on to your mysql database on any computer on the internet and do whatever he wants for your data, for the solution, see Example 2.
Example 2: Add a user named "test2" with the password "abc" so that the user can only log on to localhost, you can also query, insert, modify, and delete the database mydb (localhost refers to the local host, that is, the host where the MYSQL database is located ),
In this way, the user knows the password of test2 and cannot directly access the database from the internet. He can only access the database through the web page on the MYSQL host.
Grant select, insert, update, delete on mydb. * to test2 @ localhost identified by "abc"; if you do not want test2 to have a password, run another command to cancel the password.
Grant select, insert, update, delete on mydb. * to test2 @ localhost identified by "";
6. Display command: (Note: Let's take a look at the database operations in MYSQL. Note: you must first log on to MYSQL. The following operations are performed at the MYSQL prompt and each command ends with a semicolon ).
A. display the Database List: show databases; two databases: mysql and test at the beginning. The mysql database contains the MYSQL system information. We change the password and add new users to use this database for operations.
B. display the data tables in the database: use mysql; show tables;
C. display the data table structure: describe table name;
D. database creation: create database name;
E. create a table: use Database Name; create table Name (field setting list );
F. Delete databases and tables: drop database name; drop table name;
G. Clear the records in the Table: delete from table name;
H. Display records in the Table: select * from table name;
7. Write c code, file name: mysql. c
// Gcc mysql. c-o test-I/usr/include/mysql-L/usr/lib-lmysqlclient
# Include
# Include
# Include
Int main ()
{
MYSQL mysql;
MYSQL_RES * res;
MYSQL_ROW row;
Char SQL [50];
Int t;
Sprintf (SQL, "select * from Student"); // defines the executed SQL statement. Here is the Student table in the test database.
Mysql_init (& mysql); // initialize the mysql Structure
If (! Mysql_real_connect (& mysql, "localhost", "root", "123456", "test", 3306, NULL, 0 ))
Printf ("\ n database connection error: % s \ n", mysql_error (& mysql ));
Else
Printf ("\ n database connection successful! \ N ");
T = mysql_real_query (& mysql, SQL, (unsigned int) strlen (SQL); // execute the defined SQL statement on the server
If (t)
Printf ("query statement error: % s \ n", mysql_error (& mysql ));
Else
{
Res = mysql_store_result (& mysql );
While (row = mysql_fetch_row (res ))
{
For (t = 0; t <mysql_num_fields (res); t ++)
Printf ("% s", row [t]);
Printf ("\ n ");
}
}
Printf ("Release the result set space... \ n ");
Mysql_free_result (res );
Mysql_close (& mysql); // release the database
Printf ("SQL errror! % S \ n ", mysql_error (& mysql); // print the error code if an error occurs
Return 0;
}
Compile: gcc-c mysql. c-I/usr/include/mysql/here-I specifies the header file directory-c is compiled into the target code
Connection: gcc mysql. o-o test-L/usr/lib/-lmysqlclient here-L specifies the library file path. This parameter-lmysqlclient is because the c api code is included in the mysqlclient library, it is because of this that C Programs are allowed to access the database.
For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2