Install docker on ubuntu 14/04

# sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

# apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

# add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"

# apt-get install docker-ce -y

mysql repo for linux

Mysql repository:-

https://dev.mysql.com/downloads/repo/

https://dev.mysql.com/downloads/repo/yum/     for centos

https://dev.mysql.com/downloads/repo/apt/       for ubuntu

Attach digital ocean block storage volume to containers

Configure your volume
To configure your volume, run the following commands on your Droplet.

# SSH into your droplet
$ ssh root@192.168.1.12 
To format the volume, copy and paste the text below:
# Format the volume with ext4 Warning: This will erase all data on the volume. Only run this command on a volume with no existing data.
$ sudo mkfs.ext4 -F /dev/disk/by-id/scsi-0DO_Volume_db2vol-nyc1Copy 
To mount the volume, copy and paste the text below:
# Create a mount point under /mnt
$ sudo mkdir -p /mnt/db2vol-nyc1

# Mount the volume
$ sudo mount -o discard,defaults /dev/disk/by-id/scsi-0DO_Volume_db2vol-nyc1 /mnt/db2vol-nyc1

# Change fstab so the volume will be mounted after a reboot
$ echo '/dev/disk/by-id/scsi-0DO_Volume_db2vol-nyc1 /mnt/db2vol-nyc1 ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstabCopy 
The volume is now accessible at /mnt/db2vol-nyc1, and you are able to write files and store other data. This data will persist if you detach the volume, and will continue to be available when the volume is reattached to another Droplet. If you want you can customize the commands above, please refer to the following articles:

How To Partition and Format DigitalOcean Block Storage Volumes in Linux
How To Partition and Format Storage Devices in Linux

mysql enabling the query cache performance impact

MySQL query cache has its own downsides too. Well, let’s think about this: If you are frequently updating the table, you are then invalidating the query cache for ALL queries cached for that table. So really, anytime you have a “frequently updated table” means you’re probably not going to get any sort of good usage from the MySQL query cache. See the below example.

query cache status

SHOW STATUS LIKE "qcache%";
Qcache_lowmem_prunes is the status variable which indicates how many times MySQL had to clear/prune some data from the Qcache to make space for the outputs of other queries. We need to observe the Qcache_lowmem_prunes  status variable and try to increase/adjust the size of the cache till we get a very low value ratio for the variable.
It is also undesirable to keep the query cache relatively high value at 256 MB as the Qcache invalidation becomes costly. 

TMP_TABLE_SIZE AND MAX_HEAP_TABLE_SIZE

https://www.saotn.org/mysql-innodb-performance-improvement/

The actual limit is determined from whichever of the values of tmp_table_size and max_heap_table_size is smaller. If an in-memory temporary table exceeds the limit, MySQL automatically converts it to an on-disk MyISAM table. Increase the value of tmp_table_size (and max_heap_table_size if necessary) if you do many advanced GROUP BY queries and you have lots of memory.

You can compare the number of internal on-disk temporary tables created to the total number of internal temporary tables created by comparing the values of the Created_tmp_disk_tables and Created_tmp_tables variables.


circumstances which could create temporary tables using MEMORY but that can go to MyISAM (so disk) if too large:
  • If there is an ORDER BY clause and a different GROUP BY clause, or if the ORDER BY or GROUP BY contains columns from tables other than the first table in the join queue;
  • DISTINCT combined with ORDER BY may require a temporary table;
  • In the case of the SQL_SMALL_RESULT option, MySQL uses an in-memory temporary table, unless the query also contains elements (described later) that require on-disk storage.
There are some conditions which will force the temporary table to use MyISAM :
  • Presence of a BLOB or TEXT column in the table;
  • Presence of any column in a GROUP BY or DISTINCT clause larger than 512 bytes;
  • Presence of any column larger than 512 bytes in the SELECT list, if UNION or UNION ALL;
tmp_table_size      = 16M
max_heap_table_size = 32M
max_connections = 1000

Value of tmp_table_size OR max_heap_table_size which ever is lower is taken
memory used if all connections are made 1000x16M=1600M  

innodb_flush_log_at_trx_commit log buffer

innodb_flush_log_at_trx_commit=1
The default value of 1 is required for full ACID compliance. The contents of the InnoDB 
log buffer are written out to the log file at each transaction commit and the log file is flushed to disk.

