內容選單標籤

2020年12月4日 星期五

CentOS8 vsftp、Apache、php、MariaDB

 此篇已經重新謄錄完成!!

可以作廢。



||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| vsftpd |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

# dnf list -h

# dnf list | grep vsftpd
vsftpd.x86_64                                        3.0.3-31.el8                                     AppStream


# dnf -y install vsftpd
...
已安裝:
logrotate-3.14.0-3.el8.x86_64                   vsftpd-3.0.3-31.el8.x86_64         完成!


# dnf list installed | grep vsftpd
vsftpd.x86_64                        3.0.3-31.el8

# rpm -qa | grep vsftpd
vsftpd-3.0.3-32.el8.x86_64



# vi /etc/vsftpd/vsftpd.conf
...
chroot_local_user=YES
...


# ls /usr/sbin | grep vsftpd
vsftpd


# ls /usr/lib/systemd/system | grep vsftpd
vsftpd.service
vsftpd@.service
vsftpd.target


# ls /etc/vsftpd
ftpusers  user_list  vsftpd.conf  vsftpd_conf_migrate.sh


# ls /var/ftp
pub



# systemctl | grep vsftpd
# systemctl status vsftpd
# systemctl start vsftpd.service
# systemctl enable vsftpd.service


# systemctl status vsftpd
# ps -aux | grep vsftpd




# firewall-cmd --state
running

# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

# firewall-cmd --get-active-zone
public
  interfaces: ens33

# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: cockpit dhcpv6-client ssh
...

# firewall-cmd --get-services | grep ftp    //沒有 vsftpd
... ftp ...tftp tftp-client ...


# firewall-cmd --permanent --zone=public --add-service=ftp
success


# firewall-cmd --reload

# firewall-cmd --zone=public --list-all
public (active)
...
  services: cockpit dhcpv6-client ftp ssh




# getsebool -a | grep ftp*
...

# setsebool -P ftpd_full_access on
# setsebool -P tftp_home_dir on




# useradd s306
# passwd s306

# cat /etc/group
...
s306:x:1000:

# cat /etc/passwd
...
s306:x:1000:1000::/home/s306:/bin/bash

# cat /etc/shadow
...
s306:$6$q5YHBgD77s9Och3S$LQvTtYegLZOJSjqoc7jsssC0NY/9z8AcZm5E0dysTZ9LZZLzVmStgdMsYUhkhbYk87g9rOptU1H/E9G9lwxmR0:18602:0:99999:7:::



# ls -l /home
drwx------. 2 s306 s306 62 12月  6 09:01 s306

# id s306
uid=1000(s306) gid=1000(s306) groups=1000(s306)





# su -l s306
$ mkdir {01..40}
$ ls

$ su
密碼:
# cd
#

# lsattr /home
-------------------- /home/s306
# chattr +a -R /home/s306
# lsattr /home
-----a-------------- /home/s306



ftp://s306@192.168.1.100/40        //測試是否可以正常ftp 及 新增檔案


# ps aux | grep s306
root        9712  0.0  0.1 334996  6276 pts/0    S    15:01   0:00 su -l s306
s306        9713  0.0  0.1 232912  3872 pts/0    S    15:01   0:00 -bash
root        9802  0.0  0.0 221896  1068 pts/0    S+   15:12   0:00 grep --color=auto s306



# kill  -9 1394    //送出 kill ,訊號9,強制結束該行程     //??pietty 連線會中斷

# chattr -a -R /home/s306


# userdel -rf s306
# cat /etc/group
# cat /etc/passwd
# cat /etc/shadow
# ls -l /home

# dnf -y remove vsftpd









||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Apache ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 



# dnf list | grep httpd
...
httpd.x86_64                                         2.4.37-30.module_el8.3.0+561+97fdbbcc            @AppStream
...


# dnf -y install httpd
...
已安裝:
 httpd-2.4.37-30.module_el8.3.0+561+97fdbbcc.x86_64
 ...


# dnf list installed grep httpd
已安裝軟體包
...
httpd.x86_64   2.4.37-30.module_el8.3.0+561+97fdbbcc    @AppStream



# ls /usr/sbin | grep httpd
httpd

# ls /usr/lib/systemd/system | grep httpd
httpd.service
httpd@.service
httpd.service.d
httpd.socket
httpd.socket.d

# ls /etc/httpd/conf
httpd.conf  magic

# ls /var/www
cgi-bin  html



# systemctl start httpd.service

# systemctl enable httpd.service

# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled>
   Active: active (running) since Sun 2020-12-13 16:19:06 CST; 24>
...



# ps -aux | grep httpd
...
apache      1893  0.0  0.2 294188  8484 ?        S    16:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache      1894  0.0  0.3 1810712 12324 ?       Sl   16:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache      1896  0.0  0.4 1941848 16412 ?       Sl   16:19   0:00 /usr/sbin/httpd -DFOREGROUND
apache      1907  0.0  0.3 1810712 12324 ?       Sl   16:19   0:00 /usr/sbin/httpd -DFOREGROUND





# firewall-cmd --get-services | grep http
...
availability http https imap imaps ipp
...


# firewall-cmd --get-zones
block dmz drop external home internal public trusted work

# firewall-cmd --get-active-zone
public


# firewall-cmd --zone=public --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: cockpit dhcpv6-client ftp ssh
...

# firewall-cmd --permanent --zone=public --add-service=http
success


# firewall-cmd --reload
success
# firewall-cmd --zone=public --list-all           public (active)
 ...
  services: cockpit dhcpv6-client ftp http ssh
...


HostOS
http://192.168.1.100/



# vi /var/www/html/01.html
This page is on /var/www/html ...

http://192.168.1.100/01.html
This page is on /var/www/html ...



-----------------------------
----------------------------- 設定虛擬目錄 Alias -----------------------------
-----------------------------

////////////////////////////////////////////////////////系統目錄下
# ls /
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr


# mkdir /opt/web

# vi /opt/web/02.html
This page is on /opt/web ...

 

# ls -l /
...
drwxr-xr-x.   2 root root    6 12月 12 15:19 home
...
drwxr-xr-x.   3 root root   17 12月 13 16:47 opt
...


# ls -l /opt
drwxr-xr-x. 2 root root 21 12月 13 16:48 web

# ls -l /opt/web
-rw-r--r--. 1 root root 29 12月 13 16:48 02.html





