Tuesday, June 30, 2015

Mysql new user addition

login as root

add new user:

mysql> CREATE USER 'some_user'@'localhost' IDENTIFIED BY 'my_password';


grant all the permissions to the new user:

mysql> GRANT ALL PRIVILEGES ON *.* TO ’some_user’@’localhost’;

grant only usage permissions to the newly crated user:
mysql> GRANT USAGE ON *.* TO 'some_user'@'localhost' IDENTIFIED BY 'my_password';

Saturday, June 13, 2015

Big data

Large scale data processing - hadoop
High rate data processing - kafka + storm    (optional samza)
Map-Reduce

Wednesday, June 3, 2015

zabbix agent installation in AWS

[ec2-user@ip-172-31-14-34 ~]$ wget http://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/2.4.5/zabbix-2.4.5.tar.gz/download
[ec2-user@ip-172-31-14-34 ~]$ mv download zabbix-2.4.5.tar.gz
[ec2-user@ip-172-31-14-34 ~]$ tar -xf zabbix-2.4.5.tar.gz
[ec2-user@ip-172-31-14-34 ~]$ cd zabbix-2.4.5
[ec2-user@ip-172-31-14-34 zabbix-2.4.5]$ sudo yum groupinstall "Development Tools"
[ec2-user@ip-172-31-14-34 zabbix-2.4.5]$ ./configure --help
[ec2-user@ip-172-31-14-34 zabbix-2.4.5]$ ./configure --prefix=/opt/zabbix-2.4.5 --enable-agent
[ec2-user@ip-172-31-14-34 zabbix-2.4.5]$ sudo make install
[ec2-user@ip-172-31-14-34 zabbix-2.4.5]$ sudo vim /opt/zabbix-2.4.5/etc/zabbix_agentd.conf
Server=172.31.29.64   //point to the ip address of zabbix server
ServerActive=172.31.29.64   //point to the ip address of zabbix server
AllowRoot=1  //change from 0 to 1
[ec2-user@ip-172-31-14-34 zabbix-2.4.5]$ sudo /opt/zabbix-2.4.5/sbin/zabbix_agentd -c /opt/zabbix-2.4.5/etc/zabbix_agentd.conf
[ec2-user@ip-172-31-14-34 zabbix-2.4.5]$ ps -ef | grep zabbix



Friday, March 20, 2015

MySql installation using RPM packages

(Source: http://dev.mysql.com/doc/refman/5.6/en/linux-installation-rpm.html)

To download MySQL community server, follow http://dev.mysql.com/downloads/mysql/ -> GA release -> linux-generic ->
MySQL-server-5.6.23-1.linux_glibc2.5.x86_64.rpm

To download MySQL community client, follow http://dev.mysql.com/downloads/mysql/ -> GA release -> linux-generic ->
MySQL-client-5.6.23-1.linux_glibc2.5.x86_64.rpm

yum localinstall Downloads/MySQL-server-5.6.23-1.linux_glibc2.5.x86_64.rpm

The above command internally executes all the below steps.

shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server

less /etc/groups#should display mysql group
which mysqld #should display /usr/sbin/mysqld
ls /etc/rc.d/init.d #should contain mysql file. This file is used to start MySQL as system service.
chkconfig --list#should display MySQL with at least one flag ON
ls /var/lib/mysql# this is used as the data directory. This should contain all the core databases initialized by /usr/bin/mysqld_safe script.

yum localinstall Downloads/MySQL-client-5.6.23-1.linux_glibc2.5.x86_64.rpm

which mysql # should display /usr/bin/mysql

service mysql start # starts mysqld

mysql -uroot -ppassword # mysqld_safe stores password in file $home/.mysql-secret. If file not found in home directory, try to find in / directory.

As of MySQL 5.6.8, new RPM install operations (not upgrades) invoke mysql_install_db with the --random-passwords option that provides for more secure MySQL installation. Invoking mysql_install_db with --random-passwords causes it to assign a random password to the MySQL root accounts, set the “password expired” flag for those accounts, and not create anonymous-user MySQL accounts. It will be necessary after installation to start the server, connect as root using the password written to the $HOME/.mysql_secret file, and assign a new root password. Until this is done, root cannot do anything else. This must be done for each root account you intend to use. To change the password, you can use the SET PASSWORD statement (for example, with the mysql client).


Mysql innodb Btree indexes

The Btree is simply a collection of one or more data pages, called nodes. The  non-leaf nodes help in reaching the leaf nodes. Depending on the height of the tree, reaching leaf nodes might require traversing through multiple levels of non-leaf nodes.

In Innodb, the leaf nodes are nothing but index pages. The index pages contain index records.

For non-clustered (secondary) indexes, index record contains an index key value and a pointer to the data page containing the corresponding data record.

For clustered indexes (primary key) , the index pages are nothing but data pages.

(reference book: pro mysql, chapter 2 (index concepts))

Thursday, March 19, 2015

Why process forking?

If a a child process is forked from parent process, whenever child process crashes, linux will notify the parent process through events. For this parent process has to register a callback with linux, so that linux will call the callback with events.

When forking happens a child process is created which is an exact replica of parent process even with same register contents. Both processes continue to execute individually. For forking parent would have just executed fork() call. Upon execution parent will receive child process id as fork() return value. Child will get null as fork() return value.

Fork also has another advantage. We can share common data between both processes. Linux allows this.

Monday, March 16, 2015

Mysql InnoDB’s gap locks

InnoDB’s gap locks (http://www.mysqlperformanceblog.com/2012/03/27/innodbs-gap-locks/)

One of the most important features of InnoDB is the row level locking. This feature provides better concurrency under heavy write load but needs additional precautions to avoid phantom reads and to get a consistent Statement based replication. To accomplish that, row level locking databases also acquire gap locks.

What is a Phantom Read

A Phantom Read happens when in a running transaction, two identical statements get different values, because some other transaction has modified the table’s rows. For example:

transaction1> START TRANSACTION;
transaction1> SELECT * FROM t WHERE i > 20 FOR UPDATE;
+------+
| i |
+------+
| 21 |
| 25 |
| 30 |
+------+

transaction2> START TRANSACTION;
transaction2> INSERT INTO t VALUES(26);
transaction2> COMMIT;
transaction1> select * from t where i > 20 FOR UPDATE;
+------+
| i |
+------+
| 21 |
| 25 |
| 26 |
| 30 |
+------+
Phantom reads do not occur if you’re simply doing a SELECT. They only occur if you do UPDATE or DELETE or SELECT FOR UPDATE. InnoDB provides REPEATABLE READ for read-only SELECT, but it behaves as if you use READ COMMITTED for all write queries, in spite of your chosen transaction isolation level (considering only the two most common isolation levels, REPEATABLE READ and READ COMMITTED).

What is a gap lock?

A gap lock is a lock on the gap between index records. Thanks to this gap lock, when you run the same query twice, you get the same result, regardless other session modifications on that table. This makes reads consistent and therefore makes the replication between servers consistent. If you execute SELECT * FROM id > 1000 FOR UPDATE twice, you expect to get the same value twice. To accomplish that, InnoDB locks all index records found by the WHERE clause with an exclusive lock and the gaps between them with a shared gap lock.

This lock doesn’t only affect to SELECT … FOR UPDATE. This is an example with a DELETE statement:
transaction1 > SELECT * FROM t;
+------+
| age |
+------+
| 21 |
| 25 |
| 30 |
+------+
Start a transaction and delete the record 25:
transaction1 > START TRANSACTION;
transaction1 > DELETE FROM t WHERE age=25;
At this point we suppose that only the record 25 is locked. Then, we try to insert another value on the second session:
transaction2 > START TRANSACTION;
transaction2 > INSERT INTO t VALUES(26);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
transaction2 > INSERT INTO t VALUES(29);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
transaction2 > INSERT INTO t VALUES(23);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
transaction2 > INSERT INTO t VALUES(31);
Query OK, 1 row affected (0.00 sec)
After running the delete statement on the first session, not only the affected index record has been locked but also the gap before and after that record with a shared gap lock preventing the insertion of data to other sessions.

How to troubleshoot gap locks?

Is possible to detect those gap locks using SHOW ENGINE INNODB STATUS:
---TRANSACTION 72C, ACTIVE 755 sec
4 lock struct(s), heap size 1248, 3 row lock(s), undo log entries 1
MySQL thread id 3, OS thread handle 0x7f84a78ba700, query id 163 localhost msandbox
TABLE LOCK table `test`.`t` trx id 72C lock mode IX
RECORD LOCKS space id 19 page no 4 n bits 80 index `age` of table `test`.`t` trx id 72C lock_mode X
RECORD LOCKS space id 19 page no 3 n bits 80 index `GEN_CLUST_INDEX` of table `test`.`t` trx id 72C lock_mode X locks rec but not gap
RECORD LOCKS space id 19 page no 4 n bits 80 index `age` of table `test`.`t` trx id 72C lock_mode X locks gap before rec
If you have lot of gaps locks in your transactions affecting the concurrency and the performance you can disable them in two different ways:

1- Change the ISOLATION level to READ COMMITTED. In this isolation level, it is normal and expected that query results can change during a transaction, so there is no need to create locks to prevent that from happening.
2- innodb_locks_unsafe_for_binlog = 1. Disables the gap locks except for foreign-key constraint checking or duplicate-key checking.

The most important difference between these two options is that the second one is a global variable that affects all sessions and needs a server restart to change its value. Both options cause phantom reads (non repeatable reads) so in order to prevent problems with the replication you should change the binary log format to “row”.

Depending on the statement, the behavior of these locks can be different. In the following link there is a good source of information:

http://dev.mysql.com/doc/refman/5.1/en/innodb-locks-set.html.

Conclusion
MySQL uses REPEATABLE READ as the default isolation level so it needs to lock the index records and the gaps to avoid phantom reads and to get a consistent Statement based replication. If your application can deal with phantom reads and your binary log is in row format, changing the ISOLATION to READ COMMITTED will help you to avoid all those extra locks. As a final advice, keep your transactions short :)

Followers