1. 程式人生 > >【Linux】Ubuntu安裝Mysql

【Linux】Ubuntu安裝Mysql

嘗試多次apt-get install報錯,那就別偷懶,下載檔案包一個一個安裝吧。

首先徹底刪除:

徹底解除安裝軟體

# 刪除軟體及其配置檔案

apt-get --purge remove mysql-server mysql-client

# 刪除沒用的依賴包

apt-get autoremove mysql-server mysql-client

# 此時dpkg的列表中有“rc”狀態的軟體包,可以執行如下命令做最後清理:

dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P

(然後...開工~)

(如果後續工作中有問題,考慮更換/etc/apt/sources.list:

deb http://mirrors.163.com/debian/ jessie main non-free contrib  
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib  
deb http://mirrors.163.com/debian/ jessie-backports main non-free contrib  
deb-src http://mirrors.163.com/debian/ jessie main non-free contrib  
deb-src http://mirrors.163.com/debian/ jessie-updates main non-free contrib  
deb-src http://mirrors.163.com/debian/ jessie-backports main non-free contrib  
deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib  
deb-src http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib 

1.下載deb-bundle檔案包

首先登入伺服器

執行: wget -c https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-server_5.7.18-1ubuntu16.04_amd64.deb-bundle.tar 

下載DEB-bundle包

然後執行解壓

解壓命令為:

tar –xvf mysql-server_5.7.18-1ubuntu16.04_amd64.deb-bundle.tar

解壓完得到一大堆deb的檔案:

mysql-testsuite_5.7.18-1ubuntu16.04_amd64.deb
mysql-community-client_5.7.18-1ubuntu16.04_amd64.deb
mysql-community-server_5.7.18-1ubuntu16.04_amd64.deb
mysql-community-test_5.7.18-1ubuntu16.04_amd64.deb
libmysqlclient20_5.7.18-1ubuntu16.04_amd64.deb
mysql-client_5.7.18-1ubuntu16.04_amd64.deb
libmysqld-dev_5.7.18-1ubuntu16.04_amd64.deb
mysql-community-source_5.7.18-1ubuntu16.04_amd64.deb
libmysqlclient-dev_5.7.18-1ubuntu16.04_amd64.deb
mysql-server_5.7.18-1ubuntu16.04_amd64.deb
mysql-common_5.7.18-1ubuntu16.04_amd64.deb

2.執行安裝

然後按照順序依次安裝這些檔案

sudo dpkg -i mysql-common_5.7.18-1ubuntu16.04_amd64.deb

sudo dpkg -i libmysqlclient20_5.7.18-1ubuntu16.04_amd64.deb

 sudo dpkg -i libmysqlclient-dev_5.7.18-1ubuntu16.04_amd64.deb

sudo dpkg -i libmysqld-dev_5.7.18-1ubuntu16.04_amd64.deb

sudo dpkg -i mysql-community-client_5.7.18-1ubuntu16.04_amd64.deb

sudo dpkg -i mysql-client_5.7.18-1ubuntu16.04_amd64.deb

sudo dpkg -i mysql-community-source_5.7.18-1ubuntu16.04_amd64.deb

(中間可能會讓安裝libaio等依賴包,可以使用apt-get install安裝,如果版本不對,注意先apt-get update,apt-get upgrade,然後再install;或者實在版本不對,在網上找到包的下載地址,使用wget下載,再安裝。)

3.下載安裝libmecab2

剩下的這一步需要下載依賴庫libmecab2,這塊折騰了很久,因為第一次下載的版本是0.996-1.1的,而最新的mysql-community-server要求版本>=0.996-1.2

先執行命令 sudo dpkg -r libmecab2 刪除舊版本

然後重下,但是最後安裝不了,然後又換了幾個下載地址才搞定...

執行下載:wget -c http://th.archive.ubuntu.com/ubuntu/pool/universe/m/mecab/libmecab2_0.996-1.3_amd64.deb

安裝最後一個檔案:

sudo dpkg -i mysql-community-server_5.7.18-1ubuntu16.04_amd64.deb

然後就可以登入啦 :Mysql -u username -p【password】

4.開啟遠端訪問

啟動和停止的命令

/etc/init.d/mysql start
/etc/init.d/mysql stop

mysql安裝之後,需要開啟遠端訪問許可權

建立使用者並且賦予許可權
指令碼如下:

GRANT <ALL|priv1,priv2,.....privn> ON
[object] [IDENTIFIED BY 'password']
[WITH GRANT OPTION];
MAX_QUERIES_PER_HOUR count
MAX_UPDATES_PER_HOUR count
MAX_CONNECTIONS_PER_HOUR count
MAX_USER_CONNECTIONS count

說明:priv代表權限select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個許可權

示例:

grant select,insert,update,delete,create,drop on test.hr to [email protected] identified by '123';

表示為使用者user建立從地址192.168.10.1對資料庫test的hr表進行select,insert,update,delete,create,drop的許可權,其密碼為123

grant select,insert,update,delete,create,drop on *.* to [email protected]'%' identified by '123';

表示為使用者user建立從任何遠端地址對資料庫的所有庫和表進行select,insert,update,delete,create,drop的許可權,其密碼為123

執行完後需要重新整理許可權:

flush privileges;


然後發現還是訪問不了...

查詢資料得知下面的方法:

把/etc/mysql/my.cnf檔案中的

bind-address    = 127.0.0.1

修改為

bind-address    = 0.0.0.0 或者註釋掉

但是我開啟這個檔案發現沒有這個引數,

然後發現遠端無法訪問,

後來查到執行這個命令:

netstat -apn|grep 3360

結果為:

tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      26255/mysqld

3306埠繫結到了本地

最後然後終於在

/etc/mysql/mysql.conf.d/mysqld.conf

這個目錄中找到了引數,修改完重啟就OK了

如果是使用雲服的同學,記得在管理端的安全策略中開啟3306等埠的訪問許可權。

========

參考部落格:

(https://blog.csdn.net/m0_37739193/article/details/78690593)

今天需要在Debian中安裝mysql測試,可是一直安裝不上,很是鬱悶。。
[email protected]:~# apt-get install mysql-server

[email protected]:~# apt-get install mysql-client

注意:我在Debian8.6 64位中出現這個報錯:

  1. [email protected]:~# apt-get install mysql-server  
  2. Reading package lists... Done  
  3. Building dependency tree         
  4. Reading state information... Done  
  5. The following extra packages will be installed:  
  6.   libdbd-mysql-perl libdbi-perl libhtml-template-perl libterm-readkey-perl mysql-client-5.5 mysql-server-5.5  
  7. Suggested packages:  
  8.   libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl tinyca  
  9. The following NEW packages will be installed:  
  10.   libdbd-mysql-perl libdbi-perl libhtml-template-perl libterm-readkey-perl mysql-client-5.5 mysql-server mysql-server-5.5  
  11. 0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.  
  12. Need to get 3,606 kB/4,517 kB of archives.  
  13. After this operation, 72.4 MB of additional disk space will be used.  
  14. Do you want to continue? [Y/n] y  
  15. WARNING: The following packages cannot be authenticated!  
  16.   libdbd-mysql-perl mysql-client-5.5 mysql-server-5.5 mysql-server  
  17. Install these packages without verification? [y/N] y  
  18. Err http://security.debian.org/ jessie/updates/main libdbd-mysql-perl amd64 4.028-2+deb8u2  
  19.   Could not resolve 'security.debian.org'  
  20. Err http://security.debian.org/ jessie/updates/main mysql-client-5.5 amd64 5.5.53-0+deb8u1  
  21.   Could not resolve 'security.debian.org'  
  22. Err http://security.debian.org/ jessie/updates/main mysql-server-5.5 amd64 5.5.53-0+deb8u1  
  23.   Could not resolve 'security.debian.org'  
  24. Err http://security.debian.org/ jessie/updates/main mysql-server all 5.5.53-0+deb8u1  
  25.   Could not resolve 'security.debian.org'  
  26. E: Failed to fetch http://security.debian.org/pool/updates/main/libd/libdbd-mysql-perl/libdbd-mysql-perl_4.028-2+deb8u2_amd64.deb  Could not resolve 'security.debian.org'  
  27. E: Failed to fetch http://security.debian.org/pool/updates/main/m/mysql-5.5/mysql-client-5.5_5.5.53-0+deb8u1_amd64.deb  Could not resolve 'security.debian.org'  
  28. E: Failed to fetch http://security.debian.org/pool/updates/main/m/mysql-5.5/mysql-server-5.5_5.5.53-0+deb8u1_amd64.deb  Could not resolve 'security.debian.org'  
  29. E: Failed to fetch http://security.debian.org/pool/updates/main/m/mysql-5.5/mysql-server_5.5.53-0+deb8u1_all.deb  Could not resolve 'security.debian.org'  
  30. E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?  
解決:http://blog.csdn.net/jesseyoung/article/details/41387393(文章說預設從cd介質安裝,我在Debian8.6 32位上確實如此,可是在這個64位上卻好像不是啊。。)
[email protected]:~# vi /etc/apt/sources.list(將網路源註釋掉儲存後再執行安裝命令則好使,因為我使用的是靜態IP並沒有聯網)
#deb http://security.debian.org/ jessie/updates main contrib
#deb-src http://security.debian.org/ jessie/updates main contrib

我不知道這個Debian是如何選擇映象源的,
在一臺Debian8.6的機器上的sources.list檔案是這樣的

  1. #   
  2. # deb cdrom:[Debian GNU/Linux 8.6.0 _Jessie_ - Official amd64 DVD Binary-1 20160917-14:25]/ jessie contrib main  
  3. deb cdrom:[Debian GNU/Linux 8.6.0 _Jessie_ - Official amd64 DVD Binary-1 20160917-14:25]/ jessie contrib main  
  4. deb http://security.debian.org/ jessie/updates main contrib  
  5. deb-src http://security.debian.org/ jessie/updates main contrib  
  6. # jessie-updates, previously known as 'volatile'  
  7. # A network mirror was not selected during install.  The following entries  
  8. # are provided as examples, but you should amend them as appropriate  
  9. # for your mirror of choice.  
  10. #  
  11. # deb http://ftp.debian.org/debian/ jessie-updates main contrib  
  12. # deb-src http://ftp.debian.org/debian/ jessie-updates main contrib  
當它用apt-get install mysql-server的時候就是用的本地源(你把映象掛載後並點選已連線就行)就能正常安裝

另一臺Debian8.6的sources.list檔案也是一樣的,可是當它執行apt-get install mysql-server時使用的卻是網路源,並且感覺這網路源有問題

  1. [email protected]:~# apt-get install mysql-server  
  2. Reading package lists... Done  
  3. Building dependency tree         
  4. Reading state information... Done  
  5. The following extra packages will be installed:  
  6.   libdbd-mysql-perl libdbi-perl libhtml-template-perl libterm-readkey-perl mysql-client-5.5 mysql-server-5.5  
  7. Suggested packages:  
  8.   libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl tinyca  
  9. The following NEW packages will be installed:  
  10.   libdbd-mysql-perl libdbi-perl libhtml-template-perl libterm-readkey-perl mysql-client-5.5 mysql-server mysql-server-5.5  
  11. 0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.  
  12. Need to get 3,606 kB/4,517 kB of archives.  
  13. After this operation, 72.4 MB of additional disk space will be used.  
  14. Do you want to continue? [Y/n] y   
  15. WARNING: The following packages cannot be authenticated!  
  16.   libdbd-mysql-perl mysql-client-5.5 mysql-server-5.5 mysql-server  
  17. Install these packages without verification? [y/N] y  
  18. Err http://security.debian.org/ jessie/updates/main libdbd-mysql-perl amd64 4.028-2+deb8u2  
  19.   Could not resolve 'security.debian.org'  
  20. Err http://security.debian.org/ jessie/updates/main mysql-client-5.5 amd64 5.5.53-0+deb8u1  
  21.   Could not resolve 'security.debian.org'  
  22. Err http://security.debian.org/ jessie/updates/main mysql-server-5.5 amd64 5.5.53-0+deb8u1  
  23.   Could not resolve 'security.debian.org'  
  24. Err http://security.debian.org/ jessie/updates/main mysql-server all 5.5.53-0+deb8u1  
  25.   Could not resolve 'security.debian.org'  
  26. E: Failed to fetch http://security.debian.org/pool/updates/main/libd/libdbd-mysql-perl/libdbd-mysql-perl_4.028-2+deb8u2_amd64.deb  Could not resolve 'security.debian.org'  
  27. E: Failed to fetch http://security.debian.org/pool/updates/main/m/mysql-5.5/mysql-client-5.5_5.5.53-0+deb8u1_amd64.deb  Could not resolve 'security.debian.org'  
  28. E: Failed to fetch http://security.debian.org/pool/updates/main/m/mysql-5.5/mysql-server-5.5_5.5.53-0+deb8u1_amd64.deb  Could not resolve 'security.debian.org'  
  29. E: Failed to fetch http://security.debian.org/pool/updates/main/m/mysql-5.5/mysql-server_5.5.53-0+deb8u1_all.deb  Could not resolve 'security.debian.org'  
  30. E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?  
一開始的方法:(https://lug.ustc.edu.cn/wiki/mirrors/help/debian)是映象源的問題,修改網上的映象源為
deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian stable main contrib non-free
deb http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free
  1. [email protected]:~# apt-get update  
  2. [email protected]:~# apt-get install mysql-server mysql-client(可還是報錯。。。)  
  3. Reading package lists... Done  
  4. Building dependency tree         
  5. Reading state information... Done  
  6. Some packages could not be installed. This may mean that you have  
  7. requested an impossible situation or if you are using the unstable  
  8. distribution that some required packages have not yet been created  
  9. or been moved out of Incoming.  
  10. The following information may help to resolve the situation:  
  11. The following packages have unmet dependencies:  
  12.  gsettings-desktop-schemas : Breaks: gnome-settings-daemon (< 3.19.92) but 3.14.2-3 is to be installed  
  13.                              Breaks: mutter (< 3.19.92) but 3.14.4-1~deb8u1 is to be installed  
  14.  libvlccore8 : Breaks: vlc (< 2.2.4-7~) but 2.2.4-1~deb8u1 is to be installed  
  15.                Breaks: vlc-nox (< 2.2.4-7~) but 2.2.4-1~deb8u1 is to be installed  
  16. E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.  
  17. 然後就解決不了了。。。  
最後發現還是網路源的問題,最終早到了個好使的網路源真是不容易啊。。。
  1. deb http://mirrors.163.com/debian/ jessie main non-free contrib  
  2. deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib  
  3. deb http://mirrors.163.com/debian/ jessie-backports main non-free contrib  
  4. deb-src http://mirrors.163.com/debian/ jessie main non-free contrib  
  5. deb-src http://mirrors.163.com/debian/ jessie-updates main non-free contrib  
  6. deb-src http://mirrors.163.com/debian/ jessie-backports main non-free contrib  
  7. deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib  
  8. deb-src http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib  
徹底解除安裝軟體
# 刪除軟體及其配置檔案
apt-get --purge remove mysql-server mysql-client
# 刪除沒用的依賴包
apt-get autoremove mysql-server mysql-client
# 此時dpkg的列表中有“rc”狀態的軟體包,可以執行如下命令做最後清理:
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P

補充:後來需要用Python造作mysql,上網搜了半天也沒一個能說明白的,其實很簡單,我這裡在成功安裝mysql-server和mysql-client後直接再執行apt-get install python-mysqldb就OK了啊。如果你想深入瞭解如何用Python操作mysql的話,可以參考http://www.runoob.com/python/python-mysql.html中的例項。

在安裝完debian作業系統之後大家做的第一件事大概就是修改source.list檔案了吧,否則你是無法線上更新軟體的,那麼source.list檔案中的各個配置項的具體含義你搞懂了麼?下面就以我的source.list檔案為例為大家講解一下。
  1. deb http://mirrors.163.com/debian/ jessie main non-free contrib  
  2. deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib  
  3. deb http://mirrors.163.com/debian/ jessie-backports main non-free contrib  
  4. deb-src http://mirrors.163.com/debian/ jessie main non-free contrib  
  5. deb-src http://mirrors.163.com/debian/ jessie-updates main non-free contrib  
  6. deb-src http://mirrors.163.com/debian/ jessie-backports main non-free contrib  
  7. deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib  
  8. deb-src http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib  
以下內容來自http://www.cnblogs.com/beanmoon/p/3387652.html
其中可以把每一行分為四個部分,說白了,當你線上更新某個軟體時,debian就是遵照這四個選項(準確的說是後三項)的指示找到軟體來給你安裝的:
deb    ###   ftp地址                         ###  版本代號   ###  限定詞
deb    ###   http://mirrors.163.com/debian/ ###  wheezy    ###   main non-free contrib

1. 第一部分
第一部分為deb或者deb-src,其中前者代表軟體的位置,後者代表軟體的原始碼的位置

2. 第二部分
第二部分為你的ftp映象的url,以我的為例,我是用的是大陸速度較快的網易映象。在瀏覽器中開啟此連結以後會發現有如下內容:


其中:
/dists/:目錄包含"發行版"(distributions), 此處是獲得Debian釋出版本(releases)和已釋出版本(pre-releases)的軟體包的正規途徑. 有些舊軟體包及packages.gz檔案仍在裡面.
/pool/:目錄為軟體包的實體地址. 軟體包均放進一個巨大的"池子(pool)", 按照原始碼包名稱分類存放. 為了方便管理, pool目錄下按屬性再分類("main", "contrib" 和 "non-free"), 分類下面再按原始碼包名稱的首字母歸檔. 這些目錄包含的檔案有: 運行於各種系統架構的二進位制軟體包, 生成這些二進位制軟體包的原始碼包.你可以執行命令apt-cache showsrc mypackagename, 檢視'Directory:'行獲知每個軟體包的存放位置. 例如: apache軟體包存放在pool/main/a/apache/目錄中.另外, 由於lib*軟體包數量巨大, 它們以特殊的方式歸檔: 例如, libpaper軟體包存放在pool/main/libp/libpaper/.
還有一些目錄:
/tools/:用於建立啟動盤, 磁碟分割槽, 壓縮/解壓檔案, 啟動Linux的DOS下的小工.
/doc/:基本的Debian文件, 如FAQ, 錯誤報告系統指導等..
/indices/:維護人員檔案和過載檔案.
/project/:大部分為開發人員的資源, 如:project/experimental/本目錄包含了處於開發中的軟體包和工具, 它們均處於alpha測試階段. 使用者不應使用這些軟體, 因為即使是經驗豐富的使用者也會被搞得一團糟.)

3. 第三部分
第三部分表示你的debian版本號(注意,不是某個軟體的版本號,而是debian本身的版本號),也有人稱這是對debian軟體包的一種水平劃分(這種說法還是比較形象的),具體這一項怎麼寫,可以參看http://mirrors.163.com/debian/dists/網頁裡面的內容(如下圖),大致來說按版本的舊-->新程度有這麼幾大類:oldstable,stable,testing,sid


1.oldstable顧名思義,就是以前的穩定發行版了,在本例中oldstable就是Debian6.0.8目錄的一個連結,兩者內容完全一致。
2.stable是指現在的穩定發行版,對於每個debian來說,每個穩定發行版都會有一個代號,如debian 6叫做squeeze,debian 7叫做wheezy,由於在筆者寫本文的時候穩定版是debian7,所以stable就是wheezy的一個連結。
3.testing指目前還暫時處於測試階段的debian發行版,如wheezy往下發展就是jessie了,所以testing代指的就是jessie目錄,它是jessie的一個連結。
4.unstable那就是不穩定版本了,但在這個目錄清單裡我們並沒有看到unstable目錄,其實它還有一個代號:sid,我們知道debian的發行版本名稱大都來自玩具總動員中的角色名稱,而sid就是那個專門破壞玩具的鄰居家壞小孩,所以就把unstable這個不穩定的軟體目錄起名為sid了,這裡面的軟體一般都是最新的,穩定性、安全穩定值得商榷,如果你想做試驗小白鼠,大可使用這裡的軟體。經過一段時間的測試,這裡面的軟體可能會進入下一個階段testing目錄從而出現在下一個debian發行版中,要麼就夭折被直接拋棄了。

4. 第四部分
好了,從水平的角度介紹完debian發行版,現在我們進入這些目錄大致看一下里面的內容(以stable目錄為例):


我們會發現每個目錄都會包括contrib、main、non-free等幾個目錄,那麼這些目錄又是什麼東東呢?

以垂直的觀點來看Debian軟體的分佈:main、contrib、non-free、non-us
因為 Debian 是非營利組織,但是組織架構嚴謹,有一套完善的軟體管理方式。基於其對軟體 free 度的一種堅持,對不同版權軟體包的錄入有一些限定。
下面是對它們的一些簡要介紹:
main     Debian 裡最基本及主要且符合自由軟體規範的軟體 ( packages )。
contrib     這裡頭軟體雖然可以在 Debian 裡頭運作,即使本身屬於自由軟體但多半卻是相依於非自由 ( non-free ) 軟體。
non-free   不屬於自由軟體範疇的軟體。
non-us     這個分類裡頭的軟體都來自非美國地區,當中可能有牽扯到專利、加密..等等問題。
marillat     對應 Christian Marillat 的軟體倉庫,包括mplayer, transcode等。
rareware   對應 rarewares.org 的軟體倉庫, 包括很多音效程式,如lame, musepack, beep media player等。
ustc      對應 [email protected] 維護的一些軟體包,如 mule-gbk, gaim-openq, scim, stardict dicts, patched xpdf, irssi, xmms。
java       對應 Blackdown java。包括 j2re, j2sdk ,mozilla java plugin。
firefly      對應打過firefly補丁的包,包括 fontconfig mozilla mozilla-f