天天看點

Linux下安裝mysql全過程,包括删除。

注:本文為轉載,原文位址:http://zhangyouzhi.com/post/CentOS-install-MySQL/。

在Linux上安裝MySQL資料庫,我們可以去其官網上下載下傳MySQL資料庫的rpm包,

在這裡我是通過yum來進行MySQL資料庫的安裝的,通過這種方式進行安裝,可以将跟MySQL相關的一些服務、jar包都給我們安裝好,是以省去了很多不必要的麻煩!!!

一、解除安裝掉原有MySQL

因為MySQL資料庫在Linux上實在是太流行了,是以目前下載下傳的主流Linux系統版本基本上都內建了MySQL資料庫在裡面,我們可以通過如下指令來檢視我們的作業系統上是否已經安裝了MySQL資料庫。

1
      
[[email protected] /]# rpm -qa | grep mysql  // 這個指令就會檢視該作業系統上是否已經安裝了MySQL資料庫
      

已經安裝的會顯示:

1
      
[[email protected] /]# rpm -qa | grep mysql
php-mysql-5.3.3-40.el6_6.x86_64
mysql-5.1.73-3.el6_5.x86_64
mysql-server-5.1.73-3.el6_5.x86_64
mysql-libs-5.1.73-3.el6_5.x86_64
      

有的話,我們就通過 rpm -e 指令 或者 rpm -e —nodeps 指令來解除安裝掉

1
      
[[email protected] /]# rpm -e mysql  // 普通删除模式
[[email protected] /]# rpm -e --nodeps mysql  // 強力删除模式,如果使用上面指令删除時,提示有依賴的其它檔案,則用該指令可以對其進行強力删除
      

在删除完以後我們可以通過 rpm -qa | grep mysql 指令來檢視mysql是否已經解除安裝成功!!

1
      
[[email protected] /]#  rpm -qa | grep mysql
php-mysql-5.3.3-40.el6_6.x86_64
mysql-libs-5.1.73-3.el6_5.x86_64
      

二、通過yum來進行MySQL的安裝

我是通過yum的方式來進行MySQL的資料庫安裝,首先我們可以輸入 yum list | grep mysql 指令來檢視yum上提供的MySQL資料庫可下載下傳的版本:

1
      
[[email protected] /]# yum list | grep mysql
      

就可以得到yum伺服器上MySQL資料庫的可下載下傳版本資訊:

1
      
