Flattening an array in PHP

php

You might have a multidimensional array in PHP that you might need to flatten to make it easier to work with. Let’s take the example “myArray”.

As you see from the code block below, you can create a temporary object and then from there you use array_walk_recursive to flatten the array into an easy to use format.


$objTmp = (object) array('aFlat' => array());
array_walk_recursive($myArray, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);

nginx htpasswd – how do you add to the passwd file?

nginx

I’ve gradually been moving away from Apache and lighttpd over to nginx. It’s easier to setup and a lot quicker as well.

One task I ran into was to add a user account to an htpasswd file. I’m a sucker for forgetting commands really quickly, so here’s a reference for how this is achieved:

printf "myusername:$(openssl passwd -crypt mypassword)\n" >> /etc/nginx/htpasswd

You shouldn’t even need to reload/restart nginx afterwards.

Easily fix MySQL replication

mysql

I found that when replication breaks and the slave has corrupted, it’s best to start from scratch. I use just two commands to be able to do this properly.

The first is performed on the master and copies over the specified database ‘dbname’ below to the slave machine via SSH.


mysqldump -uroot --single-transaction --routines --triggers dbname | gzip -c | ssh user@slave_machine "cat > /directory/dbname.sql.gz"

Simultaneously, make sure you run ‘show master status’ on the master and take a note of the position of the binlog prior to starting the dump.

Then, when you’re on the slave, make sure the replication has stopped by running ‘stop slave’. Drop any existing data that you have on the db, then import the new dump into MySQL.

On the master, using the same position and information you grabbed from running ‘SHOW MASTER STATUS’ on the master, update the code block below accordingly.


RESET SLAVE;
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=12345678;

You should then run ‘SHOW MASTER STATUS’ and watch the position catch up with the master and depending how far ‘behind’ replication is, you’ll need to wait for some time for replication to fix.

Making a MySQL table readonly to a user anywhere and for one tables only

mysql

There are some ways to ensure that MySQL tables are setup for readonly users with only certain access. This short snippet will explain the best way to do this.

mysql> CREATE USER ro_user;
Query OK, 0 rows affected (0.04 sec)

mysql> SET PASSWORD FOR ro_user = PASSWORD ('onionbag');
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT SELECT ON somedb.sometable TO ro_user;
Query OK, 0 rows affected (0.00 sec)

mysql> UPDATE user SET Show_db_priv='Y' WHERE User='ro_user';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

The above will create a default entry (‘%’) for all hosts, but we also want to ensure we add one for localhosts.


insert into user values ('localhost','ro_user','Y0UR_ENCRYTPED_PASSW0RD','N','N','N','N','N','N','N','N','N','N','N','N','N','N','Y','N','N','N','N','N','N','N','N','N','N','N','','','','',0,0,0,0);

MySQL dump without stopping transactions

mysql

When I needed to take a mysql dump from one database, I needed to make sure that it wasn’t going to bring down the database and thus damage the application.
With a few MySQL options, this can be avoided:
mysqldump -uroot –single-transaction –routines –triggers databasename | gzip -c | ssh root@some-server “cat > /folder/databasename.sql.gz”

What is swappiness and how to change

linux, swappiness

In Linux it is very easy to configure the use of virtual memory used by the kernel. Swappiness controls the tendency of the linux kernel to move a process out of physical memory and start using swap. Disk is slower than RAM and thus can lead to a slower response time for the application itself, therefore swappiness controls how often processes are moved into the swap cache.

For example, if swappiness is set to 0, the kernel will avoid moving processes into the swap cache.

If swappiness is set to 100, the kernel will regularly use the swap cache.

By default, this setting is normally held at 60.

On a virtualised node, a good swappiness parameter to use is 010, that is because a VM has shared disk IO and it is important for the overall system to use less disk utilization.

How to change?

Find out how much swappiness is being used by running : cat /proc/sys/vm/swappiness
Adjust the parameter by changing/adding this parameter (vm.swappiness = 0) and save in: /etc/sysctl.conf
To get the change live without a restart or sysctl reload, you can run this: echo 0 > /proc/sys/vm/swappiness

Upgrading git

git

$ wget http://git-core.googlecode.com/files/git-1.8.3.4.tar.gz
$ wget -O git-manpages-1.8.3.4.tar.gz http://code.google.com/p/git-core/downloads/detail?name=git-manpages-1.8.3.4.tar.gz&can=2&q=
Next, install all required libraries before building GIT:
$ sudo yum install zlib-devel perl-CPAN gettext
Now let’s untar and build and install GIT in the /usr directory:
$ tar xvfz git-1.8.3.4.tar.gz
$ cd git-1.8.3.4
$ ./configure
$ make
$ sudo make prefix=/usr install
$ git –version
git version 1.8.3.4

  • See more at: http://www.unixmen.com/how-to-install-latest-git-on-rhel-6-centos-6/#sthash.SAZxuE6f.dpuf