NFS是Network File System的縮寫;這個文件系統是基於網路層面,經過網絡層面實現數據同步mysql
NFS最先由Sun公司開發,分2,3,4三個版本,2和3由Sun起草開發,4.0開始Netapp公司參與並主導開發,目前最新爲4.1版本——>4.1版本是2010年出來還沒更新過linux
NFS數據傳輸基於RPC協議,RPC爲Remote Procedure Call的簡寫,意思爲 遠程過程調用sql
NFS應用場景是:A,B,C三臺機器上須要保證被訪問到的文件是同樣的,A共享數據出來,B和C分別去掛載A共享的數據目錄,從而B和C訪問到的數據和A上的一致vim
NFS架構centos
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
[root@hanfeng ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.202.130 netmask 255.255.255.0 broadcast 192.168.202.255 inet6 fe80::20c:29ff:feff:458f prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ff:45:8f txqueuelen 1000 (Ethernet) RX packets 684 bytes 68341 (66.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 226 bytes 26200 (25.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.202.150 netmask 255.255.255.0 broadcast 192.168.202.255 ether 00:0c:29:ff:45:8f txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 8 bytes 536 (536.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 536 (536.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@hanfeng ~]#
[root@hf-01 ~]# ifconfig ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.202.131 netmask 255.255.255.0 broadcast 192.168.202.255 inet6 fe80::baa:c015:db72:1d5c prefixlen 64 scopeid 0x20<link> ether 00:0c:29:b6:4d:41 txqueuelen 1000 (Ethernet) RX packets 63 bytes 7905 (7.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 79 bytes 11615 (11.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 68 bytes 5524 (5.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 68 bytes 5524 (5.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@hf-01 ~]#
[root@hanfeng ~]# yum install -y nfs-utils rpcbind
[root@hf-01 ~]# yum install -y nfs-utils
[root@hanfeng ~]# vim /etc/exports /home/nfstestdir 要分享出去的目錄是哪個目錄,這個目錄是不存在的,後期還須要建立的 192.168.202.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) 指定你要給哪一個機器去分享這個目錄,首先定義IP或IP段 保存退出
[root@hanfeng ~]# mkdir /home/nfstestdir [root@hanfeng ~]#
[root@hanfeng ~]# chmod 777 /home/nfstestdir [root@hanfeng ~]#
[root@hanfeng ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2172/master 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 1115/sshd tcp6 0 0 ::1:25 :::* LISTEN 2172/master tcp6 0 0 :::3306 :::* LISTEN 2264/mysqld tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 1115/sshd [root@hanfeng ~]# ps aux |grep rpc rpc 2393 0.0 0.1 64964 1044 ? Ss 15:40 0:00 /sbin/rpcbind -w root 2537 0.0 0.0 112680 972 pts/0 R+ 16:17 0:00 grep --color=auto rpc
[root@hf-01 ~]# 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 2393/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1334/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2146/master tcp6 0 0 :::3306 :::* LISTEN 2029/mysqld tcp6 0 0 :::111 :::* LISTEN 2393/rpcbind tcp6 0 0 :::22 :::* LISTEN 1334/sshd tcp6 0 0 ::1:25 :::* LISTEN 2146/master [root@hf-01 ~]# ps aux |grep rpc root 16644 0.0 0.0 112656 992 pts/2 R+ 04:46 0:00 grep --color=auto rpc [root@hf-01 ~]#
[root@hanfeng ~]# systemctl start nfs [root@hanfeng ~]# ps aux |grep nfs root 4560 0.0 0.0 0 0 ? S< 04:52 0:00 [nfsd4] root 4561 0.0 0.0 0 0 ? S< 04:52 0:00 [nfsd4_callbacks] root 4565 0.0 0.0 0 0 ? S 04:52 0:00 [nfsd] root 4566 0.0 0.0 0 0 ? S 04:52 0:00 [nfsd] root 4567 0.0 0.0 0 0 ? S 04:52 0:00 [nfsd] root 4568 0.0 0.0 0 0 ? S 04:52 0:00 [nfsd] root 4569 0.0 0.0 0 0 ? S 04:52 0:00 [nfsd] root 4570 0.0 0.0 0 0 ? S 04:52 0:00 [nfsd] root 4571 0.0 0.0 0 0 ? S 04:52 0:00 [nfsd] root 4572 0.0 0.0 0 0 ? S 04:52 0:00 [nfsd] root 4576 0.0 0.0 112676 984 pts/0 R+ 04:52 0:00 grep --color=auto nfs [root@hanfeng ~]#
[root@hanfeng ~]# ps aux |grep rpc rpc 2957 0.0 0.1 64908 1416 ? Ss 03:05 0:00 /sbin/rpcbind -w rpcuser 4530 0.0 0.1 42380 1744 ? Ss 04:52 0:00 /usr/sbin/rpc.statd root 4531 0.0 0.0 0 0 ? S< 04:52 0:00 [rpciod] root 4539 0.0 0.0 19324 652 ? Ss 04:52 0:00 /usr/sbin/rpc.idmapd root 4550 0.0 0.0 42556 948 ? Ss 04:52 0:00 /usr/sbin/rpc.mountd root 4602 0.0 0.0 112676 984 pts/0 R+ 05:00 0:00 grep --color=auto rpc [root@hanfeng ~]#
[root@hf-01 ~]# ps aux |grep rpc root 16644 0.0 0.0 112656 992 pts/2 R+ 04:46 0:00 grep --color=auto rpc [root@hf-01 ~]#
[root@hanfeng ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service. [root@hanfeng ~]#
yum install -y nfs-utils
[root@hf-02 ~]# showmount -e 192.168.74.129 clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host) [root@hf-02 ~]#
A機器關閉防火牆 [root@hf-01 ~]# systemctl stop firewalld [root@hf-01 ~]# getenforce Disabled [root@hf-01 ~]# B機器關閉防火牆 [root@hf-02 ~]# systemctl stop firewalld [root@hf-02 ~]# [root@hf-02 ~]# getenforce Enforcing [root@hf-02 ~]# setenforce 0 [root@hf-02 ~]# getenforce Permissive [root@hf-02 ~]#
[root@hf-02 ~]# showmount -e 192.168.202.130 Export list for 192.168.74.129: /home/nfstestdir 192.168.74.0/24 [root@hf-02 ~]#
[root@hf-02 ~]# mount -t nfs 192.168.74.129:/home/nfstestdir /mnt/ [root@hf-02 ~]# df -h 文件系統 容量 已用 可用 已用% 掛載點 /dev/sda3 18G 873M 17G 5% / devtmpfs 489M 0 489M 0% /dev tmpfs 494M 0 494M 0% /dev/shm tmpfs 494M 13M 481M 3% /run tmpfs 494M 0 494M 0% /sys/fs/cgroup /dev/sda1 197M 75M 122M 39% /boot 192.168.74.129:/home/nfstestdir 18G 7.4G 11G 42% /mnt [root@hf-02 ~]#
[root@hf-02 ~]# cd /mnt/ [root@hf-02 mnt]# ls [root@hf-02 mnt]# touch aminglinux.111 [root@hf-02 mnt]# ls -l 總用量 0 -rw-r--r--. 1 1000 1000 0 8月 24 00:34 aminglinux.111 [root@hf-02 mnt]#
[root@hf-01 ~]# ls -l /home/nfstestdir/ 總用量 0 -rw-r--r-- 1 mysql mysql 0 8月 24 00:34 aminglinux.111 [root@hf-01 ~]#