多个客户端访问服务器时如何优化。有客户端,服务端,数据库。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多个客户端访问服务器时如何优化。有客户端,服务端,数据库。相关的知识,希望对你有一定的参考价值。

客户端每次访问服务端时要从数据库读取资源,同时要记录访问的时间。。。读取数据库后也要记录读取的时间。。。。如何提高效率。。。。可能会用到多线程。。

参考技术A 你讲的问题,有两方面的优化

1.客户端到服务端的优化
2.服务端读取数据库的优化

多客户端到服务端,一般需要建立连接池,比如控制只容许50个客户端连接,那么池的最大连接数为50,超过50就需要返回超出最大连接。具体最大连接数设置为多少,由服务器的CPU决定。

服务端读取数据库的优化,可以优化表的查询速度,最常见的是加索引,优化结构。
对于数据库太过频繁的读取,也会出现数据库服务器应付不来。可以加缓存层。把常访问的公共数据放在缓存,有跟新,同时更新缓存。读取,只从缓存中读取就可以了。本回答被提问者和网友采纳
参考技术B 这要专业的人帮你优化了 这包括系统 数据库 客户端 服务器端

NFS部署和优化

NFS 即 network file system,网络文件系统。允许通过网络共享文件系统将服务端文件系统共享给远程客户端,在客户端上访问共享目录与访问客户端本地文件系统一样。

服务端IP:192.168.147.137,客户端IP:192.168.147.138

服务端:

在服务端上安装NFS服务需要安装两个包:nfs-utils 和 rpcbind

login as: root

[email protected]‘s password:

Last login: Sat Feb 25 21:16:48 2017 from 192.168.147.1

[[email protected] ~]# yum install -y nfs-utils rpcbind

创建共享目录并将其权限改为777:

[[email protected] ~]# mkdir -p /home/network/nfs

[[email protected] ~]# chmod 777 /home/network/nfs

打开配置文件/etc/exports:

[[email protected] ~]# vim /etc/exports

默认是空的,我们写入一行内容:

#共享的目录 共享给IP或网段(操作,同步,权限)

/home/network/nfs 192.168.147.138(rw,sync)

保存退出,

启动rpcbind服务

[[email protected] ~]# /etc/init.d/rpcbind start

正在启动 rpcbind:                                         [确定]

然后启动NFS服务

[[email protected] ~]# /etc/init.d/nfs start

启动 NFS 服务:                                            [确定]

启动 NFS mountd:                                          [确定]

启动 NFS 守护进程:                                        [确定]

正在启动 RPC idmapd:                                      [确定]

客户端:

在客户端主机上安装包nfs-utils

login as: root

[email protected]‘s password:

Last login: Wed Feb 22 22:07:44 2017

[[email protected] ~]# yum install -y nfs-utils

启动rpcbind和nfs服务

[[email protected] ~]# /etc/init.d/rpcbind start

正在启动 rpcbind:                                         [确定]

[[email protected] ~]# /etc/init.d/nfs start

启动 NFS 服务:                                            [确定]

启动 NFS mountd:                                          [确定]

启动 NFS 守护进程:                                        [确定]

正在启动 RPC idmapd:                                      [确定]

然后使用命令showmount查看服务端共享信息(共享目录,共享给谁)

[[email protected] ~]# showmount -e 192.168.147.137

Export list for 192.168.147.137:

/home/network/nfs 192.168.147.138

接着就可以将共享的目录挂载到客户端指定的挂载点上了

[[email protected] ~]# mount -t nfs 192.168.147.137:/home/network/nfs /mnt

查看挂载后的磁盘信息

[[email protected] ~]# df -h

Filesystem                         Size  Used Avail Use% Mounted on

/dev/mapper/vg_cp1-lv_root          18G  1.4G   15G   9% /

tmpfs                              947M     0  947M   0% /dev/shm

/dev/sda1                          485M   31M  429M   7% /boot

192.168.147.137:/home/network/nfs   18G  2.5G   14G  15% /mnt

测试nfs:

1.操作:ro 只读;rw 读写;

服务端:

[[email protected] ~]# vim /etc/exports

/home/network/nfs 192.168.147.138(ro)

[[email protected] ~]# exportfs -arv

exporting 192.168.147.138:/home/network/nfs

客户端:

[[email protected] mnt]$ ls

text.txt

[[email protected] mnt]$ cat text.txt

123456

[[email protected] mnt]$ touch 1.txt

touch: 无法创建"1.txt": 只读文件系统

服务端:

[[email protected] nfs]# vim /etc/exports

/home/network/nfs 192.168.147.138(rw)

[[email protected] nfs]# exportfs -arv

exporting 192.168.147.138:/home/network/nfs

客户端:

[[email protected] mnt]$ ls

text.txt

[[email protected] mnt]$ cat text.txt

123456

[[email protected] mnt]$ touch 1.txt

[[email protected] mnt]$ ls

1.txt  text.txt

2.是否同步:

sync,同步,内存数据实时写入磁盘

async,不同步,内存数据定期写入磁盘

