In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Let's learn about the detailed steps of building redis+mysql architecture. I believe you will benefit a lot after reading it. There are not many words in the text. I hope this short article is what you want to build redis+mysql architecture.
Redis+mysql framework building
Redis is a key-value storage system. Similar to memcached, but redis supports more value types, mainly: string (string), list (linked list), set (collection), zset (ordered collection), and hash (hash type). Redis, like memcached, caches data in memory to ensure efficiency. The difference is that redis will periodically write updated data to disk or modified operations to additional record files, and on this basis to achieve master-slave master-slave synchronization.
The main differences between redis and memcached are as follows: 1. Redis not only supports simple key type data, but also provides storage of string, set, zset, hash and other data structures. 2. Redis supports data backup, that is, data backup in master-slave mode. 3. Redis supports data persistence. You can keep the data in memory on disk and load it again when you restart it.
Next, we introduce the construction of redis+mysql architecture: environment: red Hat 6.5. virtual host VM1 ip=172.25.10.8.
One: install redis on the virtual host:
Yum install-y gcc gcc-c++; install the compiler software cd redis/tar-zxf redis-3.0.2.tar.gz; extract the package cd redis-3.0.2; enter the extracted directory make-- > # make install; compile and install cd utils/ directly; enter this util directory and execute the following script. Configure and start the program. / install_server.sh
Netstat-antlpe | grep redis; you can check the port number used by redis as 6379redis-cli; redis client command
Ste no1 1234; add the key no1 to the key value 1234; get no1; get the key value of no1
2. Introduce redis as a cache cloud server for mysql. 1. Install the lnmp environment and install the following software packages:
Yum install-y mysql-server; install mysql database yum install-y nginx-1.8.0-1.el6.ngx.x86_64.rpm php-*; install nginx and phpvim / etc/php.ini; edit php configuration file, modify time zone
Vim / etc/php-fpm.d/www.conf; modify php-fpm users and groups to nginx. Default is apache.
/ etc/init.d/php-fpm start; start the php-fpm service netstat-antlpe; check the php-fpm port number 9000; check the php-fpm port number 9000; start the nginx service; cd / usr/share/nginx/html/; enter the default release directory and write the php test page vim index.php
Vim / etc/nginx/conf.d/default.conf; write the default configuration file for nginx; open the php module
/ etc/init.d/nginx restart; restart the nginx service
Create a php test page
Vim index.php
/ etc/init.d/mysqld start; start the database
Install the redis extension for php; material: phpredis-master.zip
Unzip phpredis-master.zip; decompress the package cd phpredis-masterphpize; configure, compile and install. / configuremake-> # make installcd / usr/lib64/php/modules/; enter the module directory of php; cp / etc/php.d/mysql.ini / etc/php.d/redis.ini; copy mysql.ini to redis.ini;vim / etc/php.d/redis.in; modify the module content, in fact, is to add redis.so module
Extension=redis.so
/ etc/init.d/php-fpm reload; php-fpm re-import function modules
Add test.sql for testing to the database.
Mysql
< test.sqlmysql select * from test; update test set name="westos" where id=1; 在FIREFOX中输入master的ip进入页面,查看测试结果变化;进行验证数据从哪里读到,第一应该是从数据库中读到,刷新一次数据缓存在redis中,所以数据应该是从redis中读到;但是如果在数据库中更新了相关的数据,而redis中缓存的数据却无法及时更新,因为redis中任然有对应的KEY,数据就不会更新,这是一个缺陷,接下来就要通过mysql触发器将改变的数据同步到redis中。 三、数据库与redis数据同步: 配置gearman实现数据同步:Gearman 是一个支持分布式的任务分发框架, Gearman Job Server:Gearman 核心程序,需要编译安装并以守护进程形式运行在后台。 Gearman Client:可以理解为任务的请求者。 Gearman Worker:任务的真正执行者,一般需要自己编写具体逻辑并通过守护进程方式 运行,Gearman Worker 接收到Gearman Client 传递的任务内容后,会按顺序处理。 大致流程:下面要编写的mysql 触发器,就相当于Gearman 的客户端。修改表,插入表就相当于直接 下发任务。然后通过libmysqludfjson UDF库函数将关系数据映射为JSON 格式,然后 在通过gearman-mysql-udf插件将任务加入到Gearman的任务队列中,最后通过 redis_worker.php,也就是Gearman 的worker 端来完成redis 数据库的更新。 yum install -y libgearman-devel-1.1.8-2.el6.x8664.rpm libgearman-1.1.8-2.el6.x8664.rpm libevent-* ;安装gearman软件包及依赖包;yum install -y gearmand-1.1.8-2.el6.x86_64.rpm/etc/init.d/gearmand start ;启动geaman服务;tar -zxf gearman-1.1.2.tgz ;安装php的gearman扩展cd gearman-1.1.2phpize./configuremake -----># make install; source code compilation and installation trilogy; cd / usr/lib64/php/modules/-- > gearman.so; enter the php function module file cd / etc/php.d/cp redis.ini gearman.ini here; add the gearman function module, vim gearman.ini, as you would add the redis function module on
Extension=gearman.so
Php-m | grep gearman; check whether the gearman function module is added
Install the libmysqludfjson: libmysqludfjson UDF library function to map relational data to JSON format. Mapping data to JSON format is usually transformed by a program.
Yum install-y mysql-devel; install mysql-devle software dependency cd / root/redis/unzip libmysqludfjson-master.zipcd libmysqludfjson-mastergcc $(mysqlconfig-- cflags)-shared-fPIC-o libmysqludfjson.so libmysqludfjson.c; output libmysqludfjson.c compilation to libmysqludf_json.somysql; view mysql module directory
Show global variables like 'plugin_dir'
Cp libmysqludfjson.so / usr/lib64/mysql/; copy libmysqludfjson.so module mysql; register UDF function > CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so'
> select * from mysql.func;; view registered functions
Install gearman-mysql-udf, a plug-in that manages distributed queues that call Gearman.
Tar-zxf gearman-mysql-udf-0.6.tar.gzcd gearman-mysql-udf-0.6./configure-- libdir=/usr/lib64/mysql/plugin; compile and install make-> # make install > create function gman_do_background returns string soname 'libgearman_mysql_udf.so';; register the UDF function
> create function gman_servers_set returns string soname 'libgearman_mysql_udf.so'
> select * from mysql.func;; View the function
Cd ~ / redis/vim test.sqlmysql > show triggers from test
> select gman_servers_set ('127.0.0.1 4730');; specify the service information of gearman
Write mysql triggers (based on the actual situation)
Vim test.sql
Use test; DELIMITER $$CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN SET @ RECV=gmandobackground ('syncToRedis', json_object (NEW.id as id, NEW.name as name)); END$$ DELIMITER
Mysql
< test.sql mysql >SHOW TRIGGERS FROM test;; view triggers
Write the worker side of gearman:
Vim worker.php
Nohup php worker.php &; runs worker in the background
Update the data in mysql, mysql updates the contents of the database, and redis updates the cached data immediately. It can be verified directly.
After reading this article on the detailed steps of building a redis+mysql framework, many readers will want to know more about it. For more industry information, you can follow our industry information section.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.