php opcache

With the release of Ubuntu 14.04 and the proliferation of PHP 5.5, there is going to be a migration away from Alternative Performance Cache (APC) and toward PHP’s new built-in OPcache.

This is a logical move that seems destined for any interpreted language.  As websites have become more and more complicated with many processes running, opcode caching has become a necessity – fortunately, it’s simple to implement.

The php.net site has a nice page of all the runtime options available, but we will cover the basics here to get you started quickly.

All you need to do to get OPcache set up is to make changes in the php.ini file on your server.

Open php.ini In Your Favorite Text Editor
To get started open your php.ini file.

Apache web-servers

/etc/php5/apache2/php.ini
Nginx web-servers with PHP-FPM

/etc/php5/fpm/php.ini
Enable the OPcache
To enable the OPcache, change to the following lines — easy enough!


;opcache.enable=0
Change to:


opcache.enable=1
Note: you have to uncomment this line as well as change the “0″ to “1″.

Modify the Amount of RAM the OPcache Will Use
With OPcache, there is a trade-off between speed and the amount of RAM used. The more RAM you are willing to dedicate to storing opcode, the more opcode that can be stored. There is a diminishing return at some point, because some code will execute rarely, or your code base might not be that big. It is worth playing with this setting to see where you get the best performance-versus-RAM trade-off.  This setting is in megabytes.


;opcache.memory_consumption=64
Change to:


opcache.memory_consumption=128
Boost the Number of Scripts that Can Be Cached
OPcache has a strange setting that requires you to not only adjust the amount of RAM, but also define the number of scripts that can be cached. You have the option of tuning this parameter for your own application too, especially if you find that your hit rate is not close to 100 percent.


;opcache.max_accelerated_files=2000
Change to:


opcache.max_accelerated_files=4000
Change the Revalidate Frequency
To make sure that the OPcache notices when you change your PHP code, you can set the revalidate frequency. Basically, this will tell the cache how often to check the timestamp on the files. This is measured in seconds.


;opcache_revalidate_freq = 2
Change to:


opcache_revalidate_freq = 240
Verify that the PHP OPcache Mod is Enabled
Believe it or not, that converts most of the settings you will need to get started. PHP5 has its own module system (since 5.4), so make sure that OPcache is enabled.


sudo php5enmod opcache
Restart PHP and Your Server
You should now be all set to start using PHP 5.5’s OPcache. You just need to restart your server to get it going.

Apache Web-Servers

sudo service apache2 restart
Nginx Web-Servers

sudo service nginx restart

mysql isolation level

https://www.facebook.com/note.php?note_id=244956410932
There is a lot of documentation available on transaction isolation levels for InnoDB includingthe InnoDB transaction model, a description of the isolation levels, notes on locks, locks set by statements and notes on consistent non-locking reads.

There is another issue that has not been documented. The code that creates a read view(snapshot) for InnoDB transactions is a source of mutex contention on kernel_mutex. This code is run once per statement for read-committed and once per transaction for repeatable-read.

The function read_view_open_now is run to create a snapshot. It copies the list of uncommitted transactions into a per-transaction data structure. Memory is allocated from the per-transaction heap to store the copied data and that allocation might require a call to malloc. All of this work is done while holding kernel_mutex and other threads are unlikely to get much work done in InnoDB when kernel_mutex is locked.

Subscribe to feature request 49169 if you are interested in this. I am not sure if there is an easy fix. Work can be done to reduce the chance of allocating memory while copying the open transaction list. But splitting kernel_mutex into several locks might be hard.

When repeatable-read is used there are fewer calls to read_view_open_now and there is less contention on kernel_mutex. I ran sysbench to measure the difference in performance.

http://dev.mysql.com/doc/refman/5.5/en/innodb-transaction-isolation-levels.html#isolevel_read-committed
https://easyengine.io/tutorials/mysql/enable-innodb-file-per-table/
https://www.brightbox.com/blog/2013/10/31/on-mysql-locks/
http://stackoverflow.com/questions/20321407/mysql-table-lock-instead-of-row-locks
https://www.percona.com/blog/2015/01/14/mysql-performance-implications-of-innodb-isolation-modes/
https://www.percona.com/blog/2012/08/28/differences-between-read-committed-and-repeatable-read-transaction-isolation-levels/

