Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Sunday, June 10, 2018

Working w/ Multiple Tables in MySQL


Using JOIN
----------

# this joins 2 table via "name" fields
#
# table 1

+----------+-----------+---------+------+------------+------------+
| name     | owner     | species | sex  | birth      | death      |
+----------+-----------+---------+------+------------+------------+
| Whistler | Gwen      | bird    | NULL | 1997-12-09 | NULL       |
| Gunner   | Mark      | dog     | m    | 1992-01-14 | NULL       |
| Heath    | Stephanie | penguin | m    | 2001-12-12 | NULL       |
| Winnona  | Stephanie | dog     | m    | 2009-03-18 | NULL       |
| Hunter   | Stephanie | fish    | f    | 1997-09-21 | NULL       |
| Splitter | Gwen      | fish    | m    | 1996-01-12 | 1999-03-04 |
| Styder   | Gwen      | fish    | f    | 2018-05-17 | NULL       |
+----------+-----------+---------+------+------------+------------+

# table 2

+----------+------------+------------+------------+
| name     | date       | type       | remark     |
+----------+------------+------------+------------+
| Whistler | 0000-00-00 | litter     | arghhh     |
| Gunner   | 2001-01-13 | litter     | not again  |
| Heath    | 1991-09-24 | broken rib | huhu       |
| Winnona  | 2003-09-09 | pregnant   | yeyy       |
| Hunter   | 2011-12-12 | sick       | waaa       |
| Splitter | 1993-01-01 | sick       | waaa       |
| Styder   | 1994-05-06 | pregnant   | again yeyy |
+----------+------------+------------+------------+

# query

mysql>
mysql> SELECT pet.name,
    -> TIMESTAMPDIFF(YEAR,birth,date) AS age,
    -> remark
    -> FROM pet INNER JOIN event
    ->   ON pet.name = event.name
    -> WHERE event.type = 'litter';


Using UNION
-----------

This is alternative to using OR:

mysql> select * from pet where owner='Gwen' union select * from pet where owner='Stephanie';
+----------+-----------+---------+------+------------+------------+
| name     | owner     | species | sex  | birth      | death      |
+----------+-----------+---------+------+------------+------------+
| Whistler | Gwen      | bird    | NULL | 1997-12-09 | NULL       |
| Splitter | Gwen      | fish    | m    | 1996-01-12 | 1999-03-04 |
| Styder   | Gwen      | fish    | f    | 2018-05-17 | NULL       |
| Heath    | Stephanie | penguin | m    | 2001-12-12 | NULL       |
| Winnona  | Stephanie | dog     | m    | 2009-03-18 | NULL       |
| Hunter   | Stephanie | fish    | f    | 1997-09-21 | NULL       |
+----------+-----------+---------+------+------------+------------+
6 rows in set (0.00 sec)

mysql>

Sunday, May 27, 2018

Redis Cluster on Centos 7


1. Install redis on all nodes
yum install -y epel-release
yum install -y redis

2. Configure master
vi /etc/redis.conf  # update the following line: bind 127.0.0.1
firewall-cmd --add-port=6379/tcp

3. Configure slaves
vi /etc/redis.conf  # update the following line: slaveof 6379

4. Restart and enable redis on all nodes
systemctl enable --now redis

5. Login to master and create a key to validate
127.0.0.1:6379> info replication
127.0.0.1:6379> redis-cli
127.0.0.1:6379> set 'a' 1

6. verify on the nodes that the replication is working
127.0.0.1:6379> redis-cli
127.0.0.1:6379> get 'a'  # you must get correct value set from the master
127.0.0.1:6379> info replication