一、mysql基本操作
1,連接數據庫 mysql -u root -p -h 127.0.0.1 mysql -u root -p -h 192.168.12.56 2,授予遠程連接的權限 grant all privileges on *.* to root@"%" identified by "nihao123" 3,修改root用戶的密碼 先進入mysql的交互式模式 set password = PASSWORD('redhat123'); 4,創建mysql用戶 create user zijin@"%" identified by "zijin" 5,給予zijin用戶查詢所有庫和所有表的權限 grant select on *.* to zijin@"%" identified by "zijin" 6,查詢mysql庫中的用戶信息 use mysql; select host,user,password from user; 7,給予zijin用戶創建所有庫和表的權限,再給修改權限,再給刪除權限 grant create on *.* to zijin@"%" identified by "zijin" grant update on *.* to zijin@"%" identified by "zijin" grant delete on *.* to zijin@"%" identified by "zijin" 8,授予mysql權限的語法 mysql使用grant命令對賬戶進行授權,grant命令常見格式如下 grant 權限 on 數據庫.表名 to 賬戶@主機名 對特定數據庫中的特定表授權 grant 權限 on 數據庫.* to 賬戶@主機名 對特定數據庫中的所有表給與授權 grant 權限1,權限2,權限3 on *.* to 賬戶@主機名 對所有庫中的所有表給與多個授權 grant all privileges on *.* to 賬戶@主機名 對所有庫和所有表授權所有權限 9,移出zijin的創建權限 revoke create on *.* from zijin@"%" identified by 'zijin'; revoke delete on *.* from zijin@"%" identified by 'zijin; 10,數據庫備份與恢復 mysqldump -u root -p --all-databases > /tmp/db.sql #這不是在數據庫環境下 可以備份單個數據庫 mysqldump -u root -p luffycity > /tmp/luffycity.sql 11,導入數據 第一種: 進入mysql交互模式 source /tmp/luffycity.sql; 第二種: mysql -u root -p < /tmp/luffycity.sql 第三種: navicat
二,mariadb主從復制部署
1, 准備兩台機器
192.168.12.56 #主服務器master(可讀可寫) mariadb數據庫用戶名:root 密碼:root1 192.168.12.81 #從服務器slave(可讀) mariadb數據庫用戶名:root 密碼:root2
2, 配置主數據庫
2.1 修改主數據庫配置文件
1,進入配置文件 vim /etc/my.cnf 2,添加配置信息 [mysqld] # 如果配置文件里面有這一行,就只需要加以下兩行配置就可以了 server-id=1 log-bin=qishi2-logbin
2.2 進入數據庫
systemctl start mariadb
2.3 創建主從復制用戶
1,進入數據庫
mysql -uroot -p
2,創建用戶
create user zijin@'%' identified by 'zijin';
2.4 給從庫賬號授權
grant replication slave on *.* to 'zijin'@'%';
2.5 把主庫數據導入從庫
1,實現對主數據庫鎖表只讀,防止數據寫入,數據復制失敗 flush table with read lock; 2,查看並記錄主數據庫的狀態 show master status; 3,導出主數據庫的數據為alldb.sql mysqldump -u root -p --all-databases > /opt/alldb.sql #這個是在數據庫環境外 4,scp /opt/alldb.sql 192.168.12.81:/opt/
3,配置從數據庫
3.1 修改從數據庫的配置文件
1,進入配置文件 vim /etc/my.cnf 2,添加信息 [mysqld] server-id=5 read-only=true
3.2 啟動數據庫
systemctl start mariadb
3.3 導入主庫的數據
1,連接數據庫 mysql -u root -p 2,導入數據 source /opt/alldb.sql
3.4 配置復制的參數,slave從庫連接master主庫的配置(最重要的)
在數據庫的環境下輸入: change master to master_host='192.168.12.64', master_user='zijin', master_password='zijin', master_log_file='qishi2-logbin.000004', master_log_pos=467;
3.5 啟動從庫的同步開關
start slave #開啟
stop slave #停止
3.6 查看從庫的狀態
show slave status;
還可以輸show slave status /G;
3,主庫再設置
3.1 從庫數據導入完畢和開啟主從同步后,解鎖主庫
unlock tables; #在主庫下
3.2驗證主從復制情況
在主庫上創建數據,查看從庫數據同步狀態
3.3 在主庫上給zijin用戶select權限,並刷新權限表
grant select on *.* to zijin@"%" identified by "zijin"; flush privileges;
3.4 在從庫上登錄zijin用戶,並試圖創建一個庫
create database aaaa; ERROR 1290 (HY000): The MariaDB server is running with the --read-only option so
it cannot execute this statement #說明從庫沒有新建的權限
三、redis發布訂閱和持久化
1,redis發布訂閱
1.1基本命令
PUBLISH channel msg 將信息 message 發送到指定的頻道 channel SUBSCRIBE channel [channel ...] 訂閱頻道,可以同時訂閱多個頻道 UNSUBSCRIBE [channel ...] 取消訂閱指定的頻道, 如果不指定頻道,則會取消訂閱所有頻道 PSUBSCRIBE pattern [pattern ...] 訂閱一個或多個符合給定模式的頻道,每個模式以 * 作為匹配符,比如 it* 匹配所 有以 it 開頭的頻道
( it.news 、 it.blog 、 it.tweets 等等), news.* 匹配所有 以 news. 開頭的頻道
( news.it 、 news.global.today 等等),諸如此類 PUNSUBSCRIBE [pattern [pattern ...]] 退訂指定的規則, 如果沒有參數則會退訂所有規則 PUBSUB subcommand [argument [argument ...]] 查看訂閱與發布系統狀態 注意:使用發布訂閱模式實現的消息隊列,當有客戶端訂閱channel后只能收到后續發布到該頻道的消息,之前發送的不會緩存,
必須Provider和Consumer同時在線。
1.2發布訂閱案例
發布者 [root@web02 ~]# redis-cli 127.0.0.1:6379> PUBLISH diantai 'jinyewugenglaiwojia' (integer) 2 訂閱者1 [root@web02 ~]# redis-cli 127.0.0.1:6380> SUBSCRIBE diantai Reading messages... (press Ctrl-C to quit) 1) 'jinyewugenglaiwojia' 2) (integer) 1 訂閱者2 [root@web02 ~]# redis-cli 127.0.0.1:6381> SUBSCRIBE diantai Reading messages... (press Ctrl-C to quit) 1) 'jinyewugenglaiwojia' 2) (integer) 1
1.3 訂閱模糊匹配的頻道案例
發布者 [root@web02 ~]# redis-cli 127.0.0.1:6379> PUBLISH wangbaoqiang "jintian zhennanshou " (integer) 2 訂閱者1 127.0.0.1:6380> PSUBSCRIBE wang* Reading messages... (press Ctrl-C to quit) 1) "jintian zhennanshou " 2) (integer) 1 訂閱者2 127.0.0.1:6381> PSUBSCRIBE wa* Reading messages... (press Ctrl-C to quit) 1) "jintian zhennanshou " 2) (integer) 1
2,redis持久化之RDB
2.1 創建redis配置文件
vim /opt/redis_conf/reids-6379.conf
2.2 寫入信息
port 6379 daemonize yes dir /data/6379 pidfile /data/6379/redis.pid loglevel notice logfile "/data/6379/redis.log" protected-mode yes dbfilename dbmp.rdb save 900 1 save 300 10 save 60 10000
2.3 進入redis數據庫,寫入數據,並使用save命令開啟rdb持久化
redis-cli set name liujie set age 18 set sex nam save # 開啟rdb持久化, 也可以不用
3,redis之AOF持久化
3.1 修改redis配置文件
1,進入配置文件 vim /opt/redis_conf/redis-6379.con 2,寫入配置信息 appendonly yes appendfsync everysec
3.2 重啟redis服務
pkill reids redis-server /opt/redis_conf/redis-6379.conf
3.3 不用重啟redis,直接從RDB切換到AOF(因為生產環境中是不允許停止redis)
1,修改redis配置文件 daemonize yes port 6379 logfile /data/6379/redis.log dir /data/6379 dbfilename dbmp.rdb save 900 1 #rdb機制 每900秒 有1個修改記錄 save 300 10 #每300秒 10個修改記錄 save 60 10000 #每60秒內 10000修改記錄 2,啟動redis服務端 redis-server redis.conf 3,寫入數據 127.0.0.1:6379> set name tiger OK 127.0.0.1:6379> set age 18 OK 127.0.0.1:6379> set addr daxuecheng OK 127.0.0.1:6379> save OK 4,檢查rdb文件是否存在,然后備份rdb文件(這是生成環境中需要做的,以免切換不成功導致數據丟失) 5,開啟AOF持久化 CONFIG set appendonly yes # 開啟AOF功能 CONFIG SET save "" # 關閉RDB功能
四、redis主從同步
1,准備三個redis配置文件
cd /opt/redis_conf redis-6380.conf # 主數據庫master redis-6381.conf # 從庫slave redis-6382.conf # 從庫slave
2,在配置文件寫入配置信息,是哪個內容一樣,只是端口不一樣而已
port 6380 daemonize yes pidfile /data/6380/redis.pid loglevel notice logfile "/data/6380/redis.log" dbfilename dump.rdb dir /data/6380 protected-mode no 可以使用這一條命令通過redis-6380.conf生成6381和6382的配置文件 sed "s/6380/6381/g" redis-6380.conf > redis-6381.conf sed "s/6380/6382/g" redis-6380.conf > redis-6382.conf
3,在6381和6382文件中加入以下配置,使其成為從數據庫
slaveof 127.0.0.1 6380
4,開啟這三個redis服務,確保啟動正常,並查看狀態
1,首先開啟是三個redis服務
redis-server /opt/redis_conf/redis-6380.conf
redis-server /opt/redis_conf/redis-6381.conf
redis-server /opt/redis_conf/redis-6382.conf
2,用三個客戶端連接
redis-cli -p 6380 info replication redis-cli -p 6381 info replication redis-cli -p 6382 info replication
5,驗證redis主從復制功能
1,6380寫入數據,在6381和6382中查看數據是否同步 2,看是否能在6381和6382中寫入數據(正常情況下是不能的)
6,手動切換主從復制
也就是當6380進程死掉后,相當於主庫沒了,此時就需要從6381和6382中選擇一個作為主庫,完成主從復制切換
6.1 把最開始的主庫6380進程給殺死
kill -9 進程id
6.2 在6382的redis下執行這條命令(此時我們選擇6382為主庫)
slaveof no one
6.2 此時6381為從庫,把原來的主庫指向改為6382
slaveof no one slaveof 127.0.0.1 6382
6.3 可以驗證此時的主從復制功能
1,在6382上寫入數據,在6381上查看數據(正常情況下是數據同步的)
2,在6381上寫數據(正常情況下為報錯)
7,哨兵sentinel
對於第6步來說,必須要我們手動切換主從配置,其實是不科學的,所以,大佬開發了哨兵sentinel,用哨兵去監控主庫,當主庫掛掉的時候,哨兵從從庫中決策出一個新的主庫,剩余的從庫就作為新的主庫的從庫。
7.1准備三個哨兵配置文件
touch /opt/redis_conf/redis-26380.conf touch /opt/redis_conf/redis-26381.conf touch /opt/redis_conf/redis-26382.conf
7.2修改redis-26380.conf配置文件
port 26380 dir /var/redis/data/ logfile "26380.log" sentinel monitor qishi2master 127.0.0.1 6380 2 sentinel down-after-milliseconds qishi2master 30000 sentinel parallel-syncs qishi2master 1 sentinel failover-timeout qishi2master 180000 daemonize yes
7.3再配置另外兩個哨兵
sed "s/26380/26381/g" redis-26380.conf > redis-26381.conf sed "s/26380/26382/g" redis-26380.conf > redis-26382.conf
7.4啟動是三個哨兵
redis-sentinel /opt/redis_conf/redis-26380.conf redis-sentinel /opt/redis_conf/redis-26381.conf redis-sentinel /opt/redis_conf/redis-26382.conf
7.5查看進程
ps -ef | grep redis-sentinel
7.6可以查看三個哨兵的狀態
redis-cli -p 26380 info sentinel redis-cli -p 26381 info sentinel redis-cli -p 26382 info sentinel
7.7測試,把主庫干掉
1,查看主庫進程id ps -ef | grep redis 2,殺死主庫進程 kill -9 主庫進程ID 3,查看6381和6382的狀態 redis-cli -p 6381 info replication redis-cli -p 6381 info replication
五、redis集群
1,准備6個配置文件
1,我們把集群的配置文件放在/opt/redis_conf/redis_cluster目錄 mkdir /opt/redis_conf/redis_cluster 2,創建配置文件 cd redis_cluster touch redis-6000.conf redis-6001.conf redis-6002.conf redis-6003.conf redis-6004.conf redis-6005.conf
2,往配置文件中添加配置信息
1,進入redis-6000.conf vim redis-6000.conf 2,寫入如下配置信息 port 6000 daemonize yes dir "/opt/redis/data" logfile "/opt/redis/logs/6000.log" dbfilename "dump-6000.rdb" cluster-enabled yes # 開啟集群模式 cluster-config-file nodes-6000.conf # 集群內部的配置文件 cluster-require-full-coverage no # redis cluster需要16384個slot都正常的時候才能對外提供服務,
換句話說,只要任何一個slot異常那么整個cluster不對外提供服務。 因此生產環境一般為no 3,配置其他5個配置文件 sed "s/6000/6001/g" redis-6000.conf > redis-6001.conf sed "s/6000/6002/g" redis-6000.conf > redis-6002.conf sed "s/6000/6003/g" redis-6000.conf > redis-6003.conf sed "s/6000/6004/g" redis-6000.conf > redis-6004.conf sed "s/6000/6005/g" redis-6000.conf > redis-6005.conf
3,啟動這6個redis集群節點
redis-server redis-6000.conf redis-server redis-6001.conf redis-server redis-6002.conf redis-server redis-6003.conf redis-server redis-6004.conf redis-server redis-6005.conf
2,查看進程
ps -ef | grep redis
現在是無法往節點上添加數據的,因為哈希槽還沒分配
我們需要使用redis-trib.rb去分配集群的哈希槽,但這個腳本需要使用ruby環境去執行,所以我們需要安裝ruby解釋器
4,安裝ruby解釋器
1 下載ruby wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz 2 安裝ruby tar -xvf ruby-2.3.1.tar.gz cd ruby-2.3.1/ ./configure --prefix=/opt/ruby/ make && make install 3 配置ruby的環境變量 vim /etc/profile 寫入如下配置 PATH=$PATH:/opt/ruby/bin 4,讀取 source /etc/profile
5,安裝ruby gem包管理工具
1 下載gem包管理工具 wget http://rubygems.org/downloads/redis-3.3.0.gem 2 安裝 gem install -l redis-3.3.0.gem
6,開啟集群
/opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 127.0.0.1:6000 127.0.0.1:6001 127.0.0.1:6002
127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 命令說明: --replicas # 表示進行身份授權 1 # 表示每個主節點,只有一個從節點 # 集群會自動分配主從關系 6000、6001、6002為主服務器master 6003、6004、6005為從服務器slave
7,可以查看集群狀態與槽位
redis-cli -p 6000 cluster info redis-cli -p 6001 cluster info redis-cli -p 6002 cluster info redis-cli -p 6003 cluster info redis-cli -p 6004 cluster info redis-cli -p 6005 cluster info
8,使用命令連接redis集群
redis-cli -p 6000 -c -c 參數表示連接集群