https://www.simple-talk.com/sql/t-sql-programming/questions-about-t-sql-transaction-isolation-levels-you-were-too-shy-to-ask/

https://www.percona.com/blog/2012/07/31/innodb-table-locks/


http://stackoverflow.com/questions/20173670/clarifying-the-difference-between-row-level-lock-in-innodb-engine-and-table-leve


change isolation level:-
SELECT @@GLOBAL.tx_isolation, @@tx_isolation; 

SET global TRANSACTION ISOLATION LEVEL READ COMMITTED; 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SET global TRANSACTION ISOLATION LEVEL REPEATABLE READ; 

SET GLOBAL tx_isolation='READ-COMMITTED';

SET SESSION tx_isolation='READ-COMMITTED';

]# mysql -e "show variables" | grep isolation

tx_isolation    READ-COMMITTED

## disable binlog  OR
SET GLOBAL binlog_format = 'ROW';  

error: rlm_sql_mysql: MySQL error 'Binary logging not possible. Message: Transaction level 'READ-COMMITTED' in InnoDB is not safe for binlog mode 'STATEMENT''

SET GLOBAL binlog_format = 'ROW';  


If you are not planning to use your MySQL server for the replication consider turning the binary logging off by removing the option --log-bin from the command options for the mysqld utility starting the MySQL server.



Innodb buffer pool insight

http://dev.mysql.com/doc/refman/5.6/en/optimizing-innodb-diskio.html

Buffer pool content

InnoDB data is stored in 16 KB pages (blocks), either on disk (ibdata files) or in memory (buffer pool). Each one of these pages may contain one or more row.
The buffer pool is basically a cache for these pages: once a page’s content is requested by a query, the page is cached in the buffer pool.
You may be wondering what kind of data is stored in these pages. Short answer: indexes (on a size point of view).
MySQL offers a very useful INFORMATION_SCHEMA database, which contains since version 5.5 a table named innodb_buffer_page. This table holds 1 record (row) per page in the buffer pool, including interesting data such as what the page contains.
https://dev.mysql.com/doc/refman/5.5/en/innodb-buffer-pool.html#innodb-buffer-pool-lru

innodb_buffer_pool_instances