# vi /etc/httpd/conf/httpd.conf
...    //最後加上
Alias /opt_web /opt/web
<Directory /opt/web>
        Require all granted
</Directory>   



# systemctl restart httpd.service

http://192.168.1.100/opt_web/02.html
This page is on /opt/web ...



//////////////////////////////////////////////////////// home目錄下

# mkdir /home/web

# vi /home/web/03.html
This page is on /home/web ...

# ls -l /
...
drwxr-xr-x.   3 root root   17 12月 13 17:06 home
...

# ls -l /home
drwxr-xr-x. 2 root root 21 12月 13 17:07 web

# ls -l /home/web
-rw-r--r--. 1 root root 30 12月 13 17:07 03.html



# vi /etc/httpd/conf/httpd.conf
...
Alias /home_web /home/web
<Directory /home/web>
        Require all granted
</Directory>


# systemctl restart httpd.service


http://192.168.1.100/home_web/03.html
Forbidden
You don't have permission to access this resource.





# tail -1 /var/log/httpd/error_log
[Sun Dec 13 17:17:59.229269 2020] [core:error] [pid 2902:tid 139709336037120] (13)Permission denied: [client 192.168.1.105:65439] AH00132: file permissions deny server access: /home/web/03.html


# man ls
...
-Z, --context
              print any security context of each file
...



# ls -Z /var/www
system_u:object_r:httpd_sys_content_t:s0 html

# ls -Z /var/www/html
unconfined_u:object_r:httpd_sys_content_t:s0 01.html


# ls -Z /opt
unconfined_u:object_r:usr_t:s0 web

# ls -Z /opt/web
unconfined_u:object_r:usr_t:s0 02.html

# ls -Z /home
unconfined_u:object_r:user_home_dir_t:s0 web

# ls -Z /home/web
unconfined_u:object_r:user_home_t:s0 03.html

# chcon -t httpd_sys_content_t /home/web

# ls -Z /home
unconfined_u:object_r:httpd_sys_content_t:s0 web

# ls -Z /home/web
unconfined_u:object_r:user_home_t:s0 03.html

# chcon -t httpd_sys_content_t /home/web/03.html

# ls -Z /home/web
unconfined_u:object_r:httpd_sys_content_t:s0 03.html


http://192.168.1.100/home_web/03.html
This page is on /home/web ...


# vi /home/web/04.html
This page is on /home/web TOO ....

# ls -Z /home/web
unconfined_u:object_r:httpd_sys_content_t:s0 03.html
unconfined_u:object_r:httpd_sys_content_t:s0 04.html




http://192.168.1.100/home_web/04.html
This page is on /home/web TOO ....



@@@ 若要在 /home 下建立網站虛擬目錄,先
 
# chcon -t httpd_sys_content_t /home/web

再建立網頁,就不須再變更各網頁





-----------------------------
----------------------------- UserDir個人網頁 -----------------------------
-----------------------------

# dnf -y install httpd
# systemctl start httpd.service
# systemctl enable httpd.service
# systemctl status httpd.service


# firewall-cmd --get-active-zone
public
  interfaces: ens33

# firewall-cmd --zone=public --list-all
public (active)
  ...
  services: cockpit dhcpv6-client ssh
...

# firewall-cmd --permanent --zone=public --add-service=http
success

# firewall-cmd --reload
success

# firewall-cmd --zone=public --list-all
public (active)
  ...
  services: cockpit dhcpv6-client http ssh



http://172.31.145.99/






# useradd kk1
# passwd kk1

# mkdir /home/kk1/public_html


****************************************************
//使用者家目錄的參考,建立 /etc/skel/public_html 這個目錄,那麼未來新增使用者後,在他的家目錄下就會有 public_html 那個目錄了

# mkdir /etc/skel/public_html
# ls /etc/skel/
public_html

# useradd kk1
# passwd kk1

****************************************************


# ls -l /home/
drwx------. 3 kk1 kk1 81 12月 14 23:17 kk1

# chmod 711 /home/kk1
# ls -l /home/
drwx--x--x. 3 kk1  kk1  81 12月 18 13:51 kk1


# ls -l /home/kk1
drwxr-xr-x. 2 root root 6 12月 14 23:17 public_html


# vi /home/kk1/public_html/index.html
This page is on /home/kk1/public_html...


# vi /etc/httpd/conf.d/userdir.conf
...
#    UserDir disabled
...
    UserDir public_html
...


# systemctl restart httpd.service


http://172.31.145.99/~kk1
Forbidden
You don't have permission to access this resource.


# tail -1 /var/log/httpd/error_log
[Fri Dec 18 13:58:02.211753 2020] [core:error] [pid 10084:tid 139974091257600] (13)Permission denied: [client 172.31.145.100:50148] AH00035: access to /~kk1 denied (filesystem path '/home/kk1/public_html') because search permissions are missing on a component of the path


# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      31


//Enforcing:SELinux 正在執行; Permissive :臨時關閉; 如果是 Disabled, 即已經關閉
# getenforce
Enforcing

# setenforce 0

# getenforce
Permissive


****************************************************
永久關閉 SELinux
# vi /etc/sysconfig/selinux
...
#SELINUX=enforcing
SELINUX=disabled

儲存檔案後重新啟動系統便會永久關閉 SELinux
****************************************************


http://172.31.145.99/~kk1/
This page is on /home/kk1/public_html...

由此可知是SELinux

再將 SELinux 執行
# setenforce 1
# getenforce
Enforcing


# getsebool -a | grep httpd
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
...

# setsebool -P httpd_enable_homedirs on

http://172.31.145.99/~kk1/
This page is on /home/kk1/public_html...





-----------------------------
----------------------------- VirtualHost 虛擬主機 -----------------------------
-----------------------------

# dnf -y install httpd

# systemctl start httpd.service
# systemctl enable httpd.service
# systemctl status httpd.service
# firewall-cmd --permanent --zone=public --add-service=http
success
# firewall-cmd --reload
success

http://172.31.145.99/


# mkdir /var/www/asus
# vi /var/www/asus/index.html
This is <h1>Asus</h1>web...

# mkdir /var/www/acer
# vi /var/www/acer/index.html
This is <h1>Acer</h1>web...



# vi /etc/httpd/conf/httpd.conf
...
<VirtualHost *:80>
        DocumentRoot /var/www/html
        ServerName 172.31.145.99
</VirtualHost>


<VirtualHost *:80>
        DocumentRoot /var/www/asus
        ServerName web.asus.com.tw
