天天看點

centos6.7 系統安裝配置 mysql 資料庫并配置遠端通路

這裡我們所說的安裝 mysql 實際上是指安裝 mysql-server ,就是mysql的服務端,mysql 用戶端一般在 我們的系統裡已經安裝好了,并不需要再次安裝了。不過我們也可以在安裝 mysql-server 時重新安裝 mysql 用戶端

使用yum指令安裝mysql

使用指令

yum list | grep mysql

檢視

yum

上可用的 mysql 資料庫版本。

yum list | grep mysql
yum list mysql-server
           

使用

yum install mysql-server

安裝mysql服務端

➜  ~ yum install mysql-server

...此處省略一萬字

總下載下傳量:12 M
确定嗎?[y/N]:y
下載下傳軟體包:
(1/5): mysql-5.1.73-7.el6.i686.rpm                | 904 kB     00:00     
(2/5): mysql-libs-5.1.73-7.el6.i686.rpm           | 1.2 MB     00:00     
(3/5): mysql-server-5.1.73-7.el6.i686.rpm         | 8.8 MB     00:00     
(4/5): perl-DBD-MySQL-4.013-3.el6.i686.rpm        | 134 kB     00:00     
(5/5): perl-DBI-1.609-4.el6.i686.rpm              | 705 kB     00:00     
-------------------------------------------------------------------------
總計                                      22 MB/s |  12 MB     00:00     
運作 rpm_check_debug 
執行事務測試
事務測試成功
執行事務
  正在更新   : mysql-libs-5.1.73-7.el6.i686                          1/6 
  正在安裝   : perl-DBI-1.609-4.el6.i686                             2/6 
  正在安裝   : perl-DBD-MySQL-4.013-3.el6.i686                       3/6 
  正在安裝   : mysql-5.1.73-7.el6.i686                               4/6 
  正在安裝   : mysql-server-5.1.73-7.el6.i686                        5/6 
  清理       : mysql-libs-5.1.73-5.el6_6.i686                        6/6 
  Verifying  : perl-DBD-MySQL-4.013-3.el6.i686                       1/6 
  Verifying  : mysql-server-5.1.73-7.el6.i686                        2/6 
  Verifying  : perl-DBI-1.609-4.el6.i686                             3/6 
  Verifying  : mysql-libs-5.1.73-7.el6.i686                          4/6 
  Verifying  : mysql-5.1.73-7.el6.i686                               5/6 
  Verifying  : mysql-libs-5.1.73-5.el6_6.i686                        6/6 

已安裝:
  mysql-server.i686 0:5.1.73-7.el6                                       

作為依賴被安裝:
  mysql.i686 0:5.1.73-7.el6        perl-DBD-MySQL.i686 0:4.013-3.el6     
  perl-DBI.i686 0:1.609-4.el6     

作為依賴被更新:
  mysql-libs.i686 0:5.1.73-7.el6                                         

完畢!
           

到這裡就安裝完成了

啟動 mysql 服務

當我們第一次啟動 mysql 資料庫時,會進行一系列的初始化配置,輸出如下内容

➜  ~ 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 VM_176_3_centos 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  ]
           

這時, mysql 資料庫就啟動成功了,第二次啟動資料庫的時候 就不會再輸出這麼多内容了。

重新開機 mysql 資料庫

重新開機資料庫可以使用以下指令:

➜  ~ service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
           

設定開機自啟

資料庫啟動成功後,設定 mysql 開機自啟動

➜  ~ chkconfig mysqld on
           

可以通過

chkconfig --list | grep mysql

指令檢視 mysql 資料庫是否是開機自啟動

➜  ~ chkconfig --list | grep mysql
mysqld             0:關閉    1:關閉    2:啟用    3:啟用    4:啟用    5:啟用    6:關閉
           

注意:

chkconfig --list

可以檢視伺服器中 所有服務的自啟動狀态

設定 mysql 的密碼

mysql資料庫安裝完以後隻有一個root管理者賬号,但是此時的root賬号還沒有為其設定密碼,這時回頭看一下在第一次啟動mysql服務時,輸出的一大段資訊中,我們看到有這樣一行資訊 :

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

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

➜  ~ mysqladmin -u root password '123456'  // 通過該指令給root賬号設定密碼為 123456
           

然後就可以通過

mysql -u root -p

指令來登入 mysql 資料庫了