0 = log buffer are written to the log file approximately once per second and the log file is flushed to disk
2 = log buffer are written to the log file after transaction commit and the log file is flushed to disk approximately once per second


logbuffer-->logfiles[ib_logfileN] --> redolog [ib_logfile0 , ib_logfile1] size limit is 512G (default-48M)

log buffer
The memory area that holds data to be written to the log files that make up the redo log. It is controlled by the innodb_log_buffer_size configuration option.

redo log

A disk-based data structure used during crash recovery, to correct data written by incomplete transactions. During normal operation, it encodes requests to change InnoDB table data, which result from SQL statements or low-level API calls through NoSQL interfaces. Modifications that did not finish updating the data files before an unexpected shutdown are replayed automatically.

The redo log is physically represented as a set of files, typically named ib_logfile0 and ib_logfile1. The data in the redo log is encoded in terms of records affected; this data is collectively referred to as redo. The passage of data through the redo logs is represented by the ever-increasing LSN value. The original 4GB limit on maximum size for the redo log is raised to 512GB in MySQL 5.6.3.

The disk layout of the redo log is influenced by the configuration options innodb_log_file_size, innodb_log_group_home_dir, and (rarely)innodb_log_files_in_group. The performance of redo log operations is also affected by the log buffer, which is controlled by the innodb_log_buffer_size configuration option.


ansible cheat sheet


Friday, August 12, 2016

How To - Ansible adhoc commands (One Liners)

Make a note of below assumptions for one liners explained on this page are as below,

User root - the superhero, superuser
User normaluser - user that has root sudo privilege and all ansible required connectivity in place User user1 - user that has root sudo privilege

Understanding basic differences in commands using various users with various rights

To run command on all servers to check reach ability from ansible point of view with 25 parallel forks with current user (i.e. root or normaluser)
# ansible all -m ping -f 25
To run command on all servers to check reach ability from ansible point of view with 25 parallel forks when you logged in as normal user (user1) and using ansible privilege of another normal user (normaluser)
# sudo -u normaluser ansible all -m ping -f 25
To run a command on list of servers (IP addresses) specified in a text file with 25 parallel forks with current user (i.e. root or normaluser)
# ansible all -i /tmp/my-inventory -m ping -f 25
To run a command on list of servers (IP addresses) specified in a text file with 25 parallel forks when you logged in as normal user (user1) and using ansible privilege of another normal user (normaluser)
# sudo -u normaluser ansible all -i /tmp/my-inventory -m ping -f 25
To run command on all servers in a group with 25 parallel forks with current user (i.e. root or normaluser)
# ansible all -a "df -h" -f 25
To run command on all servers in a group with 25 parallel forks when you logged in as normal user (user1) and using ansible privilege of another normal user (normaluser)
# sudo -u normaluser ansible all -a "df -h" -f 25
To run command which requires super user rights on all servers in a group with 25 parallel forks with current user (i.e. root)
# ansible all -a "/etc/init.d/snmpd restart" -f 25
To run command which requires super user rights on all servers in a group with 25 parallel forks with current user (i.e. normaluser)
# ansible all -a "/etc/init.d/snmpd restart" -f 25 --sudo 
To run command which requires super user rights on all servers in a group with 25 parallel forks when you logged in as normal user (user1) and using ansible privilege of another normal user (normaluser)
# sudo -u normaluser ansible all -a "/etc/init.d/snmpd restart " -f 25 --sudo 

Ping Module

To limit ping for random first 10 servers from a group of hosts defined in /etc/ansible/hosts file
# sudo -u normaluser all dev -m ping --limit allvmhosts[0-10]
To limit Ping some servers from "api" group of hosts defined in /etc/ansible/hosts file i.e. [11 to 20]
# sudo -u normaluser ansible all -m ping --limit allvmhosts[11-20]
In case of JSON Error "Error: ansible requires a json module, none found!" Try your command in raw mode as below
# sudo -u normaluser ansible all -m ping  -m raw

Service Module

To install a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=installed"
To start a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=started"
To stop a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=stopped"
To restart a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=restarted"
To reload a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=reloaded"
To enable a service on all servers in a group using service module to get started in OS startup/bootup
# ansible all -m service -a "name=snmpd enabled=yes"
To disable a service on all servers in a group using service module to get started in OS startup/bootup
# ansible all -m service -a "name=snmpd enabled=no"

