新聞中心
當然,mysql的安裝方法多種多樣,在centos上,你可以采用yum的方式安裝,這樣的好處是:快速方便?;旧?,它會幫你解決所有的函數(shù)庫依賴問題,正常情況下,只要YUM執(zhí)行完成,那么MySQL也就可以使用了。

10年積累的做網(wǎng)站、成都做網(wǎng)站經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先網(wǎng)站設計后付款的網(wǎng)站建設流程,更有密山免費網(wǎng)站建設讓你可以放心的選擇與我們合作。
但我更傾向于使用源碼的方式來安裝MySQL,原因也很簡單:除了有詳細的官方文檔外,你還可以非常清楚地知道你自己在做什么,這點在以后MySQL運行出現(xiàn)問題時將會有很大的幫助!
但即便是按照官方文檔來安裝,你也會遇到各種各樣的問題,這里,我將呈現(xiàn)一個完整的過程給大家,直到完成下面的4個任務:
-
下載MySQL 5.6
-
安裝準備:安裝MySQL依賴函數(shù)庫
-
安裝與運行MySQL
-
優(yōu)化MySQL
(1)賬戶安全優(yōu)化
(2)數(shù)據(jù)庫安全優(yōu)化
1.下載MySQL 5.6
下載地址:http://dev.mysql.com/downloads/mysql/5.6.html
進入該下載地址后,選擇:
Linux - Generic (glibc 2.5) (x86, 64-bit), Compressed TAR Archive
或
Linux - Generic (glibc 2.5) (x86, 32-bit), Compressed TAR Archive
這取決于你用的是32位的還是64位的,這里,我下載的是64位的,下載完成后的包如下:
| 1 2 | [root@leaf ~] # ls mysql-5.6.29-linux-glibc2.5-x86_64. tar .gz |
MySQL依賴一個名為libaio的函數(shù)庫,需要先安裝它,否則后面安裝MySQL會出現(xiàn)問題。
如下:
| 1 2 3 4 5 6 | [root@leaf ~] # yum search libaio #查找libaio的相關信息 [root@leaf ~] # yum install libaio Loaded plugins: security Setting up Install Process Package libaio-0.3.107-10.el6.x86_64 already installed and latest version Nothing to do |
當然,有可能在你安裝完CentOS后,你就已經(jīng)安裝了libaio,這取決你你當時安裝CentOS時所選擇的安裝包,如上面我的安裝提示,就說明我的系統(tǒng)上已經(jīng)安裝了libaio了。
(1)分別創(chuàng)建一個名為mysql的用戶和用戶組
如下:
| 1 2 | [root@leaf ~] # groupadd mysql [root@leaf ~] # useradd -r -g mysql -s /bin/false mysql |
-r和-s參數(shù)的可以使得mysql這個用戶沒有登陸你系統(tǒng)的權限,這可以保證系統(tǒng)的安全性。
(2)解包與建立軟鏈接
如下:
| 1 2 3 | [root@leaf ~] # cd /usr/local [root@leaf local ] # tar zxvf /root/mysql-5.6.29-linux-glibc2.5-x86_64.tar.gz [root@leaf local ] # ln -s /usr/local/mysql-5.6.29-linux-glibc2.5-x86_64/ mysql |
需要知道的是,正常情況下,我們習慣將編譯安裝的軟件放在/usr/local目錄下,當然你也可以自行選擇,不過還是建議放在這里。
建立軟鏈接的好處是,如果你需要使用mysql的安裝目錄,就不用去輸入一長串的目錄名稱了,因為我們解壓縮后的mysql包的目錄,名字很長。
(3)初始化Data目錄
解包完MySQL后,MySQL目錄中會有一個data目錄:
| 1 2 3 | [root@leaf local ] # cd mysql [root@leaf mysql] # ls -d data/ data/ |
里面包含的是MySQL運行所必需的系統(tǒng)信息,因此我們需要將這些數(shù)據(jù)初始化,如下:
| 1 2 3 4 5 | [root@leaf mysql] # chown -R mysql . #修改mysql目錄下的所有文件的屬主為mysql [root@leaf mysql] # chgrp -R mysql . #修改mysql目錄下的所有文件的屬組為mysql [root@leaf mysql] # scripts/mysql_install_db --user=mysql #以mysql用戶的身份初始化數(shù)據(jù) [root@leaf mysql] # chown -R root . #修改mysql目錄下的所有文件的屬主為root [root@leaf mysql] # chown -R mysql data #修改mysql目錄下的data目錄的屬主為mysql |
(4)啟動MySQL
如下:
| 1 2 3 4 5 | [root@leaf mysql] # bin/mysqld_safe --user=mysql & [1] 30877 [root@leaf mysql] # 160306 11:58:50 mysqld_safe Logging to '/var/log/mysqld.log'. 160306 11:58:50 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 160306 11:58:51 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld .pid ended |
理論上應該是可以的,但是看我上面的操作,出現(xiàn)了ended的提示,也就是mysql啟動不了,這時我們去看一下日志信息:
| 1 2 3 4 5 | [root@leaf ~] # tail -f /var/log/mysqld.log ...... 2016-03-06 12:00:36 31231 [ERROR] /usr/local/mysql/bin/mysqld : Can 't create/write to file ' /var/run/mysqld/mysqld .pid' (Errcode: 2 - No such file or directory) 2016-03-06 12:00:36 31231 [ERROR] Can 't start server: can' t create PID file : No such file or directory 160306 12:00:36 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld .pid ended |
問題:can't create PID file: No such file or directory,即找不到mysql啟動的pid文件
解決方案:mysqld目錄不存在,我們創(chuàng)建它就可以了
| 1 2 3 4 5 | [root@leaf mysql] # mkdir /var/run/mysqld [root@leaf mysql] # cd /var/run/mysqld/ [root@leaf mysqld] # touch mysqld.pid #創(chuàng)建mysqld.pid文件 [root@leaf mysqld] # cd .. [root@leaf run] # chown -R mysql mysqld #將mysqld目錄的屬主設置為mysql |
回到mysql目錄,我們再啟動一次mysql,如下:
| 1 2 3 4 | [root@leaf run] # cd /usr/local/mysql [root@leaf mysql] # bin/mysqld_safe --user=mysql 160306 12:12:45 mysqld_safe Logging to '/var/log/mysqld.log' . 160306 12:12:45 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql |
可以看到并沒有終止運行的提示信息,我們再確認一下mysql服務是不是已經(jīng)啟動了:
| 1 2 3 4 | [root@leaf ~] # netstat -antup Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID /Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 31484 /mysqld |
mysql服務確實已經(jīng)成功啟動了!
(5)測試mysql服務
為了使mysql可以更好地在你的系統(tǒng)上運行,建議進行一定的mysql服務測試,如下:
| 1 2 3 4 | [root@leaf mysql] # bin/mysqladmin version bin /mysqladmin : connect to server at 'localhost' failed error: 'Can' t connect to local MySQL server through socket '/tmp/mysql.sock' (2)' Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists! |
按照官方文檔的操作去測試,但卻出現(xiàn)了上面的問題(需要注意的是,我的mysql服務已經(jīng)開啟了?。?/p>
問題:/tmp/mysql.sock不存在
解決方案:其實mysql.sock是存在的,只是它不在/tmp目錄下而已,默認情況下,mysql.sock在/var/lib/mysql/目錄下,我們只需要創(chuàng)建一個軟鏈接到/tmp目錄下即可
| 1 | [root@leaf mysql] # ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock |
這時再重復上面的操作:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@leaf mysql] # bin/mysqladmin version bin /mysqladmin Ver 8.42 Distrib 5.6.29, for linux-glibc2.5 on x86_64 Copyright (c) 2000, 2016, Oracle and /or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and /or its affiliates. Other names may be trademarks of their respective owners. Server version 5.6.29 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /tmp/mysql .sock Uptime: 6 min 36 sec Threads: 1 Questions: 2 Slow queries: 0 Opens: 67 Flush tables: 1 Open tables: 60 Queries per second avg: 0.005 |
成功了!然后我們再進行下面的操作熱熱身吧:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | [root@leaf mysql] # bin/mysqladmin -u root shutdown #通過mysqladmin關閉mysql服務 [root@leaf mysql] # bin/mysqld_safe --user=mysql & #啟動mysql服務 #查看mysql數(shù)據(jù)庫中默認存在的數(shù)據(jù)庫 [root@leaf mysql] # bin/mysqlshow +--------------------+ | Databases | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ #查看mysql數(shù)據(jù)庫(注意此mysql數(shù)據(jù)庫是一個實體,與上面的統(tǒng)稱不同)中的數(shù)據(jù)表 [root@leaf mysql] # bin/mysqlshow mysql Database: mysql +---------------------------+ | Tables | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | innodb_index_stats | | innodb_table_stats | | ndb_binlog_index | | plugin | | proc | | procs_priv | | proxies_priv | | servers | | slave_master_info | | slave_relay_log_info | | slave_worker_info | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 查看mysql數(shù)據(jù)庫中的所有user表 [root@leaf mysql] # bin/mysql -e "SELECT User, Host, plugin FROM mysql.user" mysql +------+-----------+-----------------------+ | User | Host | plugin | +------+-----------+-----------------------+ | root | localhost | mysql_native_password | | root | leaf | mysql_native_password | | root | 127.0.0.1 | mysql_native_password | | root | ::1 | mysql_native_password | | | localhost | mysql_native_password | | | leaf | mysql_native_password | +------+-----------+-----------------------+ |
準確來講,MySQL是已經(jīng)成功安裝完成了!下面我們再做一些基礎的優(yōu)化,主要是從安全的角度去考慮。
文章題目:CentOS上源碼安裝MySQL問題解決及安全優(yōu)化
當前路徑:http://fisionsoft.com.cn/article/cdsicop.html


咨詢
建站咨詢