</VirtualHost>
<Directory "/var/www/asus">
    Options Indexes
    AllowOverride None
    Require all granted
</Directory>


<VirtualHost *:80>
        DocumentRoot /var/www/acer
        ServerName web.acer.com.tw
</VirtualHost>
<Directory "/var/www/acer">
    Options Indexes
    AllowOverride None
    Require all granted
</Directory>




# systemctl reload httpd.service


修改 windows10 hosts 檔
開啟記事本,以系統管理員身分執行
檔案-->開啟舊檔:C:\Windows\System32\drivers\etc\hosts
新增資料:
172.31.145.99 web.asus.com.tw  web.acer.com.tw

完成後-->檔案-->儲存檔案





http://web.asus.com.tw/
This is
Asus
web...

http://web.acer.com.tw/
This is
Acer
web...






???????????????????????????以下作廢
-----------------------------
----------------------------- php + MariaDB -----------------------------
-----------------------------
DDDDDDDDDDDDDDDDDD
# dnf -y install httpd

# systemctl start httpd.service
# systemctl enable httpd.service
# systemctl status httpd.service

# firewall-cmd --permanent --zone=public --add-service=http
# firewall-cmd --reload

http://172.31.145.99/



# dnf -y install php

# dnf list installed | grep php
php.x86_64                           7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-cli.x86_64                       7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-common.x86_64                    7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-fpm.x86_64                       7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream


# rpm -qa | grep php
php-cli-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-fpm-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-common-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64




# ls /usr/sbin | grep php
php-fpm

# ls /usr/lib/systemd/system | grep php
php-fpm.service


# systemctl | grep php
php-fpm.service   

# ls /etc/php.ini
/etc/php.ini









# systemctl restart httpd.service


# vi /var/www/html/test.php
<?php
        phpinfo();
php?>


http://172.31.145.99/test.php









++++++++++++++++++++++ MariaDB ++++++++++++++++


# dnf -y install httpd
# systemctl start httpd.service
# systemctl enable httpd.service
# systemctl status httpd.service
# firewall-cmd --permanent --zone=public --add-service=http
# firewall-cmd --reload

http://172.31.145.99/


# dnf -y install php
# systemctl restart httpd.service

# vi /var/www/html/test.php
<?php
echo "kk";
phpinfo();
?>

http://192.168.1.100/test.php




# dnf list | grep mariadb
mariadb.x86_64                                         3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
...
mariadb-server.x86_64                              3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
...


# dnf -y install mariadb-server

# dnf list installed | grep mariadb
mariadb.x86_64                       3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-backup.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-common.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-connector-c.x86_64           3.0.7-1.el8                             @AppStream
mariadb-connector-c-config.noarch    3.0.7-1.el8                             @AppStream
mariadb-errmsg.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-gssapi-server.x86_64         3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-server.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-server-utils.x86_64          3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream


# rpm -qa | grep mariadb
mariadb-server-utils-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64
mariadb-connector-c-config-3.0.7-1.el8.noarch
mariadb-connector-c-3.0.7-1.el8.x86_64
mariadb-errmsg-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64
mariadb-gssapi-server-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64
mariadb-server-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64
mariadb-common-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64
mariadb-backup-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64
mariadb-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64

# ls /usr/lib/systemd/system | grep mariadb
mariadb@bootstrap.service.d
mariadb.service
mariadb@.service



# systemctl start mariadb.service
# systemctl enable mariadb.service
# systemctl status mariadb.service
# systemctl is-active mariadb.service
active


# mysqladmin -u root password db123456
# mysql -u root -p
Enter password:


> select @@datadir;
+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+




--------------- 伺服器字元集 character set 和連線校對 collaction   -------------
 
> show variables like 'character_set_server';
+----------------------+--------+
| Variable_name        | Value  |
+----------------------+--------+
| character_set_server | latin1 |
+----------------------+--------+


> set character_set_server='utf8';
Query OK, 0 rows affected (0.000 sec)

> show variables like 'character_set_server';
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| character_set_server | utf8  |
+----------------------+-------+




> show variables like 'collation_server';
+------------------+-----------------+
| Variable_name    | Value           |
+------------------+-----------------+
| collation_server | utf8_general_ci |
+------------------+-----------------+


> set collation_server='utf8_unicode_ci';
Query OK, 0 rows affected (0.000 sec)

> show variables like 'collation_server';
+------------------+-----------------+
| Variable_name    | Value           |
+------------------+-----------------+
| collation_server | utf8_unicode_ci |
+------------------+-----------------+





--------------- 資料庫字元集 character set 和連線校對 collaction   -------------

> show variables like 'character_set_database';
+------------------------+--------+
| Variable_name          | Value  |
+------------------------+--------+
| character_set_database | latin1 |
+------------------------+--------+


> set character_set_database='utf8';
Query OK, 0 rows affected (0.000 sec)

> show variables like 'character_set_database';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| character_set_database | utf8  |
+------------------------+-------+



> show variables like 'collation_database';
+--------------------+-----------------+
| Variable_name      | Value           |
+--------------------+-----------------+
| collation_database | utf8_general_ci |
+--------------------+-----------------+


> set collation_database='utf8_unicode_ci';
Query OK, 0 rows affected (0.000 sec)

> show variables like 'collation_database';
+--------------------+-----------------+
| Variable_name      | Value           |
+--------------------+-----------------+
| collation_database | utf8_unicode_ci |
+--------------------+-----------------+




> show variables like '%database';
+------------------------+-----------------+
| Variable_name          | Value           |
+------------------------+-----------------+
| character_set_database | utf8            |
| collation_database     | utf8_unicode_ci |
| skip_show_database     | OFF             |
+------------------------+-----------------+








> create database testdb character set='utf8' collate='utf8_unicode_ci';

> show variables like '%database';
+------------------------+-------------------+
| Variable_name          | Value             |
+------------------------+-------------------+
| character_set_database | latin1            |
| collation_database     | latin1_swedish_ci |
| skip_show_database     | OFF               |
+------------------------+-------------------+

> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| testdb             |
+--------------------+

> use testdb;

> create table tblmen (mid int primary key auto_increment, na varchar(20), accnt varchar(20), pwd varchar(20));

> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| tblmen           |
+------------------+

> describe tblmen;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| mid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| na    | varchar(20) | YES  |     | NULL    |                |
| accnt | varchar(20) | YES  |     | NULL    |                |
| pwd   | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

