一、mysql簡(jiǎn)介
說(shuō)到數(shù)據(jù)庫(kù)尺迂,我們大多想到的是關(guān)系型數(shù)據(jù)庫(kù)笤妙,比如mysql冒掌、oracle、sqlserver等等蹲盘,這些數(shù)據(jù)庫(kù)軟件在windows上安裝都非常的方便股毫,在Linux上如果要安裝數(shù)據(jù)庫(kù),咱不得不首先推薦的是mysql數(shù)據(jù)庫(kù)了召衔,而且Mysql數(shù)據(jù)庫(kù)的第一個(gè)版本就是發(fā)行在Linux系統(tǒng)上的铃诬。
MySQL是一個(gè)關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),由瑞典MySQL AB公司開(kāi)發(fā)苍凛,目前屬于Oracle公司趣席。MySQL是一種關(guān)聯(lián)數(shù)據(jù)庫(kù)管理系統(tǒng),關(guān)聯(lián)數(shù)據(jù)庫(kù)將數(shù)據(jù)保存在不同的表中醇蝴,而不是將所有數(shù)據(jù)放在一個(gè)大倉(cāng)庫(kù)內(nèi)宣肚,這樣就增加了速度并提高了靈活性。MySQL的SQL語(yǔ)言是用于訪問(wèn)數(shù)據(jù)庫(kù)的最常用標(biāo)準(zhǔn)化語(yǔ)言悠栓。MySQL軟件采用了雙授權(quán)政策(本詞條“授權(quán)政策”)霉涨,它分為社區(qū)版和商業(yè)版,由于其體積小惭适、速度快笙瑟、總體擁有成本低,尤其是開(kāi)放源碼這一特點(diǎn)癞志,一般中小型網(wǎng)站的開(kāi)發(fā)都選擇MySQL作為網(wǎng)站數(shù)據(jù)庫(kù)往枷。由于其社區(qū)版的性能卓越,搭配PHP和Apache可組成良好的開(kāi)發(fā)環(huán)境今阳。
在Linux上安裝mysql數(shù)據(jù)庫(kù)师溅,我們可以去其官網(wǎng)上下載mysql數(shù)據(jù)庫(kù)的rpm包,5.6下載鏈接地址
盾舌,大家可以根據(jù)自己的操作系統(tǒng)去下載對(duì)應(yīng)的數(shù)據(jù)庫(kù)文件墓臭,目前最新的版本是5.7.17。
在這里我是通過(guò)yum來(lái)進(jìn)行mysql數(shù)據(jù)庫(kù)的安裝的妖谴,通過(guò)這種方式進(jìn)行安裝窿锉,可以將跟mysql相關(guān)的一些服務(wù)、jar包都給我們安裝好膝舅,所以省去了很多不必要的麻煩N嗽亍!仍稀!
二洼滚、卸載掉原有mysql
因?yàn)閙ysql數(shù)據(jù)庫(kù)在Linux上實(shí)在是太流行了,所以目前下載的主流Linux系統(tǒng)版本基本上都集成了mysql數(shù)據(jù)庫(kù)在里面技潘,我們可以通過(guò)如下命令來(lái)查看我們的操作系統(tǒng)上是否已經(jīng)安裝了mysql數(shù)據(jù)庫(kù)
[root@xiaoluo ~]# rpm -qa | grep mysql
// 這個(gè)命令就會(huì)查看該操作系統(tǒng)上是否已經(jīng)安裝了mysql數(shù)據(jù)庫(kù)
有的話,我們就通過(guò)rpm -e
命令 或者rpm -e --nodeps
命令來(lái)卸載掉
[root@xiaoluo ~]# rpm -e mysql // 普通刪除模式
[root@xiaoluo ~]# rpm -e --nodeps mysql // 強(qiáng)力刪除模式,如果使用上面命令刪除時(shí)钞艇,提示有依賴(lài)的其它文件,則用該命令可以對(duì)其進(jìn)行強(qiáng)力刪除
在刪除完以后我們可以通過(guò) rpm -qa | grep mysql 命令來(lái)查看mysql是否已經(jīng)卸載成功J捌!
三摆霉、通過(guò)yum
來(lái)進(jìn)行mysql
的安裝
我是通過(guò)yum
的方式來(lái)進(jìn)行mysql
的數(shù)據(jù)庫(kù)安裝豪椿,首先我們可以輸入yum list | grep mysql
命令來(lái)查看yum
上提供的mysql
數(shù)據(jù)庫(kù)可下載的版本:
[root@xiaoluo ~]# yum list | grep mysql
就可以得到y(tǒng)um服務(wù)器上mysql數(shù)據(jù)庫(kù)的可下載版本信息:
然后我們可以通過(guò)輸入yum install -y mysql-server mysql mysql-devel
命令將mysql mysql-server mysql-devel
都安裝好(注意:安裝mysql時(shí)我們并不是安裝了mysql客戶端就相當(dāng)于安裝好了mysql數(shù)據(jù)庫(kù)了,我們還需要安裝mysql-server服務(wù)端才行)
[root@xiaoluo ~]# yum install -y mysql-server mysql mysql-deve
在等待了一番時(shí)間后携栋,yum會(huì)幫我們選擇好安mysql數(shù)據(jù)庫(kù)所需要的軟件以及其它附屬的一些軟件
我們發(fā)現(xiàn)搭盾,通過(guò)yum方式安裝mysql數(shù)據(jù)庫(kù)省去了很多沒(méi)必要的麻煩,當(dāng)出現(xiàn)下面的結(jié)果時(shí)刻两,就代表mysql數(shù)據(jù)庫(kù)安裝成功了
此時(shí)我們可以通過(guò)如下命令增蹭,查看剛安裝好的mysql-server的版本
[root@xiaoluo ~]# rpm -qi mysql-server
我們安裝的mysql-server并不是最新版本,如果你想嘗試最新版本磅摹,那就去mysql官網(wǎng)下載rpm包安裝就行了滋迈,至此我們的mysql數(shù)據(jù)庫(kù)已經(jīng)安裝完成了。
四户誓、mysql數(shù)據(jù)庫(kù)的初始化及相關(guān)配置
我們?cè)诎惭b完mysql數(shù)據(jù)庫(kù)以后饼灿,會(huì)發(fā)現(xiàn)會(huì)多出一個(gè)mysqld的服務(wù),這個(gè)就是咱們的數(shù)據(jù)庫(kù)服務(wù)帝美,我們通過(guò)輸入 service mysqld start 命令就可以啟動(dòng)我們的mysql服務(wù)碍彭。
注意:如果我們是第一次啟動(dòng)mysql服務(wù),mysql服務(wù)器首先會(huì)進(jìn)行初始化的配置悼潭,如:
[root@xiaoluo ~]# service mysqld start
初始化 MySQL 數(shù)據(jù)庫(kù): WARNING: The host 'xiaoluo' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatiblewith this binary MySQL version.
The MySQL daemon, mysqld, should worknormally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnameswhen specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h xiaoluo password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default.
This isstrongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ;
/usr/bin/mysqld_safe &You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script! [確定]
正在啟動(dòng) mysqld: [確定]
這時(shí)我們會(huì)看到第一次啟動(dòng)mysql服務(wù)器以后會(huì)提示非常多的信息庇忌,目的就是對(duì)mysql數(shù)據(jù)庫(kù)進(jìn)行初始化操作,當(dāng)我們?cè)俅沃匦聠?dòng)mysql服務(wù)時(shí)舰褪,就不會(huì)提示這么多信息了皆疹,如:
[root@xiaoluo ~]# service mysqld restart
停止 mysqld: [確定]
正在啟動(dòng) mysqld: [確定]
我們?cè)谑褂?strong>mysql數(shù)據(jù)庫(kù)時(shí),都得首先啟動(dòng)mysqld服務(wù)占拍,我們可以 通過(guò) chkconfig --list | grep mysqld
命令來(lái)查看mysql服務(wù)是不是開(kāi)機(jī)自動(dòng)啟動(dòng)略就,如:
[root@xiaoluo ~]# chkconfig --list | grep mysqld
mysqld 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉 6:關(guān)閉
我們發(fā)現(xiàn)mysqld服務(wù)并沒(méi)有開(kāi)機(jī)自動(dòng)啟動(dòng),我們當(dāng)然可以通過(guò)chkconfig mysqld on
命令來(lái)將其設(shè)置成開(kāi)機(jī)啟動(dòng)晃酒,這樣就不用每次都去手動(dòng)啟動(dòng)了
[root@xiaoluo ~]# chkconfig mysqld on
[root@xiaoluo ~]# chkconfig --list | grep mysql
mysqld 0:關(guān)閉 1:關(guān)閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關(guān)閉
mysql數(shù)據(jù)庫(kù)安裝完以后只會(huì)有一個(gè)root管理員賬號(hào)表牢,但是此時(shí)的root賬號(hào)還并沒(méi)有為其設(shè)置密碼,在第一次啟動(dòng)mysql服務(wù)時(shí)贝次,會(huì)進(jìn)行數(shù)據(jù)庫(kù)的一些初始化工作崔兴,在輸出的一大串信息中,我們看到有這樣一行信息 :
/usr/bin/mysqladmin -u root password 'new-password' // 為root賬號(hào)設(shè)置密碼
所以我們可以通過(guò) 該命令來(lái)給我們的root賬號(hào)設(shè)置密碼(注意:這個(gè)root賬號(hào)是mysql的root賬號(hào),非Linux的root賬號(hào))
[root@xiaoluo ~]# mysqladmin -u root password 'root' // 通過(guò)該命令給root賬號(hào)設(shè)置密碼為 root
此時(shí)我們就可以通過(guò) mysql -u root -p
命令來(lái)登錄我們的mysql數(shù)據(jù)庫(kù)了
五敲茄、mysql數(shù)據(jù)庫(kù)的主要配置文件
-
1
./etc/my.cnf
這是mysql的主配置文件
我們可以查看一下這個(gè)文件的一些信息
[root@xiaoluo etc]# ls my.cnf my.cnf
[root@xiaoluo etc]# cat my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
-
2
./var/lib/mysql
mysql數(shù)據(jù)庫(kù)的數(shù)據(jù)庫(kù)文件存放位置
我們的mysql數(shù)據(jù)庫(kù)的數(shù)據(jù)庫(kù)文件通常是存放在了/ver/lib/mysql
這個(gè)目錄下
[root@xiaoluo ~]# cd /var/lib/mysql/
[root@xiaoluo mysql]# ls -l
總用量 20488
-rw-rw----. 1 mysql mysql 10485760 4月 6 22:01 ibdata1
-rw-rw----. 1 mysql mysql 5242880 4月 6 22:01 ib_logfile0
-rw-rw----. 1 mysql mysql 5242880 4月 6 21:59 ib_logfile1
drwx------. 2 mysql mysql 4096 4月 6 21:59 mysql // 這兩個(gè)是mysql數(shù)據(jù)庫(kù)安裝時(shí)默認(rèn)的兩個(gè)數(shù)據(jù)庫(kù)文件
srwxrwxrwx. 1 mysql mysql 0 4月 6 22:01 mysql.sock
drwx------. 2 mysql mysql 4096 4月 6 21:59 test // 這兩個(gè)是mysql數(shù)據(jù)庫(kù)安裝時(shí)默認(rèn)的兩個(gè)數(shù)據(jù)庫(kù)文件
我們可以自己創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)螺戳,來(lái)驗(yàn)證一下該數(shù)據(jù)庫(kù)文件的存放位置
創(chuàng)建一個(gè)我們自己的數(shù)據(jù)庫(kù):
mysql> create database xiaoluo;
Query OK, 1 row affected (0.00 sec)
[root@xiaoluo mysql]# ls -l
總用量 20492
-rw-rw----. 1 mysql mysql 10485760 4月 6 22:01 ibdata1
-rw-rw----. 1 mysql mysql 5242880 4月 6 22:01 ib_logfile0
-rw-rw----. 1 mysql mysql 5242880 4月 6 21:59 ib_logfile1
drwx------. 2 mysql mysql 4096 4月 6 21:59 mysql
srwxrwxrwx. 1 mysql mysql 0 4月 6 22:01 mysql.sock
drwx------. 2 mysql mysql 4096 4月 6 21:59 test
drwx------. 2 mysql mysql 4096 4月 6 22:15 xiaoluo // 這個(gè)就是我們剛自己創(chuàng)建的xiaoluo數(shù)據(jù)庫(kù)
[root@xiaoluo mysql]# cd xiaoluo/
[root@xiaoluo xiaoluo]# ls
db.opt
-
3
./var/log
mysql數(shù)據(jù)庫(kù)的日志輸出存放位置
我們的mysql數(shù)據(jù)庫(kù)的一些日志輸出存放位置都是在/var/log
這個(gè)目錄下
[root@xiaoluo xiaoluo]# cd
[root@xiaoluo ~]# cd /var/log
[root@xiaoluo log]# ls
amanda cron maillog-20130331 spice-vdagent.log anaconda.ifcfg.log cron-20130331
mcelog spooler anaconda.log cups messages spooler-20130331 anaconda.program.log
dirsrv messages-20130331 sssd anaconda.storage.log dmesg mysqld.log
tallylog anaconda.syslog dmesg.old ntpstats tomcat6 anaconda.xlog dracut.log piranha
wpa_supplicant.log anaconda.yum.log gdm pm-powersave.log wtmp audit httpd ppp
Xorg.0.log boot.log ibacm.log prelink Xorg.0.log.old btmp lastlog sa Xorg.1.log
btmp-20130401 libvirt samba Xorg.2.log cluster luci secure Xorg.9.log ConsoleKit
maillog secure-20130331 yum.log
其中mysqld.log 這個(gè)文件就是我們存放我們跟mysql數(shù)據(jù)庫(kù)進(jìn)行操作而產(chǎn)生的一些日志信息,通過(guò)查看該日志文件折汞,我們可以從中獲得很多信息
因?yàn)槲覀兊膍ysql數(shù)據(jù)庫(kù)是可以通過(guò)網(wǎng)絡(luò)訪問(wèn)的,并不是一個(gè)單機(jī)版數(shù)據(jù)庫(kù)盖腿,其中使用的協(xié)議是 tcp/ip 協(xié)議爽待,我們都知道m(xù)ysql數(shù)據(jù)庫(kù)綁定的端口號(hào)是** 3306** ,所以我們可以通過(guò)netstat -anp
命令來(lái)查看一下翩腐,Linux系統(tǒng)是否在監(jiān)聽(tīng) 3306 這個(gè)端口號(hào):
結(jié)果如上所示鸟款,Linux系統(tǒng)監(jiān)聽(tīng)的3306端口號(hào)就是我們的mysql數(shù)據(jù)庫(kù)!C浴:问病!