菜鳥的《Linux程式設計》學習(8):MySQL資料庫安裝、配置及基本操作

來源:互聯網
上載者:User

標籤:

1. MySQL資料庫:在涉及到一些大型的Web系統或者嵌入式軟體的開發時,都少不了用資料庫來管理資料。在Windows作業系統下,使用過各種各樣的資料庫,如:sqlServer、Oracle、MySQL等等,我們知道,在Windows系統上的軟體安裝配置都是介面式,操作明顯而簡捷。那麼在Linux作業系統上,對於資料庫的選用,好像首推的是MySQL,它是一個關係型資料庫管理系統,由瑞典MySQL AB公司開發,目前屬於Oracle公司。MySQL是一種關聯資料庫管理系統,關聯資料庫將資料儲存在不同的表中,而不是將所有資料放在一個大倉庫內,這樣就增加了速度並提高了靈活性。MySQL的SQL語言是用於訪問資料庫的最常用標準化語言。MySQL軟體採用了雙授權政策(本詞條“授權政策”),它分為社區版和商業版,由於其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,一般中小型網站的開發都選擇MySQL作為網站資料庫。由於其社區版的效能卓越,搭配PHP和Apache可組成良好的開發環境。今天,閱讀了《Linux程式設計》的MySQL這一章節,根據自己在虛擬機器中安裝配置的步驟,記錄於此,供於以後的學習回顧。2. MySQL安裝步驟:2.1 查看自己系統中是否已經存在因為mysql資料庫在Linux上實在是太流行了,所以目前下載的主流Linux系統版本基本上都整合了mysql資料庫在裡面,我們可以通過如下命令來查看我們的作業系統上是否已經安裝了mysql資料庫:
[[email protected] ~]# rpm -qa | grep mysql

我們使用root賬戶登入系統,由於我的系統上已經安裝MySQL ,所以得到系統中已安裝版本資訊:
若是未安裝過,則會出現沒有安裝的提示,此時即可跳過步驟2的卸載。2.2 卸載原有的MySQL使用下面命令,卸載現有MySQL:
[[email protected] ~]# rpm -e --nodeps mysql
此時,系統中已經不存在了MySQL的安裝記錄。2.3 yum方式安裝MySQL通過yum的方式來進行mysql的資料庫安裝,首先我們可以輸入 yum list | grep mysql 命令來查看yum上提供的mysql資料庫可下載的版本:

[[email protected] ~]#yum list | grep mysql

我們可以看到yum伺服器上有很多MySQL的可下載版本:

然後,我們通過輸入 yum install -y mysql-server mysql mysql-devel 命令將mysql mysql-server mysql-devel都安裝
[[email protected] ~]# yum install -y mysql-server mysql mysql-devel

幾秒鐘時間過後,我們可以看到下面的資訊:

當我們看到complete這個賞心悅目的單詞時,說明我們已經成功的安裝了MySQL資料庫。3. MySQL資料庫配置3.1 MySQL服務設定在安裝完mysql資料庫以後,會發現會多出一個mysqld的服務,這個就是咱們的資料庫服務,我們通過輸入 service mysqld start 命令就可以啟動我們的mysql服務。

一般我們將該資料庫的服務設定為開機自啟動,使用命令chkconfig mysqld on來設定,同時使用命令chkconfig --list | grep mysql來查看結果:
[[email protected] ~]# chkconfig mysqld on[[email protected] ~]# chkconfig --list | grep mysqlmysqld         0:off1:off2:on3:on4:on5:on6:of

3.2 賬戶密碼設定在我們安裝完MySQL資料庫後,就預設有一個名稱為root的管理員賬戶,此時,需要手動設定密碼:使用命令mysqladmin -u root password ‘root‘ 設定密碼之後,我們可以通過命令mysql -u root -p來登入資料庫:
至此,對於MySQL資料庫配置,就可以結束了。4. MySQL基本操作4.1 基本設定檔(1)在/etc/my.cnf記憶體儲了MySQL資料庫的主要配置資訊,查看:
[[email protected] ~]# cat /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid[[email protected] ~]# 

