Saturday, February 14, 2015

Mapping CRUD routes manually

So, in route.rb, adding this CRUD route:

resources: employees

Generates these routes:
$rake routes

         employees  GET       /employees(.:format)              employees#index
                            POST     /employees(.:format)              employees#create
  new_employee GET        /employees/new(.:format)      employees#new
   edit_employee GET        /employees/:id/edit(.:format) employees#edit
           employee GET        /employees/:id(.:format)        employees#show
                           PATCH   /employees/:id(.:format)        employees#update
                           PUT         /employees/:id(.:format)        employees#update

                           DELETE /employees/:id(.:format)        employees#destroy

To enter these manually, add:
get   'employees'              => 'employees#index'
post    'employees'              => 'employees#create'
get      'employees/new'      => 'employees#new',    as: :new_employee
get      'employees/:id/edit' => 'employees#edit',   as: :edit_employee
get      'employees/:id'        => 'employees#show',   as: :employee
patch  'employees/:id'        => 'employees#update'
put      'employees/:id'       => 'employees#update'
delete 'employees/:id'        => 'employees#destroy'

For more information see, http://guides.rubyonrails.org/routing.html

Saturday, August 16, 2014

Simple bash script for getting the current, average and maximum status value in MySQL

Here's a script to get the current, average and maximum value of a status counter in MySQL. This is useful when you need to monitor a certain counter which you will use as a threshold for running administrative scripts such as pt-online-schema-change, pt-table-checksum, pt-stalk, etc:

counters.sh
#!/bin/bash

INTERVAL=1
USER=root
PASSWORD=msandbox
HOST=127.0.0.1
VARIABLE=Threads_running
MAX=1
AVG=1
COUNT=0
SUM=0
while [ 1 ]
do
  CURRENT=`mysql -h$HOST -u$USER -p$PASSWORD -BNe "SHOW GLOBAL STATUS LIKE '$VARIABLE'"|tr "\t" " "|cut -d " " -f2`
  if [ $CURRENT -gt $MAX ]; then
    MAX=$CURRENT
  fi
  SUM=`expr $SUM + $CURRENT`
  COUNT=`expr $COUNT + 1`
  AVG=`expr $SUM / $COUNT`
  echo -e "$VARIABLE: CUR=$CURRENT MAX=$MAX AVG=$AVG" 
  sleep $INTERVAL
done


Example:
./counters.sh 
Threads_running: CUR=1 MAX=1 AVG=1
Threads_running: CUR=1 MAX=1 AVG=1
Threads_running: CUR=1 MAX=1 AVG=1
Threads_running: CUR=1 MAX=1 AVG=1
Threads_running: CUR=137 MAX=137 AVG=28
Threads_running: CUR=140 MAX=140 AVG=46
Threads_running: CUR=140 MAX=140 AVG=60
Threads_running: CUR=136 MAX=140 AVG=69
Threads_running: CUR=125 MAX=140 AVG=75

Threads_running: CUR=144 MAX=144 AVG=82

Tuesday, June 17, 2014

Useful 1 liner perl script(s)

I'll be updating this article for every time I use a one-liner perl code I need to use at work.

1.  perl -n -e 'chomp;print $_ . " "';

If I need to concatenate input with spaces and I need to display it.
Example: 

#rpm -qa|grep rpm|perl -n -e 'chomp;print $_ . " "';
rpm-libs-4.8.0-37.el6.x86_64 rpm-4.8.0-37.el6.x86_64 rpm-python-4.8.0-37.el6.x86_64

#rpm -qa|grep perl|perl -n -e 'chomp;print $_ . " "' | xargs echo "yum install";
yum install perl-Pod-Escapes-1.04-136.el6.x86_64 perl-libs-5.10.1-136.el6.x86_64 perl-Module-Pluggable-3.90-136.el6.x86_64 perl-DBI-1.609-4.el6.x86_64 perl-Net-LibIDN-0.12-3.el6.x86_64 perl-Net-SSLeay-1.35-9.el6.x86_64 perl-Time-HiRes-1.9721-136.el6.x86_64 perl-Pod-Simple-3.13-136.el6.x86_64 perl-version-0.77-136.el6.x86_64 perl-5.10.1-136.el6.x86_64 perl-DBD-MySQL-4.013-3.el6.x86_64 perl-IO-Socket-SSL-1.31-2.el6.noarch