[[email protected] /]# yum list | grep mysql
mysql-libs.x86_64                          5.1.73-3.el6_5               @updates
php-mysql.x86_64                           5.3.3-40.el6_6               @updates
apr-util-mysql.x86_64                      1.3.9-3.el6_0.1              base    
asterisk-mysql.x86_64                      1.8.32.1-1.el6               epel    
bacula-director-mysql.x86_64               5.0.0-12.el6                 base    
bacula-storage-mysql.x86_64                5.0.0-12.el6                 base    
collectd-mysql.x86_64                      4.10.9-1.el6                 epel    
dmlite-plugins-mysql.x86_64                0.7.2-1.el6                  epel    
dovecot-mysql.x86_64                       1:2.0.9-8.el6_6.4            updates 
dpm-copy-server-mysql.x86_64               1.8.9-2.el6                  epel    
dpm-name-server-mysql.x86_64               1.8.9-2.el6                  epel    
dpm-server-mysql.x86_64                    1.8.9-2.el6                  epel    
dpm-srm-server-mysql.x86_64                1.8.9-2.el6                  epel    
dspam-mysql.x86_64                         3.10.2-6.el6                 epel    
exim-mysql.x86_64                          4.72-7.el6                   epel    
freeradius-mysql.x86_64                    2.1.12-6.el6                 base    
fts-mysql.i686                             3.2.30-1.el6                 epel    
fts-mysql.x86_64                           3.2.30-1.el6                 epel    
gnokii-smsd-mysql.x86_64                   0.6.30-2.el6                 epel    
holland-mysqldump.noarch                   1.0.10-3.el6                 epel    
holland-mysqllvm.noarch                    1.0.10-3.el6                 epel    
lfc-server-mysql.x86_64                    1.8.9-2.el6                  epel    
libdbi-dbd-mysql.x86_64                    0.8.3-5.1.el6                base    
libnss-mysql.i686                          1.5-12.el6                   epel    
libnss-mysql.x86_64                        1.5-12.el6                   epel    
libodb-mysql.i686                          2.3.0-1.el6                  epel    
libodb-mysql.x86_64                        2.3.0-1.el6                  epel    
libodb-mysql-devel.i686                    2.3.0-1.el6                  epel    
libodb-mysql-devel.x86_64                  2.3.0-1.el6                  epel    
lighttpd-mod_mysql_vhost.x86_64            1.4.35-1.el6                 epel    
lua-sql-mysql.x86_64                       2.1.1-7.el6                  epel    
mod_auth_mysql.x86_64                      1:3.0.0-11.el6_0.1           base    
mydns-mysql.x86_64                         1.2.8.31-1.el6               epel    
mysql.x86_64                               5.1.73-3.el6_5               base    
mysql++.i686                               3.0.9-3.el6                  epel    
mysql++.x86_64                             3.0.9-3.el6                  epel    
mysql++-devel.i686                         3.0.9-3.el6                  epel    
mysql++-devel.x86_64                       3.0.9-3.el6                  epel    
mysql++-manuals.x86_64                     3.0.9-3.el6                  epel    
mysql-bench.x86_64                         5.1.73-3.el6_5               base    
mysql-connector-java.noarch                1:5.1.17-6.el6               base    
mysql-connector-odbc.x86_64                5.1.5r1144-7.el6             base    
mysql-connector-python.noarch              1.1.6-1.el6                  epel    
mysql-devel.i686                           5.1.73-3.el6_5               base    
mysql-devel.x86_64                         5.1.73-3.el6_5               base    
mysql-embedded.i686                        5.1.73-3.el6_5               base    
mysql-embedded.x86_64                      5.1.73-3.el6_5               base    
mysql-embedded-devel.i686                  5.1.73-3.el6_5               base    
mysql-embedded-devel.x86_64                5.1.73-3.el6_5               base    
mysql-libs.i686                            5.1.73-3.el6_5               base    
mysql-mmm.noarch                           2.2.1-2.el6                  epel    
mysql-mmm-agent.noarch                     2.2.1-2.el6                  epel    
mysql-mmm-monitor.noarch                   2.2.1-2.el6                  epel    
mysql-mmm-tools.noarch                     2.2.1-2.el6                  epel    
mysql-proxy.i686                           0.8.5-1.el6                  epel    
mysql-proxy.x86_64                         0.8.5-1.el6                  epel    
mysql-proxy-devel.i686                     0.8.5-1.el6                  epel    
mysql-proxy-devel.x86_64                   0.8.5-1.el6                  epel    
mysql-server.x86_64                        5.1.73-3.el6_5               base    
mysql-test.x86_64                          5.1.73-3.el6_5               base    
mysql-utilities.noarch                     1.3.6-1.el6                  epel    
mysqlreport.noarch                         3.5-4.el6                    epel    
mysqltuner.noarch                          1.1.1-1.el6                  epel    
nagios-plugins-mysql.x86_64                1.4.16-10.el6                epel    
ndoutils-mysql.x86_64                      1.5.2-1.el6                  epel    
nodejs-mysql.noarch                        2.0.0-alpha9.2.el6.2         epel    
opensips-mysql.x86_64                      1.7.2-2.el6                  epel    
owncloud-mysql.noarch                      6.0.6-1.el6                  epel    
pam_mysql.i686                             1:0.7-0.12.rc1.el6           epel    
pam_mysql.x86_64                           1:0.7-0.12.rc1.el6           epel    
pdns-backend-mysql.x86_64                  3.3.1-1.el6                  epel    
perl-Class-DBI-mysql.noarch                1.00-7.el6                   epel    
php-pear-MDB2-Driver-mysql.noarch          1.5.0-0.8.b4.el6             epel    
php-pear-MDB2-Driver-mysqli.noarch         1.5.0-0.8.b4.el6             epel    
poco-mysql.i686                            1.4.2p1-2.el6                epel    
poco-mysql.x86_64                          1.4.2p1-2.el6                epel    
proftpd-mysql.x86_64                       1.3.3g-4.el6                 epel    
pyicq-t-mysql.noarch                       0.8.1.5-5.el6                epel    
qt-mysql.i686                              1:4.6.2-28.el6_5             base    
qt-mysql.x86_64                            1:4.6.2-28.el6_5             base    
qt5-qtbase-mysql.i686                      5.4.0-4.el6                  epel    
qt5-qtbase-mysql.x86_64                    5.4.0-4.el6                  epel    
rekall-mysql.i686                          2.4.6-13.el6                 epel    
rekall-mysql.x86_64                        2.4.6-13.el6                 epel    
root-sql-mysql.x86_64                      5.34.24-1.el6                epel    
rsyslog-mysql.x86_64                       5.8.10-10.el6_6              updates 
rsyslog7-mysql.x86_64                      7.4.10-3.el6_6               updates 
ruby-mysql.x86_64                          2.8.2-1.el6                  epel    
soci-mysql.i686                            3.2.2-2.el6                  epel    
soci-mysql.x86_64                          3.2.2-2.el6                  epel    
soci-mysql-devel.i686                      3.2.2-2.el6                  epel    
soci-mysql-devel.x86_64                    3.2.2-2.el6                  epel    
tcl-mysqltcl.x86_64                        3.05-8.el6                   epel    
trytond-mysql.noarch                       1.8.10-1.el6                 epel    
voms-mysql-plugin.x86_64                   3.1.6-1.el6                  epel    
zabbix-proxy-mysql.x86_64                  1.8.22-1.el6                 epel    
zabbix-server-mysql.x86_64                 1.8.22-1.el6                 epel    
zabbix-web-mysql.noarch                    1.8.22-1.el6                 epel    
zabbix20-proxy-mysql.x86_64                2.0.13-2.el6                 epel    
zabbix20-server-mysql.x86_64               2.0.13-2.el6                 epel    
zabbix20-web-mysql.noarch                  2.0.13-2.el6                 epel    
zabbix22-dbfiles-mysql.noarch              2.2.1-5.el6                  epel    
zabbix22-proxy-mysql.x86_64                2.2.1-5.el6                  epel    
zabbix22-server-mysql.x86_64               2.2.1-5.el6                  epel    
zabbix22-web-mysql.noarch                  2.2.1-5.el6                  epel    
[[email protected] /]#
      