If innodb_buffer_pool_size is set to more than 1GB, innodb_buffer_pool_instances divides the InnoDB buffer pool into a specific number of instances. The default was 1 in MariaDB 5.5, but for large systems with buffer pools of many gigabytes, many instances can help reduce contention concurrency. The default is 8 in mysql 5.7 and MariaDB 10.0, with the exception of 32-bit Windows, where it depends on the value of innodb_buffer_pool_size. Each instance manages its own data structures and takes an equal portion of the total buffer pool size, so for example if innodb_buffer_pool_size is 4GB and innodb_buffer_pool_instances is set to 4, each instance will be 1GB. Each instance should ideally be at least 1GB in size.
Writing modified pages back to disk
When you make a modification to an InnoDB page, InnoDB does not write it immediately to disk. Instead a delta is written to the transaction log (see below).
These modified-only-in-memory pages are typically reffered to as dirty pages. Writing down dirty pages happens as part of InnoDB's background process which continually performs work every 1 second, and it adjusts well to peaks and troughs in load with a feature introduced in MySQL 5.5 and further improved in MySQL 5.6 called adaptive flushing. However, if there are too many dirty pages and there is not enough free space, InnoDB may be forced to synchronously flush a dirty page to make free space.
This delaying allows InnoDB to perform fewer random writes, since there will usually be multiple changes to the same page, or by default pages in the same extent (1MB allocation unit on disk). MySQL also 5.6 allows you to disable this for SSDs.
(Note: Domas makes a comment about the doublewrite buffer below, and how I might be over-selling request merging :) It's outside of the scope covered here, but described in Facebook's blog and the MySQL manual).
Writing the transaction log
Any time you modify an InnoDB page a record of this modification is made to InnoDB's transaction log system (ib_logfile1 and ib_logfile0 on disk). This transaction log is designed to be sequentially written, and unless the server crashes the server will only ever write and not read from it.
The log files are cyclical by design. You can think of them as a concatentation of two (by default) that behave similar to tread on a tank. So the first one is filled, then the second, then InnoDB will start filling the first again. The writes to the transaction log will be 512B aligned and using the operating system's buffered IO. InnoDB actually relies on the operating system here to provide a cache of the log (which is not guaranteed). Since these writes are 512B - and a typical page is 4K what will happen without enough cache, is that the page will neeed to be read first, to make the partial-modification, then written back.
To reduce log file IO, InnoDB also has a log buffer (configured by innodb_log_buffer_size) that is filled with pending modifications which will be written and synced on any COMMIT by default.
Writing the slow log and general log
The general log is written out as each event is generated. It will be a sequential write, and is never synced or never read back (should be fairly low cost).
The slow query log is written out after query execution has finished. Similiar to the general log, it should also be very log cost.
Why I say "should" is that this also depends on filesystem choice. An fsync under ext3 does not flush one file, but all files. This has become known as the great fsync() bug. Also, the operating system will usually have a limit the amount of modifications queued up waiting to be written, so too much log activity could cause synchronous writing and impact other operations (see Domas' comment below).

IO on a Replication Master (aka binary log enabled)

The binary log is sequential IO written to as part of a COMMIT operation, but by default it is never flushed, so it is not crash-safe! It requires setting sync_binlog=1.
The binary log does not have its own in memory buffers, so it's possible if an out of date slave came online that the master server may need to read old binary log files in order to send previous server events. However in practice the master pushes changes to online slaves as they are committed so it should be write-only.
The server maps changes in the binary log to InnoDB's transaction log to ensure consistency by default.
IO on a Replication Slave
The relay logs are in the same format as the binary logs, but the IO semantics are a little different. The slave's IO_THREAD retrieves new events from the master and writes sequential IO relay log files on the slave. Then the SQL_THREAD then reads the changes from the relay logs and applies them. This read operation does not have any caches inside of MySQL to ensure no IO is performed, and instead relies on the operating system's buffered IO to assist. It's possible to decrease the amount of relay logs kept on the slave to assist in a better 'cache fit', but this also means that if the master fails there may be changes the slave was not aware of.

Writing the relay logs is not crash-safe by default, and requires setting sync_relay_log=1.
Also, the status files which maintain a mapping between the relay logs and master's binary logs (which are not a 1:1) are also not crash-safe. A new option in MySQL 5.6 allows you to use an internal table to maintain these status files for added safety, but it is not enabled by default. The new options are master-info-repository and relay-log-info-repository.

Summary
I am sure I made a couple of simplifications (for example not describing the doublewrite buffer), but hopefully it's a good start. There are a couple of key take away points I want to make:
Reads are foregrounded (except read-ahead) and don't provide the same opportunity reorder and merge - so that makes them typically random IO and very expensive. If you run a diagnostic command like iostat and you see a lot of reads on a warmed up server - this is something adding memory can usually solve.
Most writes are sequential or backgrounded. Both InnoDB and the operating system try to produce sequential IO and merge requests.
While InnoDB has always been crash safe by default, MySQL has not. There are new features in MySQL 5.6 which makes replication more reliable.

Innodb_buffer_pool_read_requests and Innodb_buffer_pool_reads are the values you need to monitor for buffer reads and disk read for the Buffer Pool.
What you are looking for is a cache miss rate of 1% or less based on this
Innodb_buffer_pool_reads X 100 / Innodb_buffer_pool_read_requests
I have written posts like this before
You should also be tracking how much data is being poured into the InnoDB transaction logs
Example
you could take the delta of Innodb_os_log_written (bytes written in a one second window) and divide it by the delta of Innodb_log_writes and track how many bytes are being flushed to the logs per second. If the result seems too high for you, you could tweek innodb_flush_method, innodb_flush_log_at_trx_commit, innodb_log_file_size and things of this nature.

find query per second (qps) with mytop

Centos6
1) using "mysqladmin status"

OR

 rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install mytop -y

mytop -uroot  --prompt  -d  databasename






imapsync

This summary is not available. Please click here to view the post.

tar backup and restore