(2)在/var/lib/mysql記憶體儲了資料庫檔案,查看:
[[email protected] mysql]# cd /var/lib/mysql[[email protected] mysql]# ls -ltotal 20500-rw-r--r--. 1 root  root     10717 May 13 10:34 create-rw-rw----. 1 mysql mysql 10485760 May 13 10:27 ibdata1-rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile0-rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile1drwx------. 2 mysql mysql     4096 May 13 10:27 mysqlsrwxrwxrwx. 1 mysql mysql        0 May 13 10:27 mysql.sockdrwx------. 2 mysql mysql     4096 May 13 10:27 test[[email protected] mysql]# 
4.2 資料庫基本操作(1)建立一個資料庫由上面的預設檔案可以看出,我們的MySQL資料庫中有兩個預設database,一個是mysql,另一個是test。我們建立一個自己的資料庫,命令如下:
[[email protected] mysql]# mysql -u root -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 21Server version: 5.1.73 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> create database yr;Query OK, 1 row affected (0.00 sec)mysql> exitBye[[email protected] mysql]# 
首先,我們用root賬戶登入資料庫,可以看到mysql> 命令提示字元號,執行建立資料庫命令create database name; 得到建立成功訊息,退出資料庫使用命令exit。現在,我們可以在/var/lib.mysql下面看到已經建立的資料庫yr:
[[email protected] mysql]# ls -ltotal 20504-rw-r--r--. 1 root  root     10717 May 13 10:34 create-rw-rw----. 1 mysql mysql 10485760 May 13 10:27 ibdata1-rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile0-rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile1drwx------. 2 mysql mysql     4096 May 13 10:27 mysqlsrwxrwxrwx. 1 mysql mysql        0 May 13 10:27 mysql.sockdrwx------. 2 mysql mysql     4096 May 13 10:27 testdrwx------. 2 mysql mysql     4096 May 13 14:41 yr[[email protected] mysql]# 
(2)建表並添加資料下面,再次登入MySQL我們在上面建立的資料庫中建表,並添加幾條資料:
[[email protected] mysql]# mysql -u root -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 23Server version: 5.1.73 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> use yr;Database changedmysql> create table user(    -> Id INTEGER AUTO_INCREMENT NOT NULL PRIMARY KEY,    -> Name VARCHAR(50),    -> age INTEGER);Query OK, 0 rows affected (0.07 sec)mysql> 

至此,yr資料庫中便建立成功一個user表,有Id 、 Name 、age三個屬性,其中Id為主鍵。添加並查詢資料:
mysql> insert into user(Id , Name , age)values(1,"aa",20);Query OK, 1 row affected (0.10 sec)mysql> insert into user(Id , Name , age)values(2,"bb",21);Query OK, 1 row affected (0.00 sec)mysql> select Id , Name , age from user;+----+------+------+| Id | Name | age  |+----+------+------+|  1 | aa   |   20 ||  2 | bb   |   21 |+----+------+------+
(3)從資料庫表中刪除資料下面我們刪除一條Id=1的資料:
mysql> delete from user where Id=1;Query OK, 1 row affected (0.05 sec)mysql> select Id , Name , age from user;+----+------+------+| Id | Name | age  |+----+------+------+|  2 | bb   |   21 |+----+------+------+1 row in set (0.00 sec)mysql> 

(4)刪除資料庫表和資料庫刪除資料庫表和資料庫使用drop命令:
mysql> drop table user;Query OK, 0 rows affected (0.02 sec)
mysql> drop database yr;Query OK, 0 rows affected (0.28 sec)mysql> exitBye[[email protected] mysql]# lltotal 20500-rw-r--r--. 1 root  root     10717 May 13 10:34 create-rw-rw----. 1 mysql mysql 10485760 May 13 10:27 ibdata1-rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile0-rw-rw----. 1 mysql mysql  5242880 May 13 10:27 ib_logfile1drwx------. 2 mysql mysql     4096 May 13 10:27 mysqlsrwxrwxrwx. 1 mysql mysql        0 May 13 10:27 mysql.sockdrwx------. 2 mysql mysql     4096 May 13 10:27 test[[email protected] mysql]# 

以上是linux系統有關mysql安裝、配置以及使用的全部內容。

菜鳥的《Linux程式設計》學習(8):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.