TimeLinux1

Thursday, March 7, 2013

How to install MySQL on Linux

MySQL is a FOSS database that is available in a free community edition and a paid commercial edition from Oracle corp. Oracle acquired MySQL from Sun Microsystemes acquisition. MySQL is an integral part of LAMP Stack and is used for a majority of web applications. Infact, MySQL is a choice DB platform for many Internet Applications like Facebook, Twitter, Yahoo etc.

Its very simple to install and run MySQL on Linux using yum, as shown below.

1- Check status of MySQL prior to install:


[root@redhat2 bin]# service mysqld status
mysqld: unrecognized service
[root@redhat2 bin]#

As you see mysql doesnt exist. You can also verify using rpm.

[root@redhat2 bin]# rpm -qa | grep mysql
[root@redhat2 bin]#

2-To install:

[root@redhat2 lib]# yum install -y mysql-server
Loaded plugins: product-id, security, subscription-manager
Updating certificate-based repositories.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mysql-server.x86_64 0:5.1.52-1.el6_0.1 will be installed
--> Processing Dependency: perl-DBD-MySQL for package: mysql-server-5.1.52-1.el6_0.1.x86_64
--> Running transaction check
---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================
 Package                           Arch                      Version                              Repository                  Size
===================================================================================================================================
Installing:
 mysql-server                      x86_64                    5.1.52-1.el6_0.1                     mynew                    8.1 M
Installing for dependencies:
 perl-DBD-MySQL                    x86_64                    4.013-3.el6                          mynew                    134 k

Transaction Summary
===================================================================================================================================
Install       2 Package(s)

Total download size: 8.2 M
Installed size: 24 M
Downloading Packages:
(1/2): mysql-server-5.1.52-1.el6_0.1.x86_64.rpm                                                             | 8.1 MB     00:00  
(2/2): perl-DBD-MySQL-4.013-3.el6.x86_64.rpm                                                                | 134 kB     00:00  
-----------------------------------------------------------------------------------------------------------------------------------
Total                                                                                               48 MB/s | 8.2 MB     00:00  
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : perl-DBD-MySQL-4.013-3.el6.x86_64                                                                               1/2
  Installing : mysql-server-5.1.52-1.el6_0.1.x86_64                                                                            2/2
Installed products updated.

Installed:
  mysql-server.x86_64 0:5.1.52-1.el6_0.1                                                                                        

Dependency Installed:
  perl-DBD-MySQL.x86_64 0:4.013-3.el6                                                                                            

Complete!

3- Verify installation:

[root@redhat2 lib]# rpm -qa | grep mysql
mysql-libs-5.1.52-1.el6_0.1.x86_64
soci-mysql-3.1.0-1.el6.x86_64
mysql-5.1.52-1.el6_0.1.x86_64
mysql-server-5.1.52-1.el6_0.1.x86_64

[root@redhat2 lib]# ls /etc/init.d/mysqld 
/etc/init.d/mysqld
[root@redhat2 lib]# 

4- Post install config:

root@redhat2 lib]# /usr/bin/mysql_install_db --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h redhat2 password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!


[root@redhat2 ~]# /usr/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): *****
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y    
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

[root@redhat2 ~]#

5- Start using MySQL:

[root@redhat2 lib]# service mysqld status
mysqld (pid  10324) is running...

[root@redhat2 lib]# mysql -u root -p
Enter password: xyzxyz

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.52 Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql> use mysql;

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
23 rows in set (0.00 sec)

mysql> exit
Bye

Enjoy!


Wednesday, March 6, 2013

Heredoc to automate shell scripts

Ever wondered if there was a way to automate passing multiple bash commands to run non interactively?
For instance, if you wanted to create a shell script to start vi editor, get into insert mode and populate some commands based on some condition and then escape, save and exit out of the editor all while being in the shell script--how would you do it?

The answer is what is called heredoc. The concept of heredoc comes from C programming language and which was later borrowed into Unix/Linux tradition. Basically what a heredoc does is to accept commands from a stream starting with a start-signature and ending with the same signature. So all the commands or interactions with the shell between those start-end signatures are non-interactive.
For a more detailed discussion visit heredoc pages here and here.

An Example:

#-------------


[root@redhat2 mrinal]# cat myscript
#!/bin/bash

tfile=$1

vi $tfile << bye                      /* start vi with a file argument $tfile by user & start signature = bye*/
i                                              /* invoke 'insert' mode of vi */
echo `date`                              /* run a command */
`uname -a`                              /* run another command */
`ls -l`
^[                                            /* escape to command mode of vi, passed by Ctrl+v on keyboard */
wq                                          /* save file */
bye                                      /* end non interactive mode when sees the signature = bye */

#------------

The above shell script will open vi non-interactively and run a bunch of commands between the start-end signature, here in our case the word 'bye'. It could be anything you choose, as long the start and end signatures are the same.