To Create the Backup Archive:

sudo tar -cvpzf yourbackupfilename.tar.gz --exclude=/path/to/backup/yourbackupfilename.tar.gz --one-file-system /

In the last part of the command a single forward slash will backup the entire root directory, or you can specify a certain directory to backup.

Restoring your backup:

sudo tar -xvpzf /path/to/yourbackupfilename.tar.gz -C /directory/to/restore/to --numeric-owner

If restoring a complete root directory backup the last section of this command would simply be a forward slash /

grep email address from file

grep -E -o "\b[_a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b"

E=extended regex
o=only print matching string

optimum mysql configuration

 [mysql]  
 # CLIENT #  
 port              = 3306  
 socket             = /var/lib/mysql/mysql.sock  
 [mysqld]  
 # GENERAL #  
 user              = mysql  
 default-storage-engine     = InnoDB  
 socket             = /var/lib/mysql/mysql.sock  
 pid-file            = /var/lib/mysql/mysql.pid  
 # MyISAM #  
 key-buffer-size        = 32M  
 myisam-recover         = FORCE,BACKUP  
 # SAFETY #  
 max-allowed-packet       = 16M  
 max-connect-errors       = 1000000  
 sort_buffer_size = 1073741824  
 myisam_sort_buffer_size = 1073741824  
 # DATA STORAGE #  
 datadir            = /var/lib/mysql/  
 # BINARY LOGGING #  
 #log-bin            = /var/lib/mysql/mysql-bin  
 #expire-logs-days        = 14  
 #sync-binlog          = 1  
 # CACHES AND LIMITS #  
 #tmp-table-size         = 32M  
 #max-heap-table-size      = 32M  
 tmp-table-size         = 1G  
 max-heap-table-size      = 1G  
 query-cache-type        = 0  
 query-cache-size        = 0  
 max-connections        = 500  
 thread-cache-size       = 50  
 open-files-limit        = 65535  
 table-definition-cache     = 4096  
 table-open-cache        = 4096  
 join_buffer_size = 1048576  
 # INNODB #  
 innodb-flush-method      = O_DIRECT  
 innodb-log-files-in-group   = 2  
 innodb-log-file-size      = 256M  
 innodb-flush-log-at-trx-commit = 1  
 innodb-file-per-table     = 1  
 innodb-buffer-pool-size    = 12G  
 # LOGGING #  
 log-error           = /var/lib/mysql/mysql-error.log  
 log-queries-not-using-indexes = 1  
 slow-query-log         = 1  
 slow-query-log-file      = /var/lib/mysql/mysql-slow.log  

wget recursive ftp download folder

wget -m --ask-password --user="ftpuser@footballfan.com"  ftp://11.18.99.4/

create network bridge in ubunut 14.04

sudo apt-get update
sudo apt-get install bridge-utils
vim /etc/network/interfaces
 auto lo  
 iface lo inet loopback  
 #10.0.2.193 Bcast:10.0.3.255 Mask:255.255.254.0  
 auto br0  
 iface br0 inet static  
     address 10.0.2.193  
     netmask 255.255.254.0  
     gateway 10.0.2.1  
     dns-nameservers 202.94.66.2 8.8.8.8  
 #set static route for LAN  
     post-up route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1 dev br0  
     bridge_ports eth1  
     bridge_stp off  
     bridge_fd 0  
     bridge_maxwait 0  
 #bridge_stp off is a setting for spanning tree. If you have a possibility for network looks, you may want to turn this on.  
 #bridge_fd 0 turns off all forwarding delay. If you do not know what this is, you probably do not need it.  
 #bridge_maxwait 0 is how long the system will wait for the Ethernet ports to come up. Zero is no wait.  

auto br0
iface br0 inet static
 address 192.168.0.201
 netmask 255.255.255.0 
 gateway 192.168.0.1
 dns-nameservers 8.8.8.8 8.8.4.4
 bridge_ports eth0
 bridge_stp off
some more commands

 ip addr flush dev <interface 1>  
 ip addr flush dev <interface 2>  
 brctl addbr <bridge name>  
 brctl addif <bridge name> <interface 1> <interface 2>  
 ip link set dev <bridge name> up