W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
本書在第18章講解過MySQL和MariaDB數(shù)據(jù)庫管理系統(tǒng)之間的因緣和特性,也狠狠地夸獎了MariaDB數(shù)據(jù)庫,但是MySQL數(shù)據(jù)庫當(dāng)前依然是生產(chǎn)環(huán)境中最常使用的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一,坐擁極大的市場份額,并且已經(jīng)通過十幾年不斷的發(fā)展向業(yè)界證明了自身的穩(wěn)定性和安全性。另外,雖然第18章已經(jīng)講解了基本的數(shù)據(jù)庫管理知識,但是為了進(jìn)一步幫助大家夯實基礎(chǔ),本章依然在這里整合了MySQL數(shù)據(jù)庫內(nèi)容,使大家在溫故的同時可以知新。
在使用Yum軟件倉庫安裝服務(wù)程序時,系統(tǒng)會自動根據(jù)RPM軟件包中的指令集完整軟件配置等工作。但是一旦選擇使用源碼包的方式來安裝,這一切就需要自己來完成了。針對MySQL數(shù)據(jù)庫來講,我們需要在系統(tǒng)中創(chuàng)建一個名為mysql的用戶,專門用于負(fù)責(zé)運行MySQL數(shù)據(jù)庫。請記得要把這類賬戶的Bash終端設(shè)置成nologin解釋器,避免黑客通過該用戶登錄到服務(wù)器中,從而提高系統(tǒng)安全性。
[root@linuxprobe cmake-2.8.11.2]# cd ..
[root@linuxprobe src]# useradd mysql -s /sbin/nologin
創(chuàng)建一個用于保存MySQL數(shù)據(jù)庫程序和數(shù)據(jù)庫文件的目錄,并把該目錄的所有者和所屬組身份修改為mysql。其中,/usr/local/mysql是用于保存MySQL數(shù)據(jù)庫服務(wù)程序的目錄,/usr/local/mysql/var則是用于保存真實數(shù)據(jù)庫文件的目錄。
[root@linuxprobe src]# mkdir -p /usr/local/mysql/var
[root@linuxprobe src]# chown -Rf mysql:mysql /usr/local/mysql
接下來解壓、編譯、安裝MySQL數(shù)據(jù)庫服務(wù)程序。在編譯數(shù)據(jù)庫時使用的是cmake命令,其中,-DCMAKE_INSTALL_PREFIX參數(shù)用于定義數(shù)據(jù)庫服務(wù)程序的保存目錄,-DMYSQL_DATADIR參數(shù)用于定義真實數(shù)據(jù)庫文件的目錄,-DSYSCONFDIR則是定義MySQL數(shù)據(jù)庫配置文件的保存目錄。由于MySQL數(shù)據(jù)庫服務(wù)程序比較大,因此編譯的過程比較漫長,在此期間可以稍微休息一下。
[root@linuxprobe src]# tar xzvf mysql-5.6.19.tar.gz
[root@linuxprobe src]# cd mysql-5.6.19/
[root@linuxprobe mysql-5.6.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc
[root@linuxprobe mysql-5.6.19]# make
[root@linuxprobe mysql-5.6.19]# make install
為了讓MySQL數(shù)據(jù)庫程序正常運轉(zhuǎn)起來,需要先刪除/etc目錄中的默認(rèn)配置文件,然后在MySQL數(shù)據(jù)庫程序的保存目錄scripts內(nèi)找到一個名為mysql_install_db的腳本程序,執(zhí)行這個腳本程序并使用--user參數(shù)指定MySQL服務(wù)的對應(yīng)賬號名稱(在前面步驟已經(jīng)創(chuàng)建),使用--basedir參數(shù)指定MySQL服務(wù)程序的保存目錄,使用--datadir參數(shù)指定MySQL真實數(shù)據(jù)庫的文件保存目錄,這樣即可生成系統(tǒng)數(shù)據(jù)庫文件,也會生成出新的MySQL服務(wù)配置文件。
[root@linuxprobe mysql-5.6.19]# rm -rf /etc/my.cnf
[root@linuxprobe mysql-5.6.19]# cd /usr/local/mysql
[root@linuxprobe mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var
把系統(tǒng)新生成的MySQL數(shù)據(jù)庫配置文件鏈接到/etc目錄中,然后把程序目錄中的開機(jī)程序文件復(fù)制到/etc/rc.d/init.d目錄中,以便通過service命令來管理MySQL數(shù)據(jù)庫服務(wù)程序。記得把數(shù)據(jù)庫腳本文件的權(quán)限修改成755以便于讓用戶有執(zhí)行該腳本的權(quán)限:
[root@linuxprobe mysql]# ln -s my.cnf /etc/my.cnf
[root@linuxprobe mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@linuxprobe mysql]# chmod 755 /etc/rc.d/init.d/mysqld
編輯剛復(fù)制的MySQL數(shù)據(jù)庫腳本文件,把第46、47行的basedir與datadir參數(shù)分別修改為MySQL數(shù)據(jù)庫程序的保存目錄和真實數(shù)據(jù)庫的文件內(nèi)容。
[root@linuxprobe mysql]# vim /etc/rc.d/init.d/mysqld
………………省略部分輸出信息………………
39 #
40 # If you want to affect other MySQL variables, you should make your changes
41 # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
42
43 # If you change base dir, you must also change datadir. These may get
44 # overwritten by settings in the MySQL configuration files.
45
46 basedir=/usr/local/mysql
47 datadir=/usr/local/mysql/var
48
………………省略部分輸出信息………………
配置好腳本文件后便可以用service命令啟動mysqld數(shù)據(jù)庫服務(wù)了。mysqld是MySQL數(shù)據(jù)庫程序的服務(wù)名稱,注意不要寫錯。順帶再使用chkconfig命令把mysqld服務(wù)程序加入到開機(jī)啟動項中。
[root@Linuxprobe mysql]# service mysqld start
Starting MySQL. SUCCESS!
[root@linuxprobe mysql]# chkconfig mysqld on
MySQL數(shù)據(jù)庫程序自帶了許多命令,但是Bash終端的PATH變量并不會包含這些命令所存放的目錄,因此我們也無法順利地對MySQL數(shù)據(jù)庫進(jìn)行初始化,也就不能使用MySQL數(shù)據(jù)庫自帶的命令了。想要把命令所保存的目錄永久性地定義到PATH變量中,需要編輯/etc/profile文件并寫入追加的命令目錄,這樣當(dāng)物理設(shè)備在下一次重啟時就會永久生效了。如果不想通過重啟設(shè)備的方式來生效,也可以使用source命令加載一下/ect/profile文件,此時新的PATH變量也可以立即生效了。
[root@linuxprobe mysql]# vim /etc/profile
………………省略部分輸出信息………………
64
65 for i in /etc/profile.d/*.sh ; do
66 if [ -r "$i" ]; then
67 if [ "${-#*i}" != "$-" ]; then
68 . "$i"
69 else
70 . "$i" >/dev/null
71 fi
72 fi
73 done
74 export PATH=$PATH:/usr/local/mysql/bin
75 unset i
76 unset -f pathmunge
[root@linuxprobe mysql]# source /etc/profile
MySQL數(shù)據(jù)庫服務(wù)程序還會調(diào)用到一些程序文件和函數(shù)庫文件。由于當(dāng)前是通過源碼包方式安裝MySQL數(shù)據(jù)庫,因此現(xiàn)在也必須以手動方式把這些文件鏈接過來。
[root@linuxprobe mysql]# mkdir /var/lib/mysql
[root@linuxprobe mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
[root@linuxprobe mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
[root@linuxprobe mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
現(xiàn)在,MySQL數(shù)據(jù)庫服務(wù)程序已經(jīng)啟動,調(diào)用的各個函數(shù)文件已經(jīng)就位,PATH環(huán)境變量中也加入了MySQL數(shù)據(jù)庫命令的所在目錄。接下來準(zhǔn)備對MySQL數(shù)據(jù)庫進(jìn)行初始化,這個初始化的配置過程與MariaDB數(shù)據(jù)庫是一樣的,只是最后變成了Thanks for using MySQL!
[root@linuxprobe mysql]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): 此處只需按下回車鍵
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y (要為root管理員設(shè)置數(shù)據(jù)庫的密碼)
New password: 輸入要為root管理員設(shè)置的數(shù)據(jù)庫密碼
Re-enter new password: 再輸入一次密碼
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y (刪除匿名賬戶)
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y (禁止root管理員從遠(yuǎn)程登錄)
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y (刪除test數(shù)據(jù)庫并取消對其的訪問權(quán)限)
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y (刷新授權(quán)表,讓初始化后的設(shè)定立即生效)
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: