ubuntu上安裝mysql非常簡單只需要幾條命令就可以完成。
#include <stdio.h>
#include <stdlib.h>
int main()
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char server[] = "localhost";
char user[] = "root";
char password[] = "mima";
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));
exit(1);
}
if (mysql_query(conn, "show tables"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
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;
}
編譯代碼的時候需要鏈接mysql的庫,可以通過如下方式編譯:
g++ -Wall mysql_test.cpp -o mysql_test -lmysqlclient
然後運行編譯好的代碼:
centos mysql 安裝及配置
Centos 6.6下安裝Mysql很簡單,
yum list mysql-server
當只有一個時候就可以直接
yum install mysql-server
yum install mysql-devel
進行安裝
先啓動Mysql服務
service mysqld start
連接一下試一下,直接
mysql
然後
\q
關閉連接
設置Mysql開機啓動
chkconfig mysqld on
開啓3306端口並保存
/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
/etc/rc.d/init.d/iptables save
連接mysql數據庫
設置密碼
use mysql;
update user set password=password('密碼') where user='root';
flush privileges;
設置Mysql遠程訪問
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
解決Mysql亂碼問題
找一個配置文件,複製到/etc/目錄,命名爲my.cnf
(有時候沒有my.cnf)
cp /usr/share/doc/mysql-server-5.1.73/my-medium.cnf /etc/my.cnf
vim my.cnf
在[client]和[mysqld]下面都添加上
default-character-set=utf8
最後按Esc輸入
:wq
保存退出
最後重新啓動服務就可以了
service mysqld restart