2,007 瀏覽數

MySQL (MariaDB) 資料庫管理系統 PHPMyAdmin

PHPMyAdmin 為 MySQL (MariaDB) 資料庫的管理系統,使用者可以透過 網頁 (使用 PHP 語言) 來進行設定與管理.使用前需要先行安裝 資料庫系統 MariaDB (MySQL) , Web 伺服器 (Apache 或 Nginx) 與 PHP .

測試環境為 CentOS 7 x86_64 (IP:192.168.1.1 ,請使用 static IP)

資料庫 MaraiDB

從 CentOS 7 開始的 Database 都換成了 MariaDB (與 MySQL 資料庫是相容的)

[root@localhost ~]# yum update
[root@localhost ~]# yum install mariadb mariadb-devel
[root@localhost ~]# yum install mariadb-server

檢查一下 MariaDB 的服務狀態.

[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]$ systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-07-21 11:30:32 CST; 2s ago
  Process: 5733 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 5705 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 5732 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─5732 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─5890 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql -...

設定一下 MariaDB

[root@localhost ~]$ mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, 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 MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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] N
 ... Success!

By default, MariaDB 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
 - 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
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

剛剛就已經設定了 MariaDB root 密碼.登入看看.

[root@localhost ~]$ mysql -u root -p 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)

MariaDB [(none)]> exit;
Bye

資料庫的安裝就告一段落了,後面需要安裝 PHPMyAdmin 但在這之前需要啟動 web server (Apache).

Apache (Httpd)

Web Server 可以自行選擇 Apache 或是 Nginx .下面使用的是 Apache

[root@localhost ~]$ yum install httpd
[root@localhost ~]$ systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@localhost ~]$ systemctl start httpd
[root@localhost ~]$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-07-21 12:03:23 CST; 5s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 6418 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─6418 /usr/sbin/httpd -DFOREGROUND
           ├─6419 /usr/sbin/httpd -DFOREGROUND
           ├─6420 /usr/sbin/httpd -DFOREGROUND
           ├─6421 /usr/sbin/httpd -DFOREGROUND
           ├─6422 /usr/sbin/httpd -DFOREGROUND
           └─6423 /usr/sbin/httpd -DFOREGROUND


如果連不上請先確一下 Firewall 的狀態.

[root@localhost ~]$ systemctl stop firewalld

PHP

phpMyAdmin 主要透過 PHP 的程式語言架構出來的,而 PHP 是一種直譯式的程式語言,用於網頁的撰寫,不同於靜態式的 HTML 它可以依據使用者的需求來呈現不同的網頁內容.

[root@benjr ~]# yum install php php-pear php-mysql php-mbstring

還需要安裝 mcrypt 擴充套件(沒安裝時 PHPMyAdmin 會顯示錯誤訊息: 缺少 mcrypt 擴充套件,請檢查 PHP 設定),預設的 YUM Repo 並沒有這些套件,需要新增 Repo 檔 (epel-release, Extra Packages for Enterprise Linux) .

[root@localhost ~]$ yum install epel-release
[root@localhost ~]$ yum install php-mcrypt

順便試一下 PHP 功能運作是否正常.你可以用自己喜歡的編輯器來鍵入下面的內容.

[root@benjr ~]# vi /var/www/html/phpinfo.php
<?php
phpinfo();
?>
[root@localhost ~]$ systemctl restart httpd
[root@localhost ~]$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-07-21 14:54:28 CST; 1s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 7072 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 7078 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─7078 /usr/sbin/httpd -DFOREGROUND
           ├─7080 /usr/sbin/httpd -DFOREGROUND
           ├─7081 /usr/sbin/httpd -DFOREGROUND
           ├─7082 /usr/sbin/httpd -DFOREGROUND
           ├─7083 /usr/sbin/httpd -DFOREGROUND
           └─7084 /usr/sbin/httpd -DFOREGROUND

/var/www/html 是 Apache 預設的目錄,你可以修改 /etc/httpd/conf/httpd.conf 修改 DocumentRoot .
這是 PHP 的格式,開啟你的網頁連上你 http://localhost/phpinfo.php ,如果一切運作正常那你會看到和下面一樣的網頁.

PHPMyAdmin

PHPMyAdmin 需要到 https://www.phpmyadmin.net/downloads/ 下載檔案,要注意一下版本選擇.
最新的 phpMyAdmin 4.7.3 需要 PHP 5.5 ~ 7.1 以及 MySQL 5.5 以上的版本.

但我使用的作業系統 CentOS 7 的版本 PHP Version 5.4.16 與 mariadb-5.5.52-1,所以只能選擇 phpMyAdmin 4.0.10.20 (PHP 5.2 以及 MySQL 5). 如果使用到不支援的版本,網頁會顯示不出來 (空白).

[root@localhost ~]$ cd /var/www/html/
[root@localhost html]$ wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@localhost html]$ tar zxvf phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@localhost html]$ mv phpMyAdmin-4.0.10.20-all-languages phpMyAdmin

解開的檔案目錄有點長,所以修改成簡短一點 phpMyAdmin ,以方便輸入 https://Your_IP/phpMyAdmin/ ,正確會顯示如下的網頁.

PHPMyAdmin 輸入剛剛前面設定的帳號與密碼即可已針對資料庫進行設定與管理.

發表迴響