3.限制用户:默认情况下,客户端对nfs文件系统操作是限制以nfsnobody用户身份进行的。

root_squash:只限制root用户为anonuid和anongid

no_root_squash:不限制root用户,

all_squash:限制所有用户为anonuid和anongid

no_all_squash:所有用户都不限制

如果限制,需要指明限制为哪个用户和组:anonuid和anongid,否则将限制为默认的nfsnobody。

(1)默认情况下,将客户端root用户创建的文件限制为nfsnobody,一般用户创建的文件限制为nobody:

[[email protected] nfs]# vim /etc/exports

/home/network/nfs 192.168.147.138(rw,sync)

[[email protected] nfs]# exportfs -arv

exporting 192.168.147.138:/home/network/nfs

[[email protected] mnt]# touch root.txt

[[email protected] mnt]# su rachy

[[email protected] mnt]$ touch rachy.txt

[[email protected] mnt]$ ls -l

总用量 0

-rw-rw-r-- 1 nobody    nobody    0 2月  25 23:06 rachy.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 2月  25 23:06 root.txt

(2)all_squash

[[email protected] nfs]# vim /etc/exports

/home/network/nfs 192.168.147.138(rw,sync,all_squash)

[[email protected] nfs]# exportfs -arv

exporting 192.168.147.138:/home/network/nfs

[[email protected] mnt]# touch root.txt

[[email protected] mnt]# su rachy

[[email protected] mnt]$ touch rachy.txt

[[email protected] mnt]$ ls -l

总用量 0

-rw-rw-r-- 1 nfsnobody nfsnobody 0 2月  25 23:11 rachy.txt

-rw-r--r-- 1 nfsnobody nfsnobody 0 2月  25 23:11 root.txt

(3)all_squash,anonuid=502,anongid=502,将所有用户都限制为服务端上用户rachy

[[email protected] nfs]# vim /etc/exports

/home/network/nfs 192.168.147.138(rw,sync,all_squash,anonuid=502,anongid=502)

[[email protected] nfs]# exportfs -arv

exporting 192.168.147.138:/home/network/nfs

[[email protected] nfs]# tail -n 1 /etc/passwd

rachy:x:502:502::/home/rachy:/bin/bash

[[email protected] mnt]# touch root

[[email protected] mnt]# su rachy

[[email protected] mnt]$ touch rachy

[[email protected] mnt]$ ls -l

总用量 0

-rw-rw-r-- 1 rachy rachy 0 2月  25 23:20 rachy

-rw-r--r-- 1 rachy rachy 0 2月  25 23:20 root

(4)no_root_squash不限制root用户,其他用户自动被限制为nobody:

[[email protected] nfs]# vim /etc/exports

/home/network/nfs 192.168.147.138(rw,sync,no_root_squash)

[[email protected] nfs]# exportfs -arv

exporting 192.168.147.138:/home/network/nfs

[[email protected] mnt]# touch root

[[email protected] mnt]# su rachy

[[email protected] mnt]$ touch rachy

[[email protected] mnt]$ ls -l

总用量 0

-rw-rw-r-- 1 nobody nobody 0 2月  25 23:34 rachy

-rw-r--r-- 1 root   root   0 2月  25 23:34 root

(5)root_squash,anonuid=502,anongid=502,将root用户限制为服务端上用户rachy,其他用户自动被限制为nobody:

[[email protected] nfs]# vim /etc/exports

/home/network/nfs 192.168.147.138(rw,sync,root_squash,anonuid=502,anongid=502)

[[email protected] nfs]# exportfs -arv

exporting 192.168.147.138:/home/network/nfs

[[email protected] mnt]# touch root

[[email protected] mnt]# su rachy

[[email protected] mnt]$ touch rachy

[[email protected] mnt]$ ls -l

总用量 0

-rw-rw-r-- 1 nobody nobody 0 2月  25 23:38 rachy

-rw-r--r-- 1 rachy  rachy  0 2月  25 23:38 root

(6)no_all_squash,都不限制的话,root被限制为nfsnobody,其他用户为nobody,与什么都不做的情况(1)一样:

[[email protected] nfs]# vim /etc/exports

/home/network/nfs 192.168.147.138(rw,sync,no_all_squash)

[[email protected] nfs]# exportfs -arv

exporting 192.168.147.138:/home/network/nfs

[[email protected] mnt]# touch root

[[email protected] mnt]# su rachy

[[email protected] mnt]$ touch rachy

[[email protected] mnt]$ ls -l

总用量 0

-rw-rw-r-- 1 nobody    nobody    0 2月  25 23:42 rachy

-rw-r--r-- 1 nfsnobody nfsnobody 0 2月  25 23:42 root


以上是关于多个客户端访问服务器时如何优化。有客户端,服务端,数据库。的主要内容,如果未能解决你的问题,请参考以下文章

NFS部署和优化

nginx优化--gzip压缩与expire浏览器缓存

如何更改服务器以便多个客户端可以访问服务器中的共享数据?

MySQL性能优化

网络编程TCP/IP协议----- 多进程多线程服务器

Web性能优化-ReponseCaching