然後我們可以通過輸入 yum install -y mysql-server mysql mysql-devel 指令将mysql mysql-server mysql-devel都安裝好(注意:安裝MySQL時我們并不是安裝了MySQL用戶端就相當于安裝好了MySQL資料庫了,我們還需要安裝mysql-server服務端才行)

1
      
[ro[email protected] /]# yum install -y mysql-server mysql mysql-deve
      

在等待了一番時間後,yum會幫我們選擇好安裝MySQL資料庫所需要的軟體以及其它附屬的一些軟體。

Linux下安裝mysql全過程,包括删除。

我們發現,通過yum方式安裝MySQL資料庫省去了很多沒必要的麻煩,當出現下面的結果時,就代表MySQL資料庫安裝成功了。

Linux下安裝mysql全過程,包括删除。

此時我們可以通過如下指令,檢視剛安裝好的mysql-server的版本

1
      
[[email protected] /]# rpm -qi mysql-server
      
1
      
[[email protected] /]# rpm -qi mysql-server
Name        : mysql-server                 Relocations: (not relocatable)
Version     : 5.1.73                            Vendor: CentOS
Release     : 3.el6_5                       Build Date: Thu 13 Feb 2014 03:42:39 AM CST
Install Date: Thu 12 Mar 2015 02:50:14 PM CST      Build Host: c6b9.bsys.dev.centos.org
Group       : Applications/Databases        Source RPM: mysql-5.1.73-3.el6_5.src.rpm
Size        : 25882723                         License: GPLv2 with exceptions
Signature   : RSA/SHA1, Thu 13 Feb 2014 03:48:08 AM CST, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://www.mysql.com
Summary     : The MySQL server and related files
Description :
MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
client/server implementation consisting of a server daemon (mysqld)
and many different client programs and libraries. This package contains
the MySQL server and some accompanying files and directories.
[[email protected] /]#
      

我們安裝的mysql-server并不是最新版本,如果你想嘗試最新版本,那就去mysql官網下載下傳rpm包安裝就行了,至此我們的MySQL資料庫已經安裝完成了。

三、MySQL資料庫的初始化及相關配置

我們在安裝完MySQL資料庫以後,會發現會多出一個mysqld的服務,這個就是咱們的資料庫服務,我們通過輸入 service mysqld start 指令就可以啟動我們的MySQL服務。

注意:如果我們是第一次啟動MySQL服務,MySQL伺服器首先會進行初始化的配置,如:

1
      
[[email protected] /]# service mysqld start
Initializing MySQL database:  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 iZ23jffs85aZ 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 is
strongly 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!

                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[[email protected] /]#
      

這時我們會看到第一次啟動MySQL伺服器以後會提示非常多的資訊,目的就是對MySQL資料庫進行初始化操作,當我們再次重新啟動MySQL服務時,就不會提示這麼多資訊了,如:

1
      
[[email protected] /]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[[email protected] /]#
      

我們在使用MySQL資料庫時,都得首先啟動mysqld服務,我們可以 通過 chkconfig —list | grep mysqld 指令來檢視mysql服務是不是開機自動啟動,如:

1
      
[[email protected] /]# chkconfig --list | grep mysqld
mysqld         	0:off	1:off	2:off	3:off	4:off	5:off	6:off
[[email protected] /]#
      

我們發現mysqld服務并沒有開機自動啟動,我們當然可以通過 chkconfig mysqld on 指令來将其設定成開機啟動,這樣就不用每次都去手動啟動了。

MySQL資料庫安裝完以後隻會有一個root管理者賬号,但是此時的root賬号還并沒有為其設定密碼,在第一次啟動mysql服務時,會進行資料庫的一些初始化工作,在輸出的一大串資訊中,我們看到有這樣一行資訊 :

1
      