> show create table tblmen \G;
*************************** 1. row ***************************
       Table: tblmen
Create Table: CREATE TABLE `tblmen` (
  `mid` int(11) NOT NULL AUTO_INCREMENT,
  `na` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `accnt` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `pwd` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`mid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci




> insert into tblmen values(1,'蔡英九','act1','pwd1');    //AUTO_INCREMENT
> select * from tblmen;
+-----+-----------+-------+------+
| mid | na        | accnt | pwd  |
+-----+-----------+-------+------+
|   1 | 蔡英九    | act1  | pwd1 |
+-----+-----------+-------+------+


//Table 匯出 文字檔
> select * into outfile 'tblmen.txt' from tblmen;

//離開 MariaDB
# ls /var/lib/mysql/testdb
db.opt  tblmen.frm  tblmen.ibd  tblmen.txt

# cat /var/lib/mysql/testdb/tblmen.txt
1       蔡英九  act1    pwd1








注意!PHP7已經不支援MySQL系列函數,而是改用MySQLi,或者PDO物件。


display_errors = On



# ls /var/log
anaconda  dnf.librepo.log  firewalld   lastlog  private  wtmp
audit     dnf.log          hawkey.log  mariadb  sssd
btmp      dnf.rpm.log      httpd       php-fpm  tuned


# ls /var/log/php-fpm
error.log  www-error.log

# tail -1 /var/log/php-fpm/error.log
[20-Dec-2020 09:07:12] NOTICE: systemd monitor interval set to 10000ms

# tail -1 /var/log/php-fpm/www-error.log
thrown in /var/www/html/0.php on line 2


# ls /var/log/httpd
access_log  error_log

# tail -1 /var/log/httpd/access_log
192.168.1.104 - - [20/Dec/2020:10:41:57 +0800] "-" 408 - "-" "-"

# tail -1 /var/log/httpd/error_log
[Sun Dec 20 08:59:24.949687 2020] [core:notice] [pid 11417:tid 140192777812288] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'




# ls /var/log/mariadb
mariadb.log

# tail -1 /var/log/mariadb/mariadb.log
Version: '10.3.17-MariaDB'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server





<?php
ini_set("display_errors","On");
error_reporting(E_ALL);

$con= mysql('localhost','root','db123456','testdb');
if (mysqli_cinnect_error()){
        echo "錯誤:".mysqli_connect_error()."<br>";
}
?>


++++++++++++++++++++++++++++++++++++++++++++++++ php

# dnf list | grep php
php.x86_64                                             7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-bcmath.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-cli.x86_64                                         7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-common.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-dba.x86_64                                         7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-dbg.x86_64                                         7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-devel.x86_64                                       7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-embedded.x86_64                                    7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-enchant.x86_64                                     7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-fpm.x86_64                                         7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-gd.x86_64                                          7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-gmp.x86_64                                         7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-intl.x86_64                                        7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-json.x86_64                                        7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-ldap.x86_64                                        7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-mbstring.x86_64                                    7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-mysqlnd.x86_64                                     7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-odbc.x86_64                                        7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-opcache.x86_64                                     7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-pdo.x86_64                                         7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-pear.noarch                                        1:1.10.5-9.module_el8.2.0+313+b04d0a66           AppStream
php-pecl-apcu.x86_64                                   5.1.12-2.module_el8.2.0+313+b04d0a66             AppStream
php-pecl-apcu-devel.x86_64                             5.1.12-2.module_el8.2.0+313+b04d0a66             AppStream
php-pecl-zip.x86_64                                    1.15.3-1.module_el8.2.0+313+b04d0a66             AppStream
php-pgsql.x86_64                                       7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-process.x86_64                                     7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-recode.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-snmp.x86_64                                        7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-soap.x86_64                                        7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-xml.x86_64                                         7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream
php-xmlrpc.x86_64                                      7.2.24-1.module_el8.2.0+313+b04d0a66             AppStream




# dnf -y install php php-fpm php-gd php-mbstring php-mysqlnd
# dnf list installed | grep php
php.x86_64                           7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-cli.x86_64                       7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-common.x86_64                    7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-fpm.x86_64                       7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-gd.x86_64                        7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-mbstring.x86_64                  7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-mysqlnd.x86_64                   7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-pdo.x86_64                       7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream


# dnf list installed | grep php

# rpm -qa | grep php

# ls /usr/sbin | grep php
php-fpm

# ls /usr/lib/systemd/system | grep php
php-fpm.service


# systemctl | grep php
php-fpm.service   

# ls /etc/php.ini
/etc/php.ini




# vi /var/www/html/0.php
<?php
phpinfo();
?>


# dnf -y install mariadb mariadb-server
# dnf list installed | grep mariadb
mariadb.x86_64                       3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-backup.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-common.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-connector-c.x86_64           3.0.7-1.el8                             @AppStream
mariadb-connector-c-config.noarch    3.0.7-1.el8                             @AppStream
mariadb-errmsg.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-gssapi-server.x86_64         3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-server.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-server-utils.x86_64          3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
# systemctl start mariadb
# systemctl enable mariadb



# vi /etc/php.ini
...
extension=mysqli
...

# systemctl restart httpd
or
# systemctl restart php-fpm





# vi /var/www/html/1.php
<?php
ini_set("display_errors","On");
error_reporting(E_ALL);

$con= mysqli_connect('localhost','root','db123456','testdb');

echo $kk;


?>











-----------------------------------------------------------------------


# systemctl status httpd
# systemctl status php-fpm
# systemctl status mariadb


<?php

$host = 'localhost';
$db = 'testdb';
$user = 'root';
$pwd = 'db123456';
$link=new pdo("mysql:host=$host;dbname=$db", $user, $pwd);

?>




-----------------------------------------------------------
# vi /var/www/html/list.php

<?php

$host = 'localhost';
$db = 'testdb';
$user = 'root';
$pwd = 'db123456';
$db_link=new pdo("mysql:host=$host;dbname=$db", $user, $pwd);
$db_link->exec("set character set utf8");

$rt=$db_link->query("select * from tblmen");
$rows=$rt->fetch();
?>

<html>
<head></head>
<body>
<h1 align="center">學生系統</h1>
<p align="center"><a href="add.php">新增學生資料</a>   <a href="query.php">查詢學生資料</a></p>
<table border=1 align="center">
<tr><td>編號</td><td>名字</td><td>帳號</td><td>密碼</td></tr>

<?php
while (!empty($rows)){
        echo "<tr>";
        echo "<td>".$rows["mid"]."</td>";
        echo "<td>".$rows["na"]."</td>";
        echo "<td>".$rows["accnt"]."</td>";
        echo "<td>".$rows["pwd"]."</td>";
        echo "<td><a href='update.php?mid=".$rows["mid"]."'>修改</a></td>";
        echo "<td><a href='delete.php?mid=".$rows["mid"]."'>刪除</a></td>";
        echo "</tr>";

        $rows=$rt->fetch();
}
$db_link=null;


?>

</table>
</body>
</html>








-----------------------------------------------------------

# vi /var/www/html/add.php

<?php
if ($_POST["act"]=="add"){
    $host = 'localhost';
    $db = 'testdb';
    $user = 'root';
    $pwd = 'db123456';
    $db_link=new pdo("mysql:host=$host;dbname=$db", $user, $pwd);
    $db_link->exec("set character set utf8");

    $rt=$db_link->query("INSERT INTO `tblmen`(`mid`, `na`, `accnt`, `pwd`) VALUES ('".$_POST["mid"]."','".$_POST["na"]."','".$_POST["accnt"]."','".$_POST["pwd"]."')");

    header("Location:list.php");
}
?>

<html>
<head></head>
<body>
<h1 align="center">新增資料</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formAdd">
<table align="center">
<tr><td>編號</td><td><input type="text" name="mid"></td></tr>
<tr><td>名字</td><td><input type="text" name="na"></td></tr>
<tr><td>帳號</td><td><input type="text" name="accnt"></td></tr>
<tr><td>密碼</td><td><input type="text" name="pwd"></td></tr>
<tr><td></td><td>
<input type="hidden" value="add" name="act">
<input type="submit" value="新增" name="ok">
<input type="reset" value="清除" name="erase">
</td></tr>
</table>
</form>
</body>
</html>








-----------------------------------------------------------

# vi /var/www/html/update.php



<?php

$host = 'localhost';
$db = 'testdb';
$user = 'root';
$pwd = 'db123456';
$db_link=new pdo("mysql:host=$host;dbname=$db", $user, $pwd);
$db_link->exec("set character set utf8");



if ($_POST["act"]=="update"){
        $str="update tblmen set na='".$_POST["na"]."',accnt='".$_POST["accnt"]."',pwd='".$_POST["pwd"]."' where mid =".$_POST["mid"];
        header("Location:list.php");
}
else{
    $str="select * from tblmen where mid=".$_GET["mid"];
}



$rt=$db_link->query($str);
$rows=$rt->fetch();

?>

<html>
<head></head>
<body>
<h1 align="center">修改資料</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formUpdate">
<table align="center">
<tr><td>編號</td><td><input type="text" name="mid" value="<?php echo $rows["mid"]; ?>" disabled></td></tr>
<tr><td>名字</td><td><input type="text" name="na" value="<?php echo $rows["na"]; ?>"></td></tr>
<tr><td>帳號</td><td><input type="text" name="accnt" value="<?php echo $rows["accnt"]; ?>"></td></tr>
<tr><td>密碼</td><td><input type="text" name="pwd" value="<?php echo $rows["pwd"]; ?>"></td></tr>

<tr><td></td><td>
<input type="hidden" value="<?php echo $rows["mid"]; ?>" name="mid">
<input type="hidden" value="update" name="act">
<input type="submit" value="更新" name="ok">
<input type="reset" value="清除" name="erase">
</td></tr>
</table>
</form>
</body>









-----------------------------------------------------------


# vi /var/www/html/query.php
<html>
<head></head>
<body>
<h1 align="center">會員查詢</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formQuery">
<table align="center">
<tr><td>姓名</td><td><input type="text" name="na"></td><td></td><td></td></tr>
<tr><td></td><td>
<input type="hidden" value="query" name="act">
<input type="submit" value="查詢" name="ok">
</td><td></td><td></td></tr>

<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>

<tr><td>編號</td><td>姓名</td><td>帳號</td><td>密碼</td></tr>

<?php
if ($_POST["act"]=="query"){
        $db_link=mysql_connect("localhost","root","db123456");


        mysql_query("SET NAMES 'utf8'");


        $db_sel=mysql_select_db("kkdb");
        $str_sel="SELECT * FROM `tblmen` WHERE `na` LIKE '%".$_POST["na"]."%'";
//echo $str_sel;
        $result=mysql_query($str_sel);
        $data_cnt=mysql_num_rows($result);

       if ($data_cnt!=0){
                while ($tbl_rows=mysql_fetch_assoc($result)){
                        echo "<tr>";
                        echo "<td>".$tbl_rows["mid"]."</td>";
                        echo "<td>".$tbl_rows["na"]."</td>";
                        echo "<td>".$tbl_rows["accnt"]."</td>";
                        echo "<td>".$tbl_rows["pwd"]."</td>";
                        echo "</tr>";
                }
        }
}
?>



</table>
</form>
</body>
</html>






=========================

vi /var/www/html/query.php

<?php

ini_set("display_errors","On");
error_reporting(E_ALL);


if ($_POST["act"]=="query"){
        $host = 'localhost';
        $db = 'testdb';
        $user = 'root';
        $pwd = 'db123456';
        $db_link=new pdo("mysql:host=$host;dbname=$db", $user, $pwd);
        $db_link->exec("set character set utf8");

        $rt=$db_link->query("SELECT * FROM `tblmen` WHERE `na` LIKE '%".$_POST["na"]."%'");
        $data_cnt=mysqli_num_rows($rt);

       if ($data_cnt!=0){
                while ($rows=mysqli_fetch_assoc($rt)){
                        echo "<tr>";
                        echo "<td>".$rows["mid"]."</td>";
                        echo "<td>".$rows["na"]."</td>";
                        echo "<td>".$rows["accnt"]."</td>";
                        echo "<td>".$rows["pwd"]."</td>";
                        echo "</tr>";
                }
        }
}
?>


<html>
<head></head>
<body>
<h1 align="center">會員查詢</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formQuery">
<table align="center">
<tr><td>姓名</td><td><input type="text" name="na"></td><td></td><td></td></tr>
<tr><td></td><td>
<input type="hidden" value="query" name="act">
<input type="submit" value="查詢" name="ok">
</td><td></td><td></td></tr>

<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td></tr>

<tr><td>編號</td><td>姓名</td><td>帳號</td><td>密碼</td></tr>




+++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++12/23
+++++++++++++++++++++++++++++++++++++++++++


# dnf -y install httpd
# systemctl start httpd
# systemctl enable httpd
# systemctl status httpd
# firewall-cmd --permanent --zone=public --add-service=http
# firewall-cmd --reload

http://172.31.145.99/






# dnf list | grep php

# dnf -y install php php-fpm php-gd php-mbstring php-mysqlnd

# dnf list installed | grep  php
php.x86_64                           7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-cli.x86_64                       7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-common.x86_64                    7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-fpm.x86_64                       7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-gd.x86_64                        7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-mbstring.x86_64                  7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-mysqlnd.x86_64                   7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream
php-pdo.x86_64                       7.2.24-1.module_el8.2.0+313+b04d0a66   @AppStream


# dnf list installed | grep php

# rpm -qa | grep php

# ls /usr/sbin | grep php
php-fpm

# ls /usr/lib/systemd/system | grep php
php-fpm.service


# systemctl | grep php
php-fpm.service   

# ls /etc/php.ini
/etc/php.ini



# systemctl restart httpd

# vi /var/www/html/0.php

<?php

ini_set("display_errors","On");   

//  
# vi /etc/php.ini 
...
display_errors = On
...

# systemctl restart httpd

無效!!!!!!
//

echo "kk<br>";
echo "瀏覽器中文編碼是否正常?<br>";

$sid="s01";
$str1="select * from tblmen where sid='".$sid."'";
$str2="select * from tblmen where sid='$sid'";        //php 字串以雙引號包起來,單引號會解析出其內容

echo $str1."<br>";
echo $str2."<br>";
echo $str3;

phpinfo();

?>


http://172.31.145.99/0.php
kk
編碼是否正常?
select * from tblmen where sid='s01'
select * from tblmen where sid='s01'

Notice: Undefined variable: str3 in /var/www/html/1.php on line 15
...



//刪除編輯錯誤時所產生的暫存檔
# rm -rf /var/www/html/.1.php.swp





# dnf list | grep mariadb
mariadb.x86_64                                         3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-backup.x86_64                            3:10.3.17-1.module_el8.1.0+257+48736ea6     AppStream
mariadb-common.x86_64                          3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-connector-c.i686                          3.0.7-1.el8                                      AppStream
mariadb-connector-c.x86_64                             3.0.7-1.el8                                      AppStream
mariadb-connector-c-config.noarch                      3.0.7-1.el8                                      AppStream
mariadb-connector-c-devel.i686                         3.0.7-1.el8                                      AppStream
mariadb-connector-c-devel.x86_64                       3.0.7-1.el8                                      AppStream
mariadb-connector-odbc.x86_64                          3.0.7-1.el8                                      AppStream
mariadb-devel.x86_64                               3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-embedded.x86_64                       3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-embedded-devel.x86_64             3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-errmsg.x86_64                             3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-gssapi-server.x86_64                   3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-java-client.noarch                                                 2.2.5-3.el8                                      AppStream
mariadb-oqgraph-engine.x86_64               3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-server.x86_64                               3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-server-galera.x86_64                    3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-server-utils.x86_64                       3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream
mariadb-test.x86_64                                    3:10.3.17-1.module_el8.1.0+257+48736ea6          AppStream



# dnf -y install mariadb-server

# dnf list installed | grep mariadb
mariadb.x86_64                       3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-backup.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-common.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-connector-c.x86_64           3.0.7-1.el8                             @AppStream
mariadb-connector-c-config.noarch    3.0.7-1.el8                             @AppStream
mariadb-errmsg.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-gssapi-server.x86_64         3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-server.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-server-utils.x86_64          3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream



# systemctl start mariadb
# systemctl enable mariadb
# systemctl status mariadb


# mysqladmin -u root password db123456
# mysql -u root -p
Enter password:



MariaDB [(none)]> select @@datadir;
+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+





--------------- 資料庫字元集 character set 和連線校對 collaction   -------------

MariaDB [(none)]> set character_set_database='utf8';
MariaDB [(none)]> set collation_database='utf8_unicode_ci';
MariaDB [(none)]> show variables like '%database';
+------------------------+-----------------+
| Variable_name          | Value           |
+------------------------+-----------------+
| character_set_database | utf8            |
| collation_database     | utf8_unicode_ci |
| skip_show_database     | OFF             |
+------------------------+-----------------+




MariaDB [(none)]> create database kkdb;

MariaDB [(none)]> show variables like '%database';
+------------------------+-----------------+
| Variable_name          | Value           |
+------------------------+-----------------+
| character_set_database | utf8            |
| collation_database     | utf8_unicode_ci |
| skip_show_database     | OFF             |
+------------------------+-----------------+

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kkdb               |
| mysql              |
| performance_schema |
+--------------------+



MariaDB [(none)]> show create database kkdb \G;
*************************** 1. row ***************************
       Database: kkdb
Create Database: CREATE DATABASE `kkdb` /*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.000 sec)

ERROR: No query specified




MariaDB [(none)]> use kkdb;
Database changed


MariaDB [kkdb]> create table tblmen (mid int primary key auto_increment, na varchar(20), accnt varchar(20), pwd varchar(20));

MariaDB [kkdb]> show tables;
+----------------+
| Tables_in_kkdb |
+----------------+
| tblmen         |
+----------------+


MariaDB [kkdb]> describe tblmen;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| mid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| na    | varchar(20) | YES  |     | NULL    |                |
| accnt | varchar(20) | YES  |     | NULL    |                |
| pwd   | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+


MariaDB [kkdb]> show create table tblmen \G;
*************************** 1. row ***************************
       Table: tblmen
Create Table: CREATE TABLE `tblmen` (
  `mid` int(11) NOT NULL AUTO_INCREMENT,
  `na` varchar(20) DEFAULT NULL,
  `accnt` varchar(20) DEFAULT NULL,
  `pwd` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`mid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

?????? 如此作法有問題........

--------------- 以上  資料庫字元集 character set 和連線校對 collaction   -------------










//移除重裝
# dnf list installed | grep mariadb
//沒效,原產生資料庫還在



!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!





# dnf -y install mariadb-server

# dnf list installed | grep mariadb
mariadb.x86_64                       3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-backup.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-common.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-connector-c.x86_64           3.0.7-1.el8                             @AppStream
mariadb-connector-c-config.noarch    3.0.7-1.el8                             @AppStream
mariadb-errmsg.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-gssapi-server.x86_64         3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-server.x86_64                3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream
mariadb-server-utils.x86_64          3:10.3.17-1.module_el8.1.0+257+48736ea6 @AppStream



# systemctl start mariadb
# systemctl enable mariadb
# systemctl status mariadb


# mysqladmin -u root password db123456
# mysql -u root -p
Enter password:

MariaDB [(none)]> select @@datadir;
+-----------------+
| @@datadir       |
+-----------------+
| /var/lib/mysql/ |
+-----------------+

MariaDB [(none)]> create database kkdb character set='utf8' collate='utf8_unicode_ci';

MariaDB [(none)]> show variables like '%database';
+------------------------+-------------------+
| Variable_name          | Value             |
+------------------------+-------------------+
| character_set_database | latin1            |
| collation_database     | latin1_swedish_ci |
| skip_show_database     | OFF               |
+------------------------+-------------------+

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kkdb               |
| mysql              |
| performance_schema |
+--------------------+


MariaDB [(none)]> use kkdb;
Database changed

MariaDB [kkdb]> create table tblmen (mid int primary key auto_increment, na varchar(20), accnt varchar(20), pwd varchar(20));

MariaDB [kkdb]> show tables;
+----------------+
| Tables_in_kkdb |
+----------------+
| tblmen         |
+----------------+


MariaDB [kkdb]> describe tblmen;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| mid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| na    | varchar(20) | YES  |     | NULL    |                |
| accnt | varchar(20) | YES  |     | NULL    |                |
| pwd   | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

MariaDB [kkdb]> show create table tblmen \G;
*************************** 1. row ***************************
       Table: tblmen
Create Table: CREATE TABLE `tblmen` (
  `mid` int(11) NOT NULL AUTO_INCREMENT,
  `na` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `accnt` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `pwd` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`mid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci






MariaDB [kkdb]> insert into tblmen values(1,'蔡英九','act1','pwd1');    //AUTO_INCREMENT 無效

MariaDB [kkdb]> insert into tblmen (na,accnt,pwd) values('關羽','act5','pwd5');    //ok!

MariaDB [kkdb]> select * from tblmen;
+-----+-----------+-------+------+
| mid | na        | accnt | pwd  |
+-----+-----------+-------+------+
|   1 | 蔡英九    | act1  | pwd1 |
+-----+-----------+-------+------+


MariaDB [kkdb]> select * into outfile 'tblmen.txt' from tblmen;

MariaDB [kkdb]> Ctrl-C -- exit!
Aborted


# cat /var/lib/mysql/kkdb/tblmen.txt
1       蔡英九  act1    pwd1



===========================

# vi /var/www/html/LinkServer.php

<?php
$host = 'localhost';
$db = 'kkdb';
$user = 'root';
$pwd = 'db123456';
$db_link=new pdo("mysql:host=$host;dbname=$db", $user, $pwd);
$db_link->exec("set character set utf8");
?>





===========================

# vi /var/www/html/list.php

<?php
include("LinkServer.php");

$rt=$db_link->query("select * from tblmen");
$rows=$rt->fetch();
?>

<html>
<head></head>
<body>
<h1 align="center">學生系統</h1>
<p align="center"><a href="add.php">新增學生資料</a>   <a href="query.php">查詢學生資料</a></p>
<table border=1 align="center">
<tr><td>編號</td><td>名字</td><td>帳號</td><td>密碼</td></tr>

<?php
while (!empty($rows)){
        echo "<tr>";
        echo "<td>".$rows["mid"]."</td>";
        echo "<td>".$rows["na"]."</td>";
        echo "<td>".$rows["accnt"]."</td>";
        echo "<td>".$rows["pwd"]."</td>";
        echo "<td><a href='update.php?mid=".$rows["mid"]."'>修改</a></td>";
        echo "<td><a href='delete.php?mid=".$rows["mid"]."'>刪除</a></td>";
        echo "</tr>";

        $rows=$rt->fetch();
}
$db_link=null;


?>

</table>
</body>
</html>






===========================

# vi /var/www/html/add.php

<?php
if ($_POST["act"]=="add"){
    include("LinkServer.php");

    $rt=$db_link->query("INSERT INTO `tblmen`(`mid`, `na`, `accnt`, `pwd`) VALUES ('".$_POST["mid"]."','".$_POST["na"]."','".$_POST["accnt"]."','".$_POST["pwd"]."')");

    header("Location:list.php");
}
?>

<html>
<head></head>
<body>
<h1 align="center">新增資料</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formAdd">
<table align="center">
<tr><td>編號</td><td><input type="text" name="mid"></td></tr>
<tr><td>名字</td><td><input type="text" name="na"></td></tr>
<tr><td>帳號</td><td><input type="text" name="accnt"></td></tr>
<tr><td>密碼</td><td><input type="text" name="pwd"></td></tr>
<tr><td></td><td>
<input type="hidden" value="add" name="act">
<input type="submit" value="新增" name="ok">
<input type="reset" value="清除" name="erase">
</td></tr>
</table>
</form>
</body>
</html>







===========================

# vi /var/www/html/update.php

<?php

include("LinkServer.php");


if ($_POST["act"]=="update"){
        $str="update tblmen set na='".$_POST["na"]."',accnt='".$_POST["accnt"]."',pwd='".$_POST["pwd"]."' where mid =".$_POST["mid"];
        header("Location:list.php");
}
else{
    $str="select * from tblmen where mid=".$_GET["mid"];
}



$rt=$db_link->query($str);
$rows=$rt->fetch();

?>

<html>
<head></head>
<body>
<h1 align="center">修改資料</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formUpdate">
<table align="center">
<tr><td>編號</td><td><input type="text" name="mid" value="<?php echo $rows["mid"]; ?>" disabled></td></tr>
<tr><td>名字</td><td><input type="text" name="na" value="<?php echo $rows["na"]; ?>"></td></tr>
<tr><td>帳號</td><td><input type="text" name="accnt" value="<?php echo $rows["accnt"]; ?>"></td></tr>
<tr><td>密碼</td><td><input type="text" name="pwd" value="<?php echo $rows["pwd"]; ?>"></td></tr>

<tr><td></td><td>
<input type="hidden" value="<?php echo $rows["mid"]; ?>" name="mid">
<input type="hidden" value="update" name="act">
<input type="submit" value="更新" name="ok">
<input type="reset" value="清除" name="erase">
</td></tr>
</table>
</form>
</body>
</html>





===========================



#vi /var/www/html/query.php


<html>
<head></head>
<body>
<h1 align="center">會員查詢</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
<form action="" method="post" name="formQuery">
<table align="center">
<tr><td>姓名</td><td><input type="text" name="na"></td><td></td><td></td></tr>
<tr><td></td><td>
<input type="hidden" value="query" name="act">
<input type="submit" value="查詢" name="ok">
</td><td></td><td></td></tr>
</table>

<?php
if($_POST["act"]=="query"){
        include("LinkServer.php");
        $rt=$db_link->query("select * from tblmen where na LIKE '%".$_POST["na"]."%'");
        $rows=$rt->fetch();

        echo "<table align='center' >";
        echo "<tr><td>編號</td><td>名字</td><td>帳號</td><td>密碼</td></tr>";
        echo "<tr><td>".$rows["mid"]."</td><td>".$rows["na"]."</td><td>".$rows["accnt"]."</td><td>".$rows["pwd"]."</td></tr>";
        echo "</table>";

}

?>

</form>
</body>
</html>








===========================

# vi /var/www/html/delete.php

<?php
include("LinkServer.php");
$rt=$db_link->query("delete from tblmen where mid ='".$_GET["mid"]."'");
?>

<html>
<head></head>
<body>
<h1 align="center">資料刪除完畢!</h1>
<p align="center"><a href="list.php">返回主畫面</a></p>
</body>
</html>





===========================

# vi /var/www/html/index.php

<?php
if ($_POST["act"]=="login"){
    include("LinkServer.php");
    $rt=$db_link->query("select * from tblmen where accnt ='".$_POST["accnt"]."' and pwd ='".$_POST["pwd"]."'");

    $data_cnt=$rt->rowCount();

        if ($data_cnt==1){
                header("Location:list.php");
        }else{
                header("Location:index.php");
        }

}
?>

<html>
<head></head>
<body>
<h1 align="center">會員登入</h1>
<form action="" method="post" name="formAdd">
<table align="center">
<tr><td>帳號</td><td><input type="text" name="accnt"></td></tr>
<tr><td>密碼</td><td><input type="text" name="pwd"></td></tr>
<tr><td></td><td>
<input type="hidden" value="login" name="act">
<input type="submit" value="登入" name="ok">
</td></tr>
</table>
</form>
</body>
</html>



===========================

//欄位分隔符號,預設"\t"
//每行結束符號,預設"\n" 
MariaDB [kkdb]> select * from tblmen into outfile 'tblmen.txt';

# cat /var/lib/mysql/kkdb/tblmen.txt
1       蔡英九  act1    pwd1
2       蘇太多  act2    pwd2
3       陳偉    act3    pwd3
4       趙少主  act44   pwd44


# vi /var/lib/mysql/kkdb/tblmen.txt
1       蔡英九  act1    pwd1
2       蘇太多  act2    pwd2
3       陳偉    act3    pwd3
4       趙少主  act44   pwd44
5       王政正  act5    pwd5
6       韓國仁  act6    pwd6



MariaDB [kkdb]> delet from tblmen;

MariaDB [kkdb]> load data infile 'tblmen.txt' into table tblmen;

MariaDB [kkdb]> select * from tblmen;
+-----+-----------+-------+-------+
| mid | na        | accnt | pwd   |
+-----+-----------+-------+-------+
|   1 | 蔡英九    | act1  | pwd1  |
|   2 | 蘇太多    | act2  | pwd2  |
|   3 | 陳偉      | act3  | pwd3  |
|   4 | 趙少主    | act44 | pwd44 |
|   5 | 王政正    | act5  | pwd5  |
|   6 | 韓國仁    | act6  | pwd6  |
+-----+-----------+-------+-------+




//備份資料庫,沒寫 table 代表全部 database

# mysqldump -u root -p kkdb > kkdb.bk.sql
Enter password:

# ls
anaconda-ks.cfg  kkdb.bk.sql



//刪除database
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kkdb               |
| mysql              |
| performance_schema |
+--------------------+


MariaDB [(none)]> drop database kkdb;
ERROR 1010 (HY000): Error dropping database (can't rmdir './kkdb', errno: 39 "Directory not empty")


MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kkdb               |
| mysql              |
| performance_schema |
+--------------------+

MariaDB [(none)]> use kkdb;

MariaDB [kkdb]> show tables;
Empty set (0.000 sec)

?????database 還在 但 table 已刪除 
?????因為 在var/lib/mysql/kkdb/ 有曾經匯出的 table 資料

# ls /var/lib/mysql/kkdb/tblmen.txt
/var/lib/mysql/kkdb/tblmen.txt

# rm  /var/lib/mysql/kkdb/tblmen.txt
rm:是否移除普通檔案'/var/lib/mysql/kkdb/tblmen.txt'? y

?????刪除在var/lib/mysql/kkdb/ 曾經匯出的 table 資料 即可完整刪除database
MariaDB [(none)]> drop database kkdb;
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+


# ls /var/lib/mysql/kkdb
ls: 無法存取 '/var/lib/mysql/kkdb': No such file or directory



//資料庫還原
//先建立空資料庫

MariaDB [(none)]> create database kkdb character set='utf8' collate='utf8_unicode_ci';
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kkdb               |
| mysql              |
| performance_schema |
+--------------------+


//還原
# mysql -u root -p kkdb < kkdb.bk.sql
Enter password:

# ls /var/lib/mysql/kkdb
db.opt  tblmen.frm  tblmen.ibd



MariaDB [(none)]> use kkdb;

MariaDB [kkdb]> show tables;
+----------------+
| Tables_in_kkdb |
+----------------+
| tblmen         |
+----------------+


MariaDB [kkdb]> select * from tblmen;
+-----+-----------+-------+-------+
| mid | na        | accnt | pwd   |
+-----+-----------+-------+-------+
|   1 | 蔡英九    | act1  | pwd1  |
|   2 | 蘇太多    | act2  | pwd2  |
|   3 | 陳偉      | act3  | pwd3  |
|   4 | 趙少主    | act44 | pwd44 |
|   5 | 王政正    | act5  | pwd5  |
|   6 | 韓國仁    | act6  | pwd6  |
+-----+-----------+-------+-------+


-----------------------------------------------------------










沒有留言:

張貼留言