linux常用环境的安装
安装linux和安装虚拟机略过,直接进行环境的安装
1,安装JDK
java环境变量:
- 系统变量: /etc/profile
- 用户环境变量: .bash_profile(当前用户的profile)
- 将文件上传到root文件夹下
- tar -zxvf jdk-8u171-linux-x64.tar.gz(解压)
- 此次用的是解压包的方式进行配置,
- 还可以通过rpm(类似于java的.exe文件),还是需要配置,通过搜索安装文件在10中可以看到,在环境变量中配置
- yum安装,自动化配置,什么都不用做,只需要执行命令,直接用
- mv jdk1.8.0_171/ /usr(移动到usr文件夹下)
- vim .bash_profile(配置环境变量)
- source .bash_profile(刷新配置)
-
1 export JAVA_HOME=/usr/jdk1.8.0_171 2 export PATH=$JAVA_HOME/bin:$PATH
- 然后用java或者javac测试一下配置是否成功
- 查询一下java安装的位置,以方便后期进行卸载软件
-
1 [root@localhost ~]# find / -name "java" 2 /etc/pki/ca-trust/extracted/java 3 /etc/pki/java 4 /usr/jdk1.8.0_171/bin/java 5 /usr/jdk1.8.0_171/jre/bin/java
-
2,tomcat的安装:
- 将文件上传到并解压
- tar -zxvf apache-tomcat-7.0.93.tar.gz(解压)
- mv apache-tomcat-7.0.93 tomcat7 (将解压缩的文件移动到新的文件夹,更改文件名)
-
1 [root@localhost ~]# mv apache-tomcat-7.0.93 tomcat7 2 [root@localhost ~]# ll 3 总用量 8932 4 -rw-------. 1 root root 1501 11月 18 22:51 anaconda-ks.cfg 5 -rw-r--r--. 1 root root 9138957 11月 19 08:11 apache-tomcat-7.0.93.tar.gz 6 drwxr-xr-x. 9 root root 220 11月 19 08:13 tomcat7 7 [root@localhost ~]# cd tomcat7/ 8 [root@localhost tomcat7]# ll 9 总用量 132 10 drwxr-xr-x. 2 root root 4096 11月 19 08:13 bin 11 -rw-r--r--. 1 root root 18099 2月 17 2019 BUILDING.txt 12 drwxr-xr-x. 2 root root 158 2月 17 2019 conf 13 -rw-r--r--. 1 root root 6090 2月 17 2019 CONTRIBUTING.md 14 drwxr-xr-x. 2 root root 4096 11月 19 08:13 lib 15 -rw-r--r--. 1 root root 56846 2月 17 2019 LICENSE 16 drwxr-xr-x. 2 root root 6 2月 17 2019 logs 17 -rw-r--r--. 1 root root 1241 2月 17 2019 NOTICE 18 -rw-r--r--. 1 root root 3255 2月 17 2019 README.md 19 -rw-r--r--. 1 root root 9368 2月 17 2019 RELEASE-NOTES 20 -rw-r--r--. 1 root root 16978 2月 17 2019 RUNNING.txt 21 drwxr-xr-x. 2 root root 30 11月 19 08:13 temp 22 drwxr-xr-x. 7 root root 81 2月 17 2019 webapps 23 drwxr-xr-x. 2 root root 6 2月 17 2019 work
- mv tomcat7/ /usr/ (移动到usr文件下)
- /usr/tomcat7/bin: (文件下); ./startup.sh: 启动tomcat
- 查看防火墙状态关闭防火墙
-
1 [root@localhost bin]# systemctl status firewalld(查看防火墙状态) 2 ● firewalld.service - firewalld - dynamic firewall daemon 3 Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) 4 Active: active (running) since 二 2019-11-19 07:25:34 CST; 1h 3min ago 5 Docs: man:firewalld(1) 6 Main PID: 654 (firewalld) 7 CGroup: /system.slice/firewalld.service 8 └─654 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 9 10 11月 19 07:25:20 localhost.localdomain systemd[1]: Starting firewalld - dynamic fir.... 11 11月 19 07:25:34 localhost.localdomain systemd[1]: Started firewalld - dynamic fire.... 12 Hint: Some lines were ellipsized, use -l to show in full. 13 [root@localhost bin]# 14 [root@localhost bin]# 15 [root@localhost bin]# systemctl stop firewalld(暂时关闭防火墙) 16 [root@localhost bin]# systemctl disable firewalld(永久关闭防火墙) 17 Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. 18 Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
-
- ./shutdown.sh: 关闭tomcat
- ps -aux|grep "tomcat": 查看tomcat进程
-
1 [root@localhost bin]# ps -aux|grep "tomcat" 2 root 12662 9.9 8.3 2288956 85008 pts/0 Sl 08:38 0:03 /usr/jdk1.8.0_171/bin/java -Djava.util.logging.config.file=/usr/tomcat7/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /usr/tomcat7/bin/bootstrap.jar:/usr/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/usr/tomcat7 -Dcatalina.home=/usr/tomcat7 -Djava.io.tmpdir=/usr tomcat7/temp org.apache.catalina.startup.Bootstrap start 3 root 12726 0.0 0.0 112720 980 pts/0 R+ 08:39 0:00 grep --color=auto tomcat
-
- 检测tomcat日志:
-
1 [root@localhost logs]# tail -f catalina.out 2 十一月 19, 2019 8:39:00 上午 org.apache.catalina.startup.HostConfig deployDirectory 3 信息: Deploying web application directory /usr/tomcat7/webapps/manager 4 十一月 19, 2019 8:39:00 上午 org.apache.catalina.startup.HostConfig deployDirectory 5 信息: Deployment of web application directory /usr/tomcat7/webapps/manager has finished in 59 ms 6 十一月 19, 2019 8:39:00 上午 org.apache.coyote.AbstractProtocol start 7 信息: Starting ProtocolHandler ["http-bio-8080"] 8 十一月 19, 2019 8:39:00 上午 org.apache.coyote.AbstractProtocol start 9 信息: Starting ProtocolHandler ["ajp-bio-8009"] 10 十一月 19, 2019 8:39:00 上午 org.apache.catalina.startup.Catalina start 11 信息: Server startup in 1658 ms 12 pwd
-
- 修改tomcat乱码:
-
1 [root@localhost conf]# vim server.xml 2 [root@localhost conf]# pwd 3 /usr/tomcat7/conf 4 修改内容: 5 <Connector port="8081" protocol="HTTP/1.1" 6 connectionTimeout="20000" 7 redirectPort="8443" URIEncoding="UTF-8" />
-
- ./catalina.sh run: 启动并查看日志
3,mysql的安装:
- centos7.X安装mysql的时候先卸载mariadb,不然会出现冲突
-
1 [root@localhost /]# rpm -qa | grep mariadb 2 mariadb-libs-5.5.56-2.el7.x86_64 3 [root@localhost /]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64(卸载) 4 [root@localhost /]# rpm -qa | grep mariadb 5 [root@localhost /]#
-
- 上传mysql需要安装的软件包
-
1 [root@localhost /]# cd /root/mysql/ 2 [root@localhost mysql]# ls 3 mysql-community-client-5.6.42-2.el7.x86_64.rpm 4 mysql-community-common-5.6.42-2.el7.x86_64.rpm 5 mysql-community-libs-5.6.42-2.el7.x86_64.rpm 6 mysql-community-server-5.6.42-2.el7.x86_64.rpm 7 net-tools-2.0-0.22.20131004git.el7.x86_64.rpm 8 perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm 9 perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm 10 perl-Data-Dumper-2.145-3.el7.x86_64.rpm 11 perl-DBI-1.627-4.el7.x86_64.rpm 12 perl-IO-Compress-2.061-2.el7.noarch.rpm 13 perl-Net-Daemon-0.48-5.el7.noarch.rpm 14 perl-PlRPC-0.2020-14.el7.noarch.rpm
-
- 安装文件下所有的rpm包
-
1 [root@localhost mysql]# rpm -ivh perl-* 2 准备中... ################################# [100%] 3 正在升级/安装... 4 1:perl-Net-Daemon-0.48-5.el7 ################################# [ 14%] 5 2:perl-Data-Dumper-2.145-3.el7 ################################# [ 29%] 6 3:perl-Compress-Raw-Zlib-1:2.061-4.################################# [ 43%] 7 4:perl-Compress-Raw-Bzip2-2.061-3.e################################# [ 57%] 8 5:perl-IO-Compress-2.061-2.el7 ################################# [ 71%] 9 6:perl-PlRPC-0.2020-14.el7 ################################# [ 86%] 10 7:perl-DBI-1.627-4.el7 ################################# [100%] 11 [root@localhost mysql]# rpm -ivh net-tools-2.0-0.22.20131004git.el7.x86_64.rpm 12 准备中... ################################# [100%] 13 正在升级/安装... 14 1:net-tools-2.0-0.22.20131004git.el################################# [100%] 15 [root@localhost mysql]# rpm -ivh mysql-community-common-5.6.42-2.el7.x86_64.rpm 16 警告:mysql-community-common-5.6.42-2.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 17 准备中... ################################# [100%] 18 正在升级/安装... 19 1:mysql-community-common-5.6.42-2.e################################# [100%] 20 [root@localhost mysql]# rpm -ivh mysql-community-libs-5.6.42-2.el7.x86_64.rpm 21 警告:mysql-community-libs-5.6.42-2.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 22 准备中... ################################# [100%] 23 正在升级/安装... 24 1:mysql-community-libs-5.6.42-2.el7################################# [100%] 25 [root@localhost mysql]# rpm -ivh mysql-community-client-5.6.42-2.el7.x86_64.rpm 26 警告:mysql-community-client-5.6.42-2.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 27 准备中... ################################# [100%] 28 正在升级/安装... 29 1:mysql-community-client-5.6.42-2.e################################# [100%] 30 [root@localhost mysql]# rpm -ivh mysql-community-server-5.6.42-2.el7.x86_64.rpm 31 警告:mysql-community-server-5.6.42-2.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 32 准备中... ################################# [100%] 33 正在升级/安装... 34 1:mysql-community-server-5.6.42-2.e################################# [100%]
-
- 创建文件添加官方的yum源
- vi /etc/yum.repos.d/mysql-community.repo
-
1 [mysql56-community] 2 name=MySQL 5.6 Community Server 3 baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/ 4 enabled=1 5 gpgcheck=0 6 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- 安装数据库: yum -y install mysql-community-server
- 启动数据库: service mysqld start
- 修改数据库默认root用户密码: mysqladmin -u root -p password
- 回车输入原始密码,再输入新的密码
- 注意:mysql5.7的初始密码是随机生成的,放在 /var/log/mysqld.log中, 使用命令 grep ‘temporary password’ /var/log/mysqld.log 读出来即可
- windons访问数据库:
- 重登一下mysql: mysql -u root -p(然后输入密码)
- 执行一下代码,输入错误的话用\c退出(一定要注意命令后边的";",很容易犯错)
-
1 mysql> show databases; 2 +--------------------+ 3 | Database | 4 +--------------------+ 5 | information_schema | 6 | mysql | 7 | performance_schema | 8 +--------------------+ 9 3 rows in set (0.00 sec) 10 11 mysql> use mysql 12 Reading table information for completion of table and column names 13 You can turn off this feature to get a quicker startup with -A 14 15 Database changed 16 mysql> show tables; 17 +---------------------------+ 18 | Tables_in_mysql | 19 +---------------------------+ 20 | columns_priv | 21 | db | 22 | event | 23 | func | 24 | general_log | 25 | help_category | 26 | help_keyword | 27 | help_relation | 28 | help_topic | 29 | innodb_index_stats | 30 | innodb_table_stats | 31 | ndb_binlog_index | 32 | plugin | 33 | proc | 34 | procs_priv | 35 | proxies_priv | 36 | servers | 37 | slave_master_info | 38 | slave_relay_log_info | 39 | slave_worker_info | 40 | slow_log | 41 | tables_priv | 42 | time_zone | 43 | time_zone_leap_second | 44 | time_zone_name | 45 | time_zone_transition | 46 | time_zone_transition_type | 47 | user | 48 +---------------------------+ 49 28 rows in set (0.00 sec) 50 51 #执行下面两个指令就可以连接数据库了 52 mysql> update user set host='%'; 53 ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY' 54 55 56 mysql> flush privileges; 57 Query OK, 0 rows affected (0.00 sec)
-
- 上不操作把所有没有密码的用户都删除,我的操作是直接把所有的用户都删除了,没事可以正常访问,一定注意删除表中留一个字段
- 如果再出现问题就给添加一个mysql表(在命令窗口没有显示,但是在外部的可视化工具中有数据,不得其姐)
- 除了一点小问题就是mysql登录第二次就不同再输入密码了,输密码会报错难受,就这样吧
4,mysql分布式安装
- 三台主机的ip为:
- 192.168.56.121(hadoop121)[主]
- 192.168.56.122(hadoop122)[从]
- 192.168.56.123(hadoop123)[从]
- 配置三台主机的文件: vim /etc/my.cnf(文件)
-
1 # mysql的主配置文件 2 server-id=1 3 log-bin=mysql-bin 4 log-slave-updates 5 slave-skip-errors=all 6 #就上边那点配置 7 8 # mysql的主配置文件 9 server-id=2 10 log-bin=mysql-bin 11 log-slave-updates 12 slave-skip-errors=all 13 #就上边那点配置 14 15 # mysql的主配置文件 16 server-id=3 17 log-bin=mysql-bin 18 log-slave-updates 19 slave-skip-errors=all 20 #就上边那点配置
- systemctl restart mysqld(重启服务)
-
- 检验sql是否配置成功
-
1 mysql> SHOW VARIABLES like 'server_id'; 2 +---------------+-------+ 3 | Variable_name | Value | 4 +---------------+-------+ 5 | server_id | 3 | 6 +---------------+-------+ 7 1 row in set (0.00 sec)
-
- 查看主节点的日志文件以及文件的位置
-
1 mysql> show master status; 2 +------------------+----------+--------------+------------------+-------------------+ 3 | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | 4 +------------------+----------+--------------+------------------+-------------------+ 5 | mysql-bin.000001 | 120 | | | | 6 +------------------+----------+--------------+------------------+-------------------+ 7 1 row in set (0.00 sec)
-
-
将主节点的配置给从节点
-
1 mysql> change master to 2 -> 3 -> master_host='192.168.56.121', 4 -> 5 -> master_user='root', 6 -> 7 -> master_password='123456', 8 -> 9 -> master_log_file='mysql-bin.000001', 10 -> 11 -> master_log_pos=120; 12 Query OK, 0 rows affected, 2 warnings (0.18 sec)
-
- 启动从节点: start slave;(mysql)
- 查看从节点的状态,是否成功配置:
- mysql> show slave status\G
-
1 #两个必须为yes 2 Slave_IO_Running: No 3 Slave_SQL_Running: Yes
- 复制的主机有问题会报异常:
-
1 Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. 2 上一个IO错误:致命错误:从I/O线程停止,因为主服务器和从服务器具有相同的MySQL服务器Uuid;这些Uuid必须不同,复制才能工作。
-
- 解决办法,删除一个配置文件,重启mysql服务
-
1 [root@localhost ~]# find / -name "auto.cnf" 2 /var/lib/mysql/auto.cnf 3 [root@localhost ~]# rm -rf /var/lib/mysql/auto.cnf 4 [root@localhost ~]# systemctl restart mysqld
-
- 重新启动mysql的从节点并查看状态,配置成功!
-
1 mysql> start slave; 2 Query OK, 0 rows affected, 1 warning (0.00 sec) 3 4 mysql> show slave status\G 5 Slave_IO_Running: Yes 6 Slave_SQL_Running: Yes
-
- 以上是配置主从复制,下面配置读写分离架构
- 安装mycat,解压并配置环境变量:
-
1 export MYCAT_HOME=/usr/mycat 2 export PATH=$JAVA_HOME/bin:$PATH:$MYCAT_HOME/bin
- source .bash_profile(刷新配置)
-
- 检验一下是否配置成功:
[root@localhost ~]# mycat
Usage: /usr/mycat/bin/mycat { console | start | stop | restart | status | dump } - 配置mycat中国年conf下的配置 schema.xml
-
1 <?xml version="1.0"?> 2 <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> 3 <mycat:schema xmlns:mycat="http://io.mycat/"> 4 5 <!-- 定义MyCat的逻辑库 --> 6 <schema name="mycat" checkSQLschema="false" sqlMaxLimit="100" dataNode="testNode"></schema> 7 <!-- 定义MyCat的数据节点 --> 8 <dataNode name="testNode" dataHost="dtHost" database="kg" /> 9 <dataHost name="dtHost" maxCon="1000" minCon="10" balance="1" 10 writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100"> 11 <heartbeat>select user()</heartbeat> 12 <!-- can have multi write hosts --> 13 <writeHost host="hostM1" url="192.168.56.121:3306" user="root" 14 password="123456"> 15 <!-- can have multi read hosts --> 16 <readHost host="hostS1" url="192.168.56.122:3306" user="root" password="123456" /> 17 <readHost host="hostS2" url="192.168.56.123:3306" user="root" password="123456" /> 18 19 </writeHost> 20 </dataHost> 21 22 23 </mycat:schema>
-
- 配置登录mycat的权限server.xml
-
1 <!DOCTYPE mycat:server SYSTEM "server.dtd"> 2 <mycat:server xmlns:mycat="http://io.mycat/"> 3 4 <system> 5 <!-- 这里配置的都是一些系统属性,可以自己查看mycat文--> 6 <property name="defaultSqlParser">druidparser</property> 7 <property name="charset">utf8</property> 8 </system> 9 10 11 <user name="root"> 12 <property name="password">123456</property> 13 <property name="schemas">mycat</property> 14 </user> 15 16 17 18 </mycat:server>
-
- 启动mycat: mycat console
- 可能会遇到启动的时间过长,启动失败,开一修改一下启动时长的配置
- 启动成功后开始测试,在idea中执行怎删改查
-
1 # url: jdbc:mysql://localhost:3306/kg 2 url: jdbc:mysql://192.168.56.124:8066/mycat?characterEncoding=UTF-8 #测试用 3 4 把以前的访问地址换为mycat的配置,
-
5,搭建tomcat的集群
- 复制三台主机,配置ip地址
- tomcat1: 192.168.56.125
- tomcat2: 192.168.56.126
- tomcat3: 192.168.56.127
- 上传nginx解压并编译(必须要安装gcc包[yum install gcc] 来编译nginx包 )
- yum install gcc
- yum -y install pcre-devel(安装依赖环境)
- yum -y install zlib-devel (安装依赖)
- ./configure: 在nginx解压文件夹下编译
-
make & make install (安装,默认路径/usr/local/nginx)
- 配置
-
1 /usr/local/nginx/sbin 2 [root@localhost sbin]# ./nginx
- http://192.168.56.128/ (nginx默认端口号80,直接输入地址启动,正确访问就可以了)
- 停止: ./nginx -s stop
-
- 修改nginx的配置文件
-
[root@localhost conf]# vim nginx.conf
-
1 #配置tomcat的分布式的tomcat和端口号 2 upstream tomcat-servers { 3 #ip_hash; 4 server 192.168.56.125:8085; 5 server 192.168.56.126:8086; 6 server 192.168.56.127:8087; 7 } 8 9 #原有的配置访问的是index.html 10 11 # location / { 12 # root html; 13 # index index.html index.htm; 14 # } 15 #配置nginx默认去访问配置好的tomcat 16 location / { 17 proxy_pass http://tomcat-servers; 18 proxy_redirect off; 19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 20 proxy_set_header X-Real-IP $remote_addr; 21 proxy_set_header Host $http_host; 22 proxy_next_upstream http_502 http_504 error timeout invalid_header; 23 }
-
- 配置完成之后重启nginx,直接访问nginx就可以了
- nginx默认是通过轮询的方式进行访问的
- 负载均衡的策略一共有6这种
- 具体的配置后期补充(待完善)
6,搭建redis集群:
- 上传redis并解压,在解压文件夹下执行make命令进行编译
- 安装: make install PREFIX=/usr/redis
- 在/usr/redis/bin启动: 启动
- 新建窗口启动操作redis: ./redis-cli -h 192.168.56.129 -p 6379(redis的端口号是6379)
- 可以直接执行: ./redis-cli -h,如果是在原唱访问的时候执行上边的命令;
- 注意的问题是redis必须要开放远程访问,不然的换就用本地连接
-
1 [root@localhost ~]# cd /usr/redis/bin/ 2 [root@localhost bin]# ./redis-cli -h 192.168.56.129 -p 6379 3 192.168.56.129:6379> set name xiaohei 4 (error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside. 5 192.168.56.129:6379> 6 [root@localhost bin]# ./redis-cli 7 127.0.0.1:6379> set name xiaohei 8 OK 9 127.0.0.1:6379>
- 就可以直接操作操作redis库了
- 将解压包中redis的配置文件复制到安装目录下,进行端口号的配置
-
1 [root@localhost redis-4.0.10]# cp redis.conf /usr/redis/ 2 [root@localhost redis-4.0.10]# cd /usr/redis/ 3 [root@localhost redis]# ls 4 bin redis.conf 5 [root@localhost redis]# vim redis.conf 6 7 #通过/port进行查找vim中的关键字 8 #启动的时候注意,加载一下配置文件 9 [root@localhost redis]# cd bin/ 10 [root@localhost bin]# ./redis-server ../redis.conf 11 #如下可见端口号被给为了6380 12 _._ 13 _.-``__ ''-._ 14 _.-`` `. `_. ''-._ Redis 4.0.10 (00000000/0) 64 bit 15 .-`` .-```. ```\/ _.,_ ''-._ 16 ( ' , .-` | `, ) Running in standalone mode 17 |`-._`-...-` __...-.``-._|'` _.-'| Port: 6380 18 | `-._ `._ / _.-' | PID: 5460 19 `-._ `-._ `-./ _.-' _.-' 20 |`-._`-._ `-.__.-' _.-'_.-'| 21 | `-._`-._ _.-'_.-' | http://redis.io 22 `-._ `-._`-.__.-'_.-' _.-' 23 |`-._`-._ `-.__.-' _.-'_.-'| 24 | `-._`-._ _.-'_.-' | 25 `-._ `-._`-.__.-'_.-' _.-' 26 `-._ `-.__.-' _.-' 27 `-._ _.-' 28 `-.__.-'
- 更改后 也可以正常访问
-
[root@localhost bin]# ./redis-cli -p 6380
127.0.0.1:6380>
-
- 修改redis默认库的数量(默认16个): databases 5
- 修改是否以后台进程方式启动(默认no): daemonize yes
-
1 [root@localhost bin]# ./redis-server ../redis.conf 2 5474:C 20 Nov 19:47:16.718 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 3 5474:C 20 Nov 19:47:16.718 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=5474, just started 4 5474:C 20 Nov 19:47:16.718 # Configuration loaded 5 [root@localhost bin]# ps -aux|grep "redis" 6 root 5466 0.0 0.5 16316 5388 pts/0 S+ 19:38 0:00 ./redis-cli -p 6380 7 root 5475 0.0 0.7 145316 7576 ? Ssl 19:47 0:00 ./redis-server 127.0.0.1:6380 8 root 5481 0.0 0.0 112728 972 pts/1 R+ 19:47 0:00 grep --color=auto redis
- 后台启动通过杀死进程关闭: kill -9 5475(进程编号)
-
- 连接redis的可视化工具:
- 将配置文件中(默认127.0.0.1): bind 0.0.0.0(所有的人都可以访问)
- 不错的免费可视化工具: 高速下载:http://129.204.188.247/download/
- 连接进行操作就完事了,安装成功!
- java的API操作redis (jedis的jar包):
-
1 #导入依赖 2 <!--操作redis的相关依赖--> 3 <dependency> 4 <groupId>redis.clients</groupId> 5 <artifactId>jedis</artifactId> 6 <version>2.9.0</version> 7 </dependency> 8 </dependencies> 9 10 #操作jedis的API 11 public class TestJedis { 12 13 //操作字符串 14 @Test 15 public void test1(){ 16 17 /* 18 * 开启redis的远程访问全限 redis.conf 配置文件开启 19 bind 0.0.0.0 20 * */ 21 //创建redis客户端 22 Jedis jedis = new Jedis("192.168.92.19", 7000); 23 //选择操作哪一个库 使用0号库 name xiaohei 24 jedis.select(0); 25 // jedis.set("name","xiaohei"); 26 //jedis.append("name","特别黑真的特别黑"); 27 jedis.del("name"); 28 String name = jedis.get("name"); 29 System.out.println(name); 30 //关闭客户端 31 jedis.close(); 32 } 33 34 //操作list集合 35 @Test 36 public void test2(){ 37 //创建客户端 38 Jedis jedis = new Jedis("192.168.92.19", 7000); 39 jedis.select(0); 40 //存储list集合 41 // jedis.lpush("names","xiaohei","xiaobai","xiaohua","xiaojr"); 42 // jedis.lpop("names"); 43 String names = jedis.lindex("names", 0); 44 System.out.println(names); 45 //遍历 46 /* List<String> names = jedis.lrange("names", 0, -1); 47 for (String name : names) { 48 System.out.println(name); 49 }*/ 50 jedis.close(); 51 52 } 53 54 }
-
- redis在项目中进行使用:
- 主要的配置和测试
-
1 #本次演示在springboot中: 2 3 #先导入依赖: 4 <!--引入redis的依赖--> 5 <dependency> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-data-redis</artifactId> 8 </dependency> 9 10 #yml配置文件 11 spring: 12 redis: 13 database: 0 #访问redis指定的数据库 14 host: 192.168.56.129 #访问redis的ip 15 port: 6380 #访问的端口号 16 17 #测试类中的方法 18 #第一个将序列化的对象存入到数据库中,再通过反序列化将redis中的数据取出来 19 #第二个是通过操作字符串进行存取 20 @RunWith(SpringRunner.class) 21 @SpringBootTest(classes = Application.class) 22 public class TestDAO { 23 24 @Autowired 25 private RedisTemplate redisTemplate; //操作返回的是对象 26 @Autowired 27 private StringRedisTemplate redis; //操作返回的是字符串 28 //还有很多方法就不列举了,具体的查询API 29 @Test 30 public void redis(){ 31 redis.opsForValue().set("000","lili"); 32 } 33 }
上边只是搭建了简单的redis,真正到搭建redis集群的时候了,redis自身是支持分布式的:
- 因为是要代替项目中mybatis的二级缓存,所以必须对mybatis的一级缓存有所了解
- 先建立一个项目,搭建springboot+mybatis的环境,然后再mapper.xml中配置二级缓存mapper标签下加一个<cache/>
- 在加入缓存后,必须在实体了中表明序列化,不然报错:
-
1 org.apache.ibatis.cache.CacheException: Error serializing object. Cause: java.io.NotSerializableException: com.baizhi.entity.Acount 2 3 at org.apache.ibatis.cache.decorators.SerializedCache.serialize(SerializedCache.java:102) 4 at org.apache.ibatis.cache.decorators.SerializedCache.putObject(SerializedCache.java:56) 5 at org.apache.ibatis.cache.decorators.LoggingCache.putObject(LoggingCache.java:51)
- 为了能看到日志将logback.xml文件放在resources下就可以了
-
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <configuration> 3 <!--name代表设置的标识--> <!--指定日志输出的位置 控制台中输出 --> 4 <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> 5 <!--定义项目的日志输出格式--> 6 <layout class="ch.qos.logback.classic.PatternLayout"> 7 <pattern> [%p] %d{yyyy-MM-dd HH:mm:ss} %m %n</pattern> 8 </layout> 9 </appender> 10 11 <!--全局配置 项目中跟日志控制--> 12 <root level="INFO"> 13 <appender-ref ref="stdout"/> 14 </root> 15 <!-- 项目中指定包日志控制–>--> 16 <logger name="com.baizhi.dao" level="DEBUG"/> 17 <logger name="com.baizhi.service" level="DEBUG"/> 18 <logger name="org.springframework.jdbc" level="DEBUG"/> 19 </configuration>
-
-
- 修改成自己配置好的分布式缓存类
-
1 //首先实现mybatis的缓存类接口 2 import java.util.concurrent.locks.ReadWriteLock; 3 4 public class RedisCache implements Cache {...) 5 6 //将实现类接口加入到自己的mybaits的mapper文件中 7 <cache type="com.baizhi.cache.RedisCache"/>
-
- 配置主要的环境:
- 全部在连接中: