NFS是Network File System的縮寫(分佈式文件系統協議),==它最大的功能就是能夠透過網絡,讓不一樣的機器、不一樣的操做系統、能夠彼此分享個別的檔案 (share files)==
NFS最先由Sun公司開發,分2,3,4三個版本,2和3由Sun起草開發,4.0開始Netapp公司參與並主導開發,最新爲4.1版本
NFS數據傳輸基於RPC協議,RPC爲Remote Procedure Call的簡寫。mysql《客戶端NFS和服務端NFS通信過程》
1)首先服務器端啓動RPC服務,並開啓111端口
2)啓動NFS服務,並向RPC註冊端口信息
3)客戶端啓動RPC(portmap服務),向服務端的RPC(portmap)服務請求服務端的NFS端口
4)服務端的RPC(portmap)服務反饋NFS端口信息給客戶端。
5)客戶端經過獲取的NFS端口來創建和服務端的NFS鏈接並進行數據的傳輸。
(注:rpcbind是centos6及之後稱之爲rpcbind,portmap是centos5及之前的版本)sql
場景1:有3臺機器A,B,C,需求是這3臺機器的數據是同樣的,當A的機器負載高時客戶想要訪問A服務器上的數據時,這時有2臺機器B,C的負載不是很高,將A與B,C的數據共享,但是怎麼樣能時實的更新B,與C的機器上呢。
此時用NFS服務便可實現,時實的更新的數據。(NFS不監聽端口,portmap監聽111端口)vim
服務端的配置:centos
[root@chy01 ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.212.11 netmask 255.255.255.0 broadcast 192.168.212.255 inet6 fe80::243b:ddac:7a2b:a5b prefixlen 64 scopeid 0x20<link> ether 00:0c:29:13:b3:3b txqueuelen 1000 (Ethernet) RX packets 5269 bytes 2572740 (2.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1167 bytes 157323 (153.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 (服務端的ip地址) [root@chy01 ~]# yum install -y nfs-utils rpcbind [root@chy01 ~]# vim /etc/exports (nfs的配置文件) /home/nfstestdir 192.168.212.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) (/home/nfstestdir 須要共享的目錄,這個目錄後續須要建立,192.168.212.0/24(給192.168.212.0這個網絡段共享目錄。(rw,sync,all_squash,anonuid=1000,anongid=1000)如此是 [root@chy01 ~]# mkdir /home/nfstestdir [root@chy01 ~]# chmod 777 /home/nfstestdir (建立目錄而且給777的權限) [root@chy01 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1275/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2240/master tcp6 0 0 :::3306 :::* LISTEN 1744/mysqld tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 1275/sshd tcp6 0 0 ::1:25 :::* LISTEN 2240/master (能夠查看到rpc服務已經啓動) [root@chy01 ~]# ps aux |grep nfs root 3502 0.0 0.0 112664 976 pts/0 S+ 07:06 0:00 grep --color=auto nfs (雖然rpc已經啓動,可是nfs服務並無啓動) [root@chy01 ~]# systemctl start nfs (手動啓動nfs) [root@chy01 ~]# ps aux |grep nfs root 3550 0.0 0.0 0 0 ? S< 07:07 0:00 [nfsd4_callbacks] root 3556 0.0 0.0 0 0 ? S 07:07 0:00 [nfsd] root 3557 0.0 0.0 0 0 ? S 07:07 0:00 [nfsd] root 3558 0.0 0.0 0 0 ? S 07:07 0:00 [nfsd] root 3559 0.0 0.0 0 0 ? S 07:07 0:00 [nfsd] root 3560 0.0 0.0 0 0 ? S 07:07 0:00 [nfsd] root 3561 0.0 0.0 0 0 ? S 07:07 0:00 [nfsd] root 3562 0.0 0.0 0 0 ? S 07:07 0:00 [nfsd] root 3563 0.0 0.0 0 0 ? S 07:07 0:00 [nfsd] root 3568 0.0 0.0 112664 976 pts/0 S+ 07:07 0:00 grep --color=auto nfs (查看nfs服務) [root@chy01 ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. (nfs開機啓動)
客戶端簡單配置:服務器
[root@chy ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.212.10 netmask 255.255.255.0 broadcast 192.168.212.255 inet6 fe80::338e:589c:fa07:65e5 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:70:77:62 txqueuelen 1000 (Ethernet) RX packets 8880 bytes 8055224 (7.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1205 bytes 139708 (136.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 (客戶端的IP地址) [root@chy ~]# yum install -y nfs-utils rpcbind (一樣須要安裝nfs-utils包)
nfs配置選項網絡
[root@chy01 ~]# cat /etc/exports /home/nfstestdir 192.168.212.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) rw 讀寫 ro 只讀 sync 同步模式,內存數據實時寫入磁盤 async 非同步模式 no_root_squash 客戶端掛載NFS共享目錄後,root用戶不受約束,權限很大 root_squash 與上面選項相對,客戶端上的root用戶收到約束,被限定成某個普通用戶 all_squash 客戶端上全部用戶在使用NFS共享目錄時都被限定爲一個普通用戶 anonuid/anongid 和上面幾個選項搭配使用,定義被限定用戶的uid和gid
掛載客戶端app
在此就不須要在安裝nfs,由於以前就有安裝nfs安裝包(須要關閉防火牆) [root@chy ~]# showmount -e 192.168.212.11 Export list for 192.168.212.11: /home/nfstestdir 192.168.212.0/24 (能夠看到共享的目錄/home/nfstestdir) [root@chy ~]# mount -t nfs 192.168.212.11:/home/nfstestdir /mnt/ (掛載時須要加-t [root@chy ~]# df -h 文件系統 容量 已用 可用 已用% 掛載點 /dev/mapper/cl-root 8.8G 5.4G 2.9G 66% / devtmpfs 737M 0 737M 0% /dev tmpfs 748M 0 748M 0% /dev/shm tmpfs 748M 8.6M 739M 2% /run tmpfs 748M 0 748M 0% /sys/fs/cgroup /dev/sda1 190M 107M 70M 61% /boot /dev/mapper/cl-var 8.4G 281M 7.7G 4% /var /dev/mapper/cl-home 497M 26M 472M 6% /home tmpfs 150M 0 150M 0% /run/user/0 192.168.212.11:/home/nfstestdir 497M 26M 472M 6% /mnt [root@chy mnt]# touch chy.111 [root@chy mnt]# ls -l 總用量 0 -rw-r--r-- 1 mysql mysql 0 8月 24 08:06 chy.111 (在客戶端建立一個chy.111) [root@chy mnt]# id mysql uid=1000(mysql) gid=1000(mysql) 組=1000(mysql)
在服務端測試ssh
[root@chy01 ~]# ls -l /home/nfstestdir/ 總用量 0 -rw-r--r-- 1 mysql mysql 0 8月 24 08:06 chy.111 [root@chy01 ~]# id mysql uid=1000(mysql) gid=1000(mysql) 組=1000(mysql)
也許有個疑問?爲何受權用戶和組是mysql呢?async
[root@chy01 ~]# cat /etc/exports /home/nfstestdir 192.168.212.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) 咱們看到,咱們設置的權限爲anonuid=1000,anongid=1000,那麼1000就是mysql