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