➜  ~ 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.

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

mysql> exit;
Bye
           

檢視安裝好的 mysql-server 版本

使用

rpm -qi mysql-server

指令檢視安裝好的 mysql-server 版本資訊

➜  ~ rpm -qi mysql-server
Name        : mysql-server                 Relocations: (not relocatable)
Version     : 5.1.73                            Vendor: CentOS
Release     : 7.el6                         Build Date: 2016年05月11日 星期三 14時05分53秒
Install Date: 2016年10月08日 星期六 14時49分09秒      Build Host: worker1.bsys.centos.org
Group       : Applications/Databases        Source RPM: mysql-5.1.73-7.el6.src.rpm
Size        : 25688915                         License: GPLv2 with exceptions
Signature   : RSA/SHA1, 2016年05月12日 星期四 18時46分25秒, 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.
           

解除安裝 mysql mysql-server

我們可以通過如下指令來檢視我們的系統上是否 安裝了 mysql

➜  ~ rpm -qa | grep mysql
mysql-5.1.73-7.el6.i686
mysql-libs-5.1.73-7.el6.i686
mysql-server-5.1.73-7.el6.i686
           

通過

rpm -e

指令 或者

rpm -e --nodeps

指令來解除安裝掉

➜  ~ rpm -qa | grep mysql
mysql-5.1.73-7.el6.i686
mysql-libs-5.1.73-7.el6.i686
mysql-server-5.1.73-7.el6.i686
➜  ~ rpm -e mysql  
error: Failed dependencies:
    mysql = 5.1.73-7.el6 is needed by (installed) mysql-server-5.1.73-7.el6.i686
➜  ~ rpm -e --nodeps mysql
➜  ~ rpm -qa | grep mysql 
mysql-libs-5.1.73-7.el6.i686
mysql-server-5.1.73-7.el6.i686
           

如上面的所示,當執行

rpm -e mysql

時會因為依賴關系 而出現錯誤,是以可以使用

rpm -e --nodeps mysql

來強制解除安裝,執行完之後 再檢視安裝的

mysql

發現少了

mysql-5.1.73-7.el6.i686

如果想全部解除安裝,再次執行

➜  ~ rpm -e --nodeps mysql-libs
➜  ~ rpm -e --nodeps mysql-server
           

即可。

mysql 的配置檔案

mysql 的配置檔案 儲存在

/etc/my.cnf

我們可以看一下 内容:

➜  ~ cat /etc/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
           

mysql 的資料庫檔案

mysql 的資料庫檔案存放路徑為:

/var/lib/mysql

➜  ~ ls -l /var/lib/mysql
總用量 20528
-rw-rw---- 1 mysql mysql 10485760 10月  8 15:11 ibdata1
-rw-rw---- 1 mysql mysql  5242880 10月  8 15:11 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 10月  8 14:50 ib_logfile1
drwx------ 2 mysql mysql     4096 10月  8 14:49 mysql
srwxrwxrwx 1 mysql mysql        0 10月  8 15:11 mysql.sock
drwx------ 2 mysql mysql     4096 10月  8 14:49 test
           

mysql 的日志輸出檔案

mysql資料庫的日志輸出存放位置在

/var/log

這個目錄下

➜  ~ ls /var/log
anaconda.ifcfg.log    anaconda.yum.log  dmesg       messages    spooler
anaconda.log          audit             dmesg.old   mysqld.log  tallylog
anaconda.program.log  boot.log          dracut.log  ntpstats    wtmp
anaconda.storage.log  btmp              lastlog     prelink     yum.log
anaconda.syslog       cron              maillog     secure
           

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

檢視監聽端口

mysql 的服務端口為 3306 ,我們可以使用

netstat -anp

指令來檢視系統是否在監聽這個端口

➜  ~ netstat -anp
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      3513/mysqld  
           

設定 mysql 的遠端通路

經過上面的設定後,還不能進行遠端通路,連接配接的時候如下圖所示:

centos6.7 系統安裝配置 mysql 資料庫并配置遠端通路

解決辦法

首先登入

mysql

資料庫,然後使用指令

grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;

配置遠端通路:

➜  ~ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
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> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye
➜  ~ 
           

再次測試,就OK了。

centos6.7 系統安裝配置 mysql 資料庫并配置遠端通路

至此,所有的安裝配置都完成了,如有纰漏,請及時告知,謝謝~~