Thursday, December 19, 2013

Installing MySQL 5.5 on Fedora 20

If you tried installing MySQL 5.5 on Fedora 20 with "yum install mysql mysql-server", you're in for a big surprise because these packages default to MariaDB now:

[root@localhost ~]# yum install mysql mysql-server
Failed to set locale, defaulting to C
Loaded plugins: langpacks, refresh-packagekit
Resolving Dependencies
--> Running transaction check
---> Package mariadb.i686 1:5.5.34-2.fc20 will be installed
--> Processing Dependency: mariadb-libs(x86-32) = 1:5.5.34-2.fc20 for package: 1:mariadb-5.5.34-2.fc20.i686
---> Package mariadb-server.i686 1:5.5.34-2.fc20 will be installed
--> Running transaction check
---> Package mariadb-libs.i686 1:5.5.34-2.fc20 will be installed
--> Finished Dependency Resolution

Suppose you really want to install the MySQL community version, it's still available but under new package names community-mysql-server and community-mysql. So if you want to install these instead, run:

# yum install community-mysql-server community-mysql-libs community-mysql

To ensure it runs upon boot:
# systemctl enable mysqld

To start MySQL, run:
# systemctl start mysqld

To stop MySQL, run:
# systemctl stop mysqld

Sunday, February 17, 2013

Troubleshooting MySQL user permissions

SELECT user(), current_user();
user() -> connection info of user
current_user() -> user that will be compared with privileges

SELECT user, host FROM mysql.user WHERE user='<the_user>';

Friday, July 27, 2012

Installing MySQL Sandbox on CentOS 6

MySQL sandbox is used to easily deploy MySQL instances for testing.  You can easily setup a single node, master-slave nodes, master-master nodes with one command.

More information on MySQL Sandbox is listed here:  http://search.cpan.org/~gmax/MySQL-Sandbox-3.0.25/lib/MySQL/Sandbox.pm

To install MySQL sandbox, you will need compiler tools and MySQL dependencies.  Please refer to

1.  Install cpan 
yum install cpan

2.  Install build dependencies
yum install perl-Test-Simple

3.  Install MySQL sandbox
cpan MySQL::Sandbox

4.  Test if Sandbox is installed:
make_sandbox
MySQL Sandbox should not run as root

If you know what you are doing and want to
 run as root nonetheless, please set the environment
variable 'SANDBOX_AS_ROOT' to a nonzero value

5.  Test is MySQL Sandbox can create one MySQL instance:
su - <regular user>
wget http://www.percona.com/redir/downloads/Percona-Server-5.5/Percona-Server-5.5.25a-27.1/binary/linux/x86_64/Percona-Server-5.5.25a-rel27.1-277.Linux.x86_64.tar.gz
mysql_sandbox Percona-Server-5.5.25a-rel27.1-277.Linux.x86_64.tar.gz
mysql -umsandbox -pmsandbox --host=127.0.0.1 --port=5525


Installing sysbench from launchpad source on CentOS 6

Sysbench is used to benchmark servers on MySQL performance.  Though sysbench is available in official repositories, the version available on launchpad is capable of benchmarking load on multiple tables.  Since there's no package for it, we have to build it ourselves.  Here's how:

1.  Install compiler tools to compile the source 
yum -y install gcc gcc-c++ autoconf automake make libtool

2.  Install bzr so you download the source from the repository
yum -y install bzr

3.  Install MySQL dependencies to build sysbench:
yum -y install mysql mysql-server mysql-devel

4.  Download sysbench
bzr branch lp:sysbench

5.  Compile and install sysbench
cd sysbench
./autogen.sh
./configure --prefix=/usr --mandir=/usr/share/man
make
make install

6.  Copy lua templates
mkdir /usr/share/sysbench/tests/db -p
cp tests/db/* /usr/share/sysbench/tests/db

7.  Test if sysbench is working

sysbench
Missing required command argument.
Usage:
  sysbench [general-options]... --test=<test-name> [test-options]... command