/usr/bin/mysqladmin -u root password 'new-password'  // 為root賬号設定密碼
      

是以我們可以通過 該指令來給我們的root賬号設定密碼(注意:這個root賬号是mysql的root賬号,非Linux的root賬号)

1
      
[[email protected] /]# mysqladmin -u root password 'root'  // 通過該指令給root賬号設定密碼為 root
      

此時我們就可以通過 mysql -u root -p 指令來登入我們的mysql資料庫了。

1
      
[[email protected] /]# mysqladmin -u root password 'root'
[[email protected] /]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql>
      

四、mysql資料庫的主要配置檔案

  1. /etc/my.cnf 這是mysql的主配置檔案

    我們可以檢視一下這個檔案的一些資訊:

    1
          
    [[email protected] /]# cd /etc
    [[email protected] etc]# ls my.cnf 
    my.cnf
    [[email protected] 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
    [[email protected] etc]#
          
  2. /var/lib/mysql mysql資料庫的資料庫檔案存放位置

    我們的mysql資料庫的資料庫檔案通常是存放在了/ver/lib/mysql這個目錄下

    1
          
    [[email protected] /]# cd /var/lib/mysql
    [[email protected] mysql]# ls -l
    total 20488
    -rw-rw---- 1 mysql mysql 10485760 Mar 12 14:58 ibdata1
    -rw-rw---- 1 mysql mysql  5242880 Mar 12 14:58 ib_logfile0
    -rw-rw---- 1 mysql mysql  5242880 Mar 12 14:57 ib_logfile1
    drwx------ 2 mysql mysql     4096 Mar 12 14:57 mysql  // 這兩個是mysql資料庫安裝時預設的兩個資料庫檔案
    srwxrwxrwx 1 mysql mysql        0 Mar 12 14:58 mysql.sock
    drwx------ 2 mysql mysql     4096 Mar 12 14:57 test   // 這兩個是mysql資料庫安裝時預設的兩個資料庫檔案
    [[email protected] mysql]#
          

我們可以自己建立一個資料庫,來驗證一下該資料庫檔案的存放位置

1
      
[[email protected] mysql]# cd /
[[email protected] /]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

//建立新的資料庫xiaobaobao
mysql> create database xiaobaobao;
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye
[[email protected] /]# cd /var/lib/mysql/
[[email protected] mysql]# ls -l
total 20492
-rw-rw---- 1 mysql mysql 10485760 Mar 12 14:58 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Mar 12 14:58 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Mar 12 14:57 ib_logfile1
drwx------ 2 mysql mysql     4096 Mar 12 14:57 mysql
srwxrwxrwx 1 mysql mysql        0 Mar 12 14:58 mysql.sock
drwx------ 2 mysql mysql     4096 Mar 12 14:57 test
drwx------ 2 mysql mysql     4096 Mar 12 15:14 xiaobaobao	// 這個就是我們剛自己建立的xiaobaobao資料庫
[[email protected] mysql]#
      
  1. /var/log mysql資料庫的日志輸出存放位置

    我們的mysql資料庫的一些日志輸出存放位置都是在/var/log這個目錄下

    1
          
    [[email protected] mysql]# cd /var/log
    [[email protected] log]# ls
    audit          cron-20150308     maillog-20150222   ntp.log          secure-20150222   vmstart.log
    boot.log       dmesg             maillog-20150301   ntpstats         secure-20150301   wtmp
    btmp           dmesg.old         maillog-20150308   prelink          secure-20150308   yum.log
    btmp-20150301  dracut.log        messages           rdate.log        spooler           yum.log-20150101
    ConsoleKit     gshell.log        messages-20150215  repair.log       spooler-20150215
    cron           httpd             messages-20150222  rsyncd.log       spooler-20150222
    cron-20150215  lastlog           messages-20150301  sa               spooler-20150301
    cron-20150222  maillog           messages-20150308  secure           spooler-20150308
    cron-20150301  maillog-20150215  mysqld.log         secure-20150215  tallylog
    [[email protected] log]#
          

其中mysqld.log 這個檔案就是我們存放我們跟mysql資料庫進行操作而産生的一些日志資訊,通過檢視該日志檔案,我們可以從中獲得很多資訊。

因為我們的mysql資料庫是可以通過網絡通路的,并不是一個單機版資料庫,其中使用的協定是 tcp/ip 協定,我們都知道mysql資料庫綁定的端口号是 3306 ,是以我們可以通過 netstat -anp 指令來檢視一下,Linux系統是否在監聽 3306 這個端口号:

Linux下安裝mysql全過程,包括删除。
1
      
[[email protected] log]# netstat -anp | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             
......
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      7459/mysqld         
......
      

結果如上所示,Linux系統監聽的3306端口号就是我們的mysql資料庫。

如有侵權,請聯系我:[email protected]

繼續閱讀