新聞中心
MySQL是一種流行的開(kāi)源關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)。在Linux平臺(tái)上,利用MySQL進(jìn)行數(shù)據(jù)庫(kù)管理和操作需要先學(xué)習(xí)MySQL的開(kāi)發(fā)語(yǔ)言和API接口。在這個(gè)過(guò)程中,MySQL頭文件就顯得尤為重要。MySQL頭文件包含了MySQL的各種庫(kù)和函數(shù)的聲明和定義,使得開(kāi)發(fā)者可以利用這些庫(kù)和函數(shù)輕松地訪問(wèn)數(shù)據(jù)庫(kù)。

目前創(chuàng)新互聯(lián)公司已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、清豐網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
MySQL頭文件的目錄結(jié)構(gòu)
MySQL頭文件主要位于MySQL安裝目錄下的include目錄中,包括:
– mysql.h: 這個(gè)頭文件包含了所有MySQL API的定義和聲明,包括MySQL連接、查詢(xún)、數(shù)據(jù)類(lèi)型等所有基本操作。
– mysql_time.h: 這個(gè)頭文件主要定義了MySQL中日期時(shí)間相關(guān)的操作。
– mysql_version.h: 這個(gè)頭文件用于獲取MySQL版本信息。
– my_global.h: 這個(gè)頭文件被廣泛調(diào)用,包括了大量公共結(jié)構(gòu)和函數(shù)定義,是開(kāi)發(fā)MySQL應(yīng)用時(shí)的基礎(chǔ)。
– my_sys.h: 這個(gè)頭文件包含了MySQL內(nèi)部的系統(tǒng)函數(shù),對(duì)于MySQL的擴(kuò)展以及對(duì)內(nèi)部函數(shù)的操作都有很大幫助。
– sql_common.h: 這個(gè)頭文件主要包括SQL的一些基礎(chǔ)定義和基本操作定義。
MySQL頭文件的使用
在Linux系統(tǒng)上,要使用MySQL頭文件,需先在c或c++代碼中包含這些頭文件。例如:
#include
#include
這些頭文件都是以C語(yǔ)言編寫(xiě)的,所以在使用時(shí)應(yīng)該使用C編譯器來(lái)編譯程序。
MySQL頭文件的相關(guān)函數(shù)
MySQL頭文件中包含了大量的函數(shù),這里只介紹一部分常用函數(shù)。
1. mysql_init
作用:初始化一個(gè)MYSQL結(jié)構(gòu)體。
定義:MYSQL *mysql_init(MYSQL *mysql);
參數(shù):mysql:MYSQL結(jié)構(gòu)指針。
使用例子:
MYSQL mysql;
mysql_init(&mysql);
2. mysql_real_connect
作用:連接MySQL服務(wù)器。
定義:MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user,
const char *passwd, const char *db, unsigned int port, const char *unix_socket,
unsigned long client_flag);
參數(shù):
mysql:通過(guò)mysql_init()初始化的MYSQL結(jié)構(gòu)指針。
host:MySQL連接的主機(jī)名或IP地址。
user:MySQL連接的用戶(hù)名。
passwd:MySQL連接的密碼。
db:要連接的MySQL數(shù)據(jù)庫(kù)名。
port:MySQL服務(wù)器端口號(hào)。
unix_socket:連接MySQL服務(wù)器的Unix套接字。當(dāng)指定了該參數(shù)后,host、port將被忽略。
client_flag:連接MySQL服務(wù)器時(shí)可以指定各種啟動(dòng)參數(shù)。
返回值:連接成功返回MYSQL結(jié)構(gòu)指針,失敗返回NULL。
使用例子:
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, “l(fā)ocalhost”, “root”, “password”, “testdb”, 0, NULL, 0);
3. mysql_query
作用:執(zhí)行SQL查詢(xún)語(yǔ)句。
定義:int mysql_query(MYSQL *mysql, const char *stmt_str);
參數(shù):
mysql:通過(guò)mysql_init()初始化的MYSQL結(jié)構(gòu)指針。
stmt_str:SQL語(yǔ)句。
返回值:執(zhí)行成功返回0, 失敗返回非0值。
使用例子:
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, “l(fā)ocalhost”, “root”, “password”, “testdb”, 0, NULL, 0);
mysql_query(&mysql, “SELECT * FROM testtable”);
相關(guān)問(wèn)題拓展閱讀:
- 怎么在linux上安裝mysql
怎么在linux上安裝mysql
linux下使用yum安裝mysql
1. 安裝mysql 服務(wù)器端:
yum install mysql-server
yum install mysql-devel
2. 安裝mysql客戶(hù)端:
yum install mysql
3. 啟動(dòng)mysql服務(wù):
service mysqld start或純肆者/etc/init.d/mysqld start
停止:
service mysqld stop
重啟:
service mysqld restart
4. 創(chuàng)做帶轎建root管理員:
mysqladmin -u root password
5.登陸行手
mysql -uroot -p123456
1.運(yùn)行平臺(tái):CentOS 6.3 x86_64,基本等同于RHEL 6.3
2.安裝方法:
安裝MySQL主要有兩種方法:一種是通過(guò)源碼自行編譯安裝,這種適合高級(jí)用戶(hù)定制MySQL的特性,這里不做說(shuō)明;另一種是通過(guò)編譯過(guò)的二進(jìn)制文件進(jìn)行安裝。二進(jìn)制文件安裝的方法又分為兩種:一種是不針對(duì)特定平臺(tái)的通用安裝方法,使用的二進(jìn)制槐租文件是后綴為.tar.gz的壓縮文件;第二種是使用RPM或其他包進(jìn)行安裝,這種安裝進(jìn)程會(huì)自動(dòng)完成系統(tǒng)的相關(guān)配置,所以比較方便。
3.下載安裝包:
a. 官芹陪方下載地址:
或鏡像文件下載:
2. 下載文件(根據(jù)操作系統(tǒng)選擇相應(yīng)的發(fā)布版本):
a. 通用安裝方法
mysql-5.5.29-linux2.6-x86_64.tar.gz
b.RPM安裝方法:
MySQL-server-5.5.29-2.el6.x86_64.rpm
MySQL-client-5.5.29-2.el6.x86_64.rpm
4.通用安裝步驟
a.檢查是否已安裝,grep的-i選項(xiàng)表示匹配時(shí)忽略大小寫(xiě)
#rpm -qa|grep -i mysql
mysql-libs-5.1.61-4.el6.x86_64
*可見(jiàn)已經(jīng)安裝了庫(kù)文件,應(yīng)該先卸載,不然會(huì)出現(xiàn)覆蓋錯(cuò)誤。注意卸:載時(shí)使用了–nodeps選項(xiàng),忽略了依賴(lài)關(guān)系:
#rpm -e mysql-libs-5.1.61-4.el6.x86_64 –nodeps
b. 添加mysql組和mysql用戶(hù),用于設(shè)置mysql安裝目錄文件所有者和所屬組。
#groupadd mysql
#useradd -r -g mysql mysql
*useradd -r參數(shù)表示mysql用戶(hù)是系統(tǒng)用戶(hù),不可用于登錄系統(tǒng)。
c. 將二進(jìn)制文件解壓到指定的安裝目錄,我們這里指定為/usr/local
# cd/usr/local/
#tar zxvf /path/to/mysql-5.5.29-linux2.6-x86_64.tar.gz
*加壓后在/usr/local/生成了解壓后的文件夾mysql-5.5.29-linux2.6-x86_64,這名字太長(zhǎng),我們?yōu)樗⒁粋€(gè)符號(hào)鏈接mysql,方便鉛首兆輸入。
#ln -s mysql-5.5.29-linux2.6-x86_64 mysql
d. /usr/local/mysql/下的目錄結(jié)構(gòu)
Directory
Contents of Directory
bin
Client programs and the mysqld server
data
Log files, databases
docs
Manual in Info format
man
Unix manual pages
include
Include (header) files
lib
Libraries
scripts
mysql_install_db
share
Miscellaneous support files, including error messages, sample configuration files, SQL for database installation
sql-bench
Benchmarks
e. 進(jìn)入mysql文件夾,也就是mysql所在的目錄,并更改所屬的組和用戶(hù)。
#cd mysql
#chown -R mysql .
#chgrp -R mysql .
f.執(zhí)行mysql_install_db腳本,對(duì)mysql中的data目錄進(jìn)行初始化并創(chuàng)建一些系統(tǒng)表格。注意mysql服務(wù)進(jìn)程mysqld運(yùn)行時(shí)會(huì)訪問(wèn)data目錄,所以必須由啟動(dòng)mysqld進(jìn)程的用戶(hù)(就是我們之前設(shè)置的mysql用戶(hù))執(zhí)行這個(gè)腳本,或者用root執(zhí)行,但是加上參數(shù)–user=mysql。
scripts/mysql_install_db –user=mysql
*如果mysql的安裝目錄(解壓目錄)不是/usr/local/mysql,那么還必須指定目錄參數(shù),如
scripts/mysql_install_db –user=mysql \
–basedir=/opt/mysql/mysql \
–datadir=/opt/mysql/mysql/data
*將mysql/目錄下除了data/目錄的所有文件,改回root用戶(hù)所有,mysql用戶(hù)只需作為mysql/data/目錄下所有文件的所有者。
chown -R root .
chown -R mysql data
g. 復(fù)制配置文件
cp support-files/my-medium.cnf /etc/my.cnf
h. 將mysqld服務(wù)加入開(kāi)機(jī)自啟動(dòng)項(xiàng)。
*首先需要將scripts/mysql.server服務(wù)腳本復(fù)制到/etc/init.d/,并重命名為mysqld。
cp support-files/mysql.server /etc/init.d/mysqld
*通過(guò)chkconfig命令將mysqld服務(wù)加入到自啟動(dòng)服務(wù)項(xiàng)中。
#chkconfig –add mysqld
*注意服務(wù)名稱(chēng)mysqld就是我們將mysql.server復(fù)制到/etc/init.d/時(shí)重命名的名稱(chēng)。
*查看是否添加成功
#chkconfig –list mysqld
mysqld 0:off 1:off:on:on:on:on:off
i. 重啟系統(tǒng),mysqld就會(huì)自動(dòng)啟動(dòng)了。
*檢查是否啟動(dòng)
#netstat -anp|grep mysqld
tcp0 0.0.0.0: 0.0.0.0:* LISTEN/mysqld
unix STREAM LISTENING5/mysqld/tmp/mysql.sock
*如果不想重新啟動(dòng),那可以直接手動(dòng)啟動(dòng)。
#service mysqld start
Starting MySQL.. SUCCESS!
j.運(yùn)行客戶(hù)端程序mysql,在mysql/bin目錄中,測(cè)試能否連接到mysqld。
#/usr/local/mysql/bin/mysql
Welcome to the MySQLmonitor. Commands end with ; or \g.
Your MySQL connection idis 2
Server version:5.5.29-log MySQL Community Server (GPL)
Copyright (c) 2023, 2023,Oracle and/or its affiliates. All rights reserved.
Oracle is a registeredtrademark of Oracle Corporation and/or its affiliates. Other names may betrademarks of their respective owners.
Type ‘help;’ or ‘\h’ forhelp. Type ‘\c’ to clear the current input statement.
mysql> quit
Bye
*此時(shí)會(huì)出現(xiàn)mysql>命令提示符,可以輸入sql語(yǔ)句,輸入quit或exit退出。為了避免每次都輸入mysql的全路徑/usr/local/mysql/bin/mysql,可將其加入環(huán)境變量中,在/etc/profile最后加入兩行命令:
MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
這樣就可以在shell中直接輸入mysql命令來(lái)啟動(dòng)客戶(hù)端程序了
#mysql
Welcome to the MySQLmonitor. Commands end with ; or \g.
Your MySQL connection idis 3
Server version:5.5.29-log MySQL Community Server (GPL)
Copyright (c) 2023, 2023,Oracle and/or its affiliates. All rights reserved.
Oracle is a registeredtrademark of Oracle Corporation and/or its
affiliates. Other nameay be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ forhelp. Type ‘\c’ to clear the current input statement.
mysql>
5.RPM安裝步驟
a.檢查是否已安裝,grep的-i選項(xiàng)表示匹配時(shí)忽略大小寫(xiě)
#rpm -qa|grep -i mysql
mysql-libs-5.1.61-4.el6.x86_64
可見(jiàn)已經(jīng)安裝了庫(kù)文件,應(yīng)該先卸載,不然會(huì)出現(xiàn)覆蓋錯(cuò)誤。注意卸載時(shí)使用了–nodeps選項(xiàng),忽略了依賴(lài)關(guān)系:
#rpm -e mysql-libs-5.1.61-4.el6.x86_64 –nodeps
2. 安裝MySQL的服務(wù)器端軟件,注意切換到root用戶(hù):
#rpm -ivh MySQL-server-5.5.29-2.el6.x86_64.rpm
安裝完成后,安裝進(jìn)程會(huì)在Linux中添加一個(gè)mysql組,以及屬于mysql組的用戶(hù)mysql??赏ㄟ^(guò)id命令查看:
#id mysql
uid=496(mysql)gid=493(mysql) groups=493(mysql)
MySQL服務(wù)器安裝之后雖然配置了相關(guān)文件,但并沒(méi)有自動(dòng)啟動(dòng)mysqld服務(wù),需自行啟動(dòng):
#service mysql start
Starting MySQL.. SUCCESS!
可通過(guò)檢查端口是否開(kāi)啟來(lái)查看MySQL是否正常啟動(dòng):
#netstat -anp|grep 3306
tcp0 0.0.0.0: 0.0.0.0:* LISTEN/mysqld
c. 安裝MySQL的客戶(hù)端軟件:
#rpm -ivh MySQL-client-5.5.29-2.el6.x86_64.rpm
如果安裝成功應(yīng)該可以運(yùn)行mysql命令,注意必須是mysqld服務(wù)以及開(kāi)啟:
#mysql
Welcome to the MySQLmonitor. Commands end with ; or \g.
Your MySQL connection idis 1
Server version: 5.5.29MySQL Community Server (GPL)
Copyright (c) 2023, 2023,Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademarkof Oracle Corporation and/or its affiliates. Other names may be trademarks oftheir respective owners.
Type ‘help;’ or ‘\h’ forhelp. Type ‘\c’ to clear the current input statement.
mysql>
d. RPM安裝方式文件分布
Directory
Contents of Directory
/usr/bin
Client programs and scripts
/usr/in
The mysqld server
/var/lib/mysql
Log files, databases
/usr/share/info
Manual in Info format
/usr/share/man
Unix manual pages
/usr/include/mysql
Include (header) files
/usr/lib/mysql
Libraries
/usr/share/mysql
Miscellaneous support files, including error messages, character set files, sample configuration files, SQL for database installation
/usr/share/sql-bench
Benchmarks
關(guān)于mysql在linux中的頭文件的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都服務(wù)器托管選創(chuàng)新互聯(lián),先上架開(kāi)通再付費(fèi)。
創(chuàng)新互聯(lián)(www.cdcxhl.com)專(zhuān)業(yè)-網(wǎng)站建設(shè),軟件開(kāi)發(fā)老牌服務(wù)商!微信小程序開(kāi)發(fā),APP開(kāi)發(fā),網(wǎng)站制作,網(wǎng)站營(yíng)銷(xiāo)推廣服務(wù)眾多企業(yè)。電話:028-86922220
分享題目:Linux中的MySQL頭文件詳解 (mysql在linux中的頭文件)
網(wǎng)頁(yè)鏈接:http://m.5511xx.com/article/dhjsjoj.html


咨詢(xún)
建站咨詢(xún)
