我们比较看一下这两个流行的数据库选项,即再次使NoSQL和SQL数据库相互竞争。
1、Redis简介
2、先决条件
·在你的计算机上安装/配置了MySQL,如果没有,请转到此处:如何在Ubuntu 16.04上安装MySQL
3、在Ubuntu上安装Redis
sudo apt update
sudo apt install redis-server
sudo vi /etc/redis/redis.conf复制代码
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd
# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis/redis-server.pid
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
4、启用密码验证:
# Require clients to issue AUTH <PASSWORD> before processing any other # commands. This might be useful in environments in which you do not trust # others with access to the host running redis-server. # # This should stay commented out for backward compatibility and because most # people do not need auth (e.g. they run their own servers). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. requirepass yourpasswordhere # Command renaming. # # It is possible to change the name of dangerous commands in a shared # environment. For instance the CONFIG command may be renamed into something # hard to guess so that it will still be available for internal-use tools # but not available for general clients. # # Example: # # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52复制代码
sudo systemctl restart redis.service复制代码
5、在Ubuntu上安装PHPRedis:
·运行以下命令以安装PHPRedis扩展名:
sudo apt-get install php-redis复制代码
·将以下行添加到你的php.ini文件中:
extension=redis.so复制代码
6、工作流程
·仅使用MySQL:
- 在[1,10000]之间随机生成一个密钥,并在MySQL数据库中搜索该密钥
- o请记下这样做所花费的时间
- 应使用时间样本来处理n个此类请求,同时将n逐渐增加为1、10、100、1000、10000、100000、1000000、1000000
·使用MySQL和Redis:
- 随机生成一个介于[1,10000]之间的密钥
- 将检查该密钥是否已经存在/存储在我们的Redis中
- §如果Redis上有它,我们不会打MySQL
- §如果Redis中不存在该密钥,我们将在MySQL数据库中搜索该密钥,并将该密钥存储到Redis
o请记下这样做所花费的时间应使用时间样本来处理
n个此类请求,同时将n逐渐增加为1、10、100、1000、10000、100000、1000000、1000000
7、源代码
仅MySQL源代码
<?php $con = mysqli_connect("localhost","root","admin","blog_db"); for($i = 1; $i <= 10000000; $i = $i *10) { $startTime = microtime(true); for($j = 1; $j <= $i; $j++) { $rand = rand(1, 100000); $sql = "SELECT VALUE from data WHERE `key` = $rand"; if (!mysqli_query($con, $sql)) { echo "Error: " . $sql . "" . mysqli_error($con); } } $endTime = microtime(true); file_put_contents('/home/ayush/Desktop/temp/blog/mysqlonly.log', $i . ',' . ($endTime - $startTime) . "\n" , FILE_APPEND); }复制代码
8、MySQL和Redis源代码
<?php $con = mysqli_connect("localhost","root","admin","blog_db"); $client = new Redis(); $client->connect('localhost'); for($i = 1; $i <= 10000000; $i = $i *10) { $startTime = microtime(true); for($j = 1; $j <= $i; $j++) { $rand = rand(1, 100000); if(!$client->exists($rand)) { $client->set($rand, $rand); $sql = "SELECT VALUE from data WHERE `key` = $rand"; if (!mysqli_query($con, $sql)) { echo "Error: " . $sql . "" . mysqli_error($con); } } } $endTime = microtime(true); file_put_contents('/home/ayush/Desktop/temp/blog/redis.log', $i . ',' . ($endTime - $startTime) . "\n" , FILE_APPEND); $client->flushAll(); }复制代码
基准测试
表格数据
Redis的性能开始显着提高。 因此,如果处理的请求数量很大,则将Redis这样的缓存引擎与数据库一起使用是一个好主意。
抽丝剥茧。细说架构那些事 优锐课