User Module

To add a user 'sam' and assign shell as /bin/bash, secondary group as wheel to it on a group on all servers in a group
# sudo -u normaluser ansible all -m user -a "name=sam shell=/bin/bash groups=wheel"
To add the user 'sam' with a specific uid and a primary group of 'admin' on all servers in a group
# sudo -u normaluser ansible all -m user -a "name=sam comment="Samual Pol" uid=1040 group=admin"
To add the user 'sam' with a bash shell, appending the group 'admins' and 'developers' to the user's groups on all servers in a group
# sudo -u normaluser ansible all -m user -a "name=sam shell=/bin/bash groups=admins,developers append=yes"
To Remove the user 'sam' from all servers in a group
# sudo -u normaluser ansible all -m user -a "name=sam state=absent remove=yes"

Shell Module

To create a backup file from command line with sudo on a group
# sudo -u normaluser ansible all -m shell -a "cp /etc/sudoers /etc/sudoer_DDMMYYYY" --sudo
To replace content of a file with special characters with sudo on a group
# sudo -u normaluser ansible all -m shell -a "sed -i 's|\# %wheel\tALL=(ALL)\tALL|%wheel\tALL=(ALL)\tALL|g' /etc/sudoers" --sudo
To change primary group of a user on a group
# sudo -u normaluser ansible all -m shell -a "usermod -aG wheel idcadm"
To execute the command in remote shell by specifying stdout going to the specified file on all remote servers in a group
# sudo -u normaluser ansible all -m shell -a "somescript.sh >> somelog.txt"
To change the working directory to somedir/ before executing the command
# sudo -u normaluser ansible all -m shell -a "somescript.sh >> somelog.txt chdir=somedir/"

File Module =

To change permissions of a file on all servers in a group
# sudo -u normaluser ansible all -m file  -a "path=/usr/bin/sudo  state=touch mode='4755' " --sudo 
To change file ownership, group and mode using octal numbers (Note: first digit should always be 0 whenever octal used)
# sudo -u normaluser ansible all -m file  -a "path=/etc/ntp.conf owner=ntp group=ntp mode=0644"
To create symbolic link
# sudo -u normaluser ansible all -m file  -a "src=/file/to/link/to dest=/path/to/symlink owner=sam group=sam state=link"
To touch a file, using symbolic modes to set the permissions (equivalent to 0644)
# sudo -u normaluser ansible all -m file  -a "path=/etc/test.conf state=touch mode='u=rw,g=r,o=r'"


Replace Module

To replace a particular line in a file on a group
# sudo -u normaluser ansible all -m replace -a "dest=/root/test.sh regexp='^(\sread -p \"gateway recover after nodes \" gateway_recover_after_nodes)$' replace='XXXXXXXXX'"

lineinfile Module

To append a line in file use lineinfilemodule on a group
# sudo -u normaluser ansible all -m lineinfile  -a "dest=/etc/sudoers_07May2015 line='# Sudo access for running passwd command'"

setfacl

I actually found something that so far does what I asked for, sharing here so anyone who runs into this issue can try out this solution:
sudo setfacl -Rdm g:www-data:rwx /var/www/pub_html
sudo setfacl -Rm g:www-data:rwx /var/www/pub_html
R is recursive, which means everything under that directory will have the rule applied to it.
d is default, which means for all future items created under that directory, have these rules apply by default. m is needed to add/modify rules.
The first command, is for new items (hence the d), the second command, is for old/existing items under the folder. Hope this helps someone out as this stuff is a bit complicated and not very intuitive.

ssh-copy-id on non standard port

In my case ,  # ssh-copy-id -i ~/.ssh/id_rsa.pub '-p 51522  username@20.94.56.24'    # here 20.94.56.24 is destination ssh server  

Remove orphaned SQL Server Replication Publications

http://atakala.com/Browser/Item.aspx?user_id=amos&dict_id=2100
Remove orphaned SQL Server Replication Publications
After adding and dropping SQL Server Replication several times, there were some orphaned publications that appeared in the replication monitor, but not under theDatabase Replication node in Enterprise Studio.

To clean them up, the following worked:

1. Add a new publication using the same name as the orphaned record (just publish one table).
2. Immediately delete the publication after it has been created. (no subscriptions are needed).

The orphaned records should now be gone.