Linux之數據庫操做

  1、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 修改主數據庫配置文件node

1,進入配置文件
vim /etc/my.cnf
2,添加配置信息
[mysqld]   # 若是配置文件裏面有這一行,就只須要加如下兩行配置就能夠了
server-id=1
log-bin=qishi2-logbin

  2.2 進入數據庫mysql

systemctl start mariadb

  2.3 建立主從複製用戶web

1,進入數據庫
mysql -uroot -p
2,建立用戶
create user zijin@'%' identified by 'zijin';

  2.4 給從庫帳號受權redis

grant replication slave on *.* to 'zijin'@'%';

  2.5 把主庫數據導入從庫sql

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 啓動數據庫vim

systemctl start mariadb

  3.3 導入主庫的數據緩存

1,鏈接數據庫
mysql -u root -p 
2,導入數據
source /opt/alldb.sql

  3.4 配置複製的參數,slave從庫鏈接master主庫的配置(最重要的)ruby

在數據庫的環境下輸入:
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 #說明從庫沒有新建的權限

  3、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功能

  4、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

   5、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 # 表示每一個主節點,只有一個從節點 # 集羣會自動分配主從關係 60006001、6002爲主服務器master 60036004、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 參數表示鏈接集羣
相關文章
相關標籤/搜索