mysql的快速入門

來源:互聯網
上載者:User

標籤:字元   key   簡單   databases   儲存   ibm   語言   bin   arch   

最近想使用c語言寫一個聊天的系統,所以看了一看mysql。看到過程中不斷的總結,就寫了一些東西。

說明一下,我使用的系統是ubuntu18.04,

linux下mysql的安裝和使用

1.安裝
sudo apt-get install mysql-server
sudo apt install mysql-client
sudo apt install libmysqlclient-dev
使用這三條命令安裝,什麼含義?
第一個是mysql的伺服器
第二個是mysql的用戶端
第三個是mysql的c語言庫.安裝之後的位置在/usr/include/mysql/下面


2.串連資料庫
mysql -h主機地址 -u使用者名稱 -p使用者密碼
mysql -h localhost -u root -p


3.資料庫的簡單操作

    # whereis mysql  查看檔案安裝路徑
    # which mysql     查詢運行檔案所在路徑(檔案夾地址)
    usr/bin/mysql 是指:mysql的運行路徑
    var/lib/mysql 是指:mysql資料庫檔案的存放路徑
    usr/lib/mysql 是指:mysql的安裝路徑

4.資料庫的相關操作

 (1).資料庫的操作
    查看資料庫
    show databases;(mysql必須使用分號結尾);
    建立資料庫
    create database mydata;
    刪除資料庫
    drop database mydata;
    使用某個資料庫
    use mydata;    
 (2).資料庫中表的建立、增加、刪除、修改
    建立一個表
    create table mytable
    (                                                   //使用圓括弧
        id int auto_increment not null primary key,     //整型、自增、不為空白、主鍵
        name varchar(20),                                 //字串20個。
    );    
    插入資料
    insert mytable value(123,"qiny");                   //設定第一個id的值,id為自增的
    insert mytable(name) value("hello");                 //有選擇的添加資料
    insert mytable(name) values("dxx"),("xiin");        //大量新增資料
    刪除表格
    drop table mytable;                                 //刪除表格
    刪除表格的一行資料
    delete from mytable where id = 123;
    修改表格的屬性值                                    //修改表的屬性包括增加屬性,刪除屬性,修改屬性
    alter table mytable
    add age int not null,             
    drop name,
    modify id int not null primary key auto_increment,
    修改表格的資料
    update mytable set name = ‘nxnn‘ where id = 123;

 (3)資料庫的查詢(常見)
    select * from student;        
    select name,id from student;
    select * from student where id = 123;
    select count(*),avg(score),sum(score) from student group by age;
    select * from student order by id desc | asc;

 (4)其他
    查看錶的構造
    deac mytable;
    
5.資料庫的許可權管理(不知道為什麼沒有成功)
 (1)建立一個使用者
    CREATE USER ‘qiny‘@‘localhost‘ IDENTIFIED BY ‘123456‘;
    create user ‘root‘@‘localhost‘ IDENTIFIED BY ‘password‘;
 (2)分配許可權
    GRANT ALL PRIVILEGES ON *.* TO ‘qiny‘@‘localhost‘ IDENTIFIED BY ‘123456‘;
    GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘localhost‘ IDENTIFIED BY ‘password‘;
 (3)移除許可權
    EVOKE ALL PRIVILEGES ON *.* FROM ‘username‘@‘localhost‘;

6.c語言串連mysql
 (1)安裝msyql的庫檔案
    編譯方式:gcc xxx.c -o target -lmysqlclient
    其中-lmysqlclient的含義是使用mysqlclient的動態庫檔案。
 (2)使用mysql的庫函數(相關函數參考mysql.h或者網上搜尋)
    MYSQL * conn; //建立一個mysql的資料庫連接指標
    conn = MSYQL_init(NULL); //初始化這個資料庫連接指標
    conn = mysql_real_connect(conn,"localhost","qiny","password","test",0,NULL,0);//串連資料庫,其中第一個參數是資料庫連接指標,第二個參數是地址,第三個參數是使用者名稱,第四個參數是密碼,第五個參數是資料庫的名稱。
    mysql_query(conn,sql);//向資料庫發出查詢請求。
    MYSQL_RES * res_ptr = mysql_store_result(conn);    //建立一個資料存放區地區,接受查詢的結果。
    int column = mysql_num_fields(res_ptr);//擷取資料庫表的列數。
    int row = mysql_num_rows(res_ptr);//擷取資料庫表的行數。
    MYSQL_FIELD * field = mysql_fetch_field(res_ptr);//擷取資料庫傳回值表的列名。
    MYSQL_ROW * mysql_row = mysql_fetch_row(res_ptr);//擷取資料庫中的一行資料 。


    

    
    
    
    
    
    
    
        


























 

mysql的快速入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.