MySQL Review on the known Features

Embed Size (px)

Citation preview

  • 8/7/2019 MySQL Review on the known Features

    1/31

    1Copyright HP Education

    MySQL Review on the known

    Features

    Ramkumar Lakshminarayanan

  • 8/7/2019 MySQL Review on the known Features

    2/31

    2Copyright HP Education

    Starting the mysql

    On Unix systems, the MySQL server binary is a filecalled mysqld.

    To start mysql using the terminal window type as

    following Service mysqld start

    To stop mysql using the terminal window type asfollowing Service mysqld stop

    To restart mysql using the terminal window type asfollowing Service mysqld restart

  • 8/7/2019 MySQL Review on the known Features

    3/31

    3Copyright HP Education

    MySQL RPM Packages

    MySQL-client MySQL client package including the mysql command-line

    tool.

    MySQL-debuginfo

    Used for debugging problems with both the client and serverprograms. Can be used to generate extra information withMySQL Server crashes.

    MySQL-devel

    The libraries needed to compile additional MySQL clients. MySQL-embedded

    The MySQL embedded server. You only need this packageif you are creating an application that has MySQL embeddedin it.

  • 8/7/2019 MySQL Review on the known Features

    4/31

    4Copyright HP Education

    MySQL RPM Packages

    MySQL-server The MySQL Server files, including the mysqld binary. This is

    required to run a MySQL Server.

    MySQL-shared Shared libraries used by various applications and languages

    to communicate with MySQL.

    MySQL-shared-compat

    This package is a replacement for MySQL-shared if yourapplication requires libraries from older versions of MySQLbut you need to upgrade mysqld to a newer version.

    MySQL-test The MySQL test suite.

    MySQL-VERSION. PLATFORM.src.rpm Source code for all the packages.

  • 8/7/2019 MySQL Review on the known Features

    5/31

    5Copyright HP Education

    To find the RPM Package installed for MySQL

    Executing rpm -qa will list all rpm files installed onyour system. To see if you currently have any mysqlpackages installed:shell> rpm -qa | grep -i mysql

    MySQL-server-6.0.8-0.glibc23

    MySQL-shared-6.0.8-0.glibc23

    MySQL-client-6.0.8-0.glibc23

    perl-DateTime-Format-MySQL-0.04-1.el5.rf

    MySQL-devel-6.0.8-0.glibc23

    perl-DBD-MySQL-3.0007-1.fc6

    In this example, four MySQL packages and two Perllibraries are installed. The Perl libraries are third-party packages for being able to connect Perl withMySQL, and are not actually a part of a MySQLinstallation.

  • 8/7/2019 MySQL Review on the known Features

    6/31

    6Copyright HP Education

    RPM Installation Commands

    To install an rpm package, run the command rpm -ifile.rpm. You can list multiple packages separated byspaces and rpm will install them at the same time.

    The v option gives more verbose output, which isuseful in case anything goes wrong.

    The h option shows installation progress using hash(#) marks.

  • 8/7/2019 MySQL Review on the known Features

    7/31

    7Copyright HP Education

    MySQL Installation with RPM

    For a basic setup of the MySQL Server, install theserver and client rpms using the rpm command withthe ivh options:

    shell> rpm -ivh MySQL-server-VERSION.PLATFORM-PROCESSOR.rpm MySQL-client-VERSION.PLATFORM-PROCESSOR.rpm

    The server rpm places the data files in the data directory,which is /var/lib/mysql by default.

    The pid file for the mysqld daemon and the error logs arelocated in the data directory.

  • 8/7/2019 MySQL Review on the known Features

    8/31

    8Copyright HP Education

    A database is simply a directory in the data directory,so you will see directories corresponding to the mysqland test databases that the rpm creates.

    [root@localhost ~]# cd /

    [root@localhost /]# cd var

    [root@localhost var]# cd lib

    [root@localhost lib]# cd mysql

    [root@localhost mysql]# ls

    bank ib_logfile0 my_database mysql.sock std twogov ib_logfile1 myexample one test

    wiproadmin ibdata1 localhost.localdomain.err mysql sakilathree

    [root@localhost mysql]#

  • 8/7/2019 MySQL Review on the known Features

    9/31

    9Copyright HP Education

    Start of MySQL

    A startup script is created in /etc/init.d that can beused by the database administrator to manually start,stop, and restart the server.

    In addition, links are used from this script to the/etc/rc.d/ directory structure where scripts are used tostart up and shut down server programs such asmysqld when the operating system is started or shutdown. After the installation is finished, mysqld will

    start.

  • 8/7/2019 MySQL Review on the known Features

    10/31

    10Copyright HP Education

    Once the installation is complete you should be ableto connect to the server to see if everything worked.The MySQL-client rpm includes the mysql command-line client so you can test your configuration by

    running the following from a command prompt:[root@localhost /]# mysql -u root

    Welcome to the MySQL monitor. Commands end with

    ; or \g.

    Your MySQL connection id is 5Server version: 5.1.45 Source distribution

    Type 'help;' or '\h' for help. Type '\c' to clear

    the current input statement.

    mysql>

  • 8/7/2019 MySQL Review on the known Features

    11/31

    11Copyright HP Education

    Understanding the Error Message

    If an error message is returned it will be helpful tocheck the error log, which is by default in the datadirectory at /var/lib/mysql. The filename is of theformat hostname-err.log

  • 8/7/2019 MySQL Review on the known Features

    12/31

    12Copyright HP Education

    Initial Configuration

    On Unix-based servers the configuration file is calledmy.cnf. On Windows servers it can be either my.cnfor my.ini. On startup, mysqld looks for thisconfiguration file in several locations. This is done in

    a specific order. /etc/my.cnf

    /etc/mysql/my.cnf

    $MYSQL_HOME/my.cnf

    /path/to/file when defaults-extra-file=/path/to/file isspecified

    /.my.cnf

  • 8/7/2019 MySQL Review on the known Features

    13/31

    13Copyright HP Education

    $MYSQL_HOME refers to an operating system userenvironment variable. If it is not set, MySQLprograms will set it to be the same as the basedirectory (basedir) by default, unless there is a my.cnf

    file in the data directory (datadir), in which case thedata directory will be used.

  • 8/7/2019 MySQL Review on the known Features

    14/31

  • 8/7/2019 MySQL Review on the known Features

    15/31

  • 8/7/2019 MySQL Review on the known Features

    16/31

    16Copyright HP Education

    Unix configuration file

    After your installation of the server packaging iscomplete you need to either use a previously createdmy.cnf file or use one of the configuration examplefiles. With both the pkg and the archive packaging

    these examples are in the support-files directory(located under the basedir directory). With the officialrpms these same files are placed under the

    /usr/share/mysql directory.

  • 8/7/2019 MySQL Review on the known Features

    17/31

    17Copyright HP Education

    Post Installation

    With Unix-based installations, all initial accounts arecreated without passwords. This includes theaccounts with a username of root that are allowed toexecute any command on the server.

    This is a large security hole and should be resolvedas soon as possible.

  • 8/7/2019 MySQL Review on the known Features

    18/31

    18Copyright HP Education

    Install_db Script

    ./usr/bin/mysql_install_db If you are not able to find the mysql_install_db search for the

    file as follows

    find -name "mysql_install_db"

  • 8/7/2019 MySQL Review on the known Features

    19/31

    19Copyright HP Education

    Root user password assignment

    In Unix-based servers a total of five users arecreated by the mysql_install_db script. There arethree root accounts: [email protected],

    root@localhost, and the root@hostname user.

    In addition, two anonymous user accounts are created.

    The first account is @localhost and

    the second account has the empty string as theusername and the server hostname for the host.

  • 8/7/2019 MySQL Review on the known Features

    20/31

  • 8/7/2019 MySQL Review on the known Features

    21/31

    21Copyright HP Education

    Root user password assignment

    Another way to change passwords for accountshaving a username of root is to manually configurethese passwords. You have two methods of doingthis manually:

    executing the SET PASSWORD command

    or executing the appropriate UPDATE statementfollowed by a FLUSH PRIVILEGES statement.

  • 8/7/2019 MySQL Review on the known Features

    22/31

    22Copyright HP Education

    To assign passwords to users other than yourselfusing the SET PASSWORD command, you mustconnect to the server as a user with SUPERprivileges.

    Initially, the users with a username of root have theSUPER privilege. Connect to the server by issuingthe following:

    shell> mysql -u root

  • 8/7/2019 MySQL Review on the known Features

    23/31

    23Copyright HP Education

    Then, issue the following SET PASSWORDstatement

    the PASSWORD() function encrypts the

    password appropriately, and must be used:

    mysql> SET PASSWORD FOR root@localhost =

    PASSWORD(new password);

  • 8/7/2019 MySQL Review on the known Features

    24/31

    24Copyright HP Education

    On Unix-based servers, there is an additional userwith a username of root to set a password for:

    mysql> SET PASSWORD FOR root@host_name =PASSWORD(new_password);

    The value host_name should be set to the name ofthe server host.

  • 8/7/2019 MySQL Review on the known Features

    25/31

    25Copyright HP Education

    Drop the root@% user because it is a security risk.The % host allows someone from any host toattempt to gain access to your server.

    The root@% user allows someone from any host topossibly gain SUPER privileges on your server.

    To remove this user log in as root@localhost andexecute:

    DROP USER root@% command.

  • 8/7/2019 MySQL Review on the known Features

    26/31

    26Copyright HP Education

    A final method of configuring your root passwords isusing the UPDATE statement to update the usertable directly.

    The following statement updates all accounts with a

    username of root to the same password:

    mysql> UPDATE mysql.user SET Password =PASSWORD(new_password) WHERE User = root;

    mysql> FLUSH PRIVILEGES;

  • 8/7/2019 MySQL Review on the known Features

    27/31

  • 8/7/2019 MySQL Review on the known Features

    28/31

    28Copyright HP Education

    Anonymous Users

    Anonymous users are user accounts with ausername of an empty string (). It is a best practiceto drop the anonymous users because there is veryrarely any valid reason to retain these accounts.

    To ensure that you have anonymous users, run thefollowing query:

    mysql> SELECT user,host,password FROM mysql.userWHERE user=;

  • 8/7/2019 MySQL Review on the known Features

    29/31

    29Copyright HP Education

    The easiest method to remove them on any operatingsystem is just log in with a root user account andissue the command:

    mysql> DROP USER @localhost;

    Query OK, 0 rows affected (0.00 sec)

    On Unix machines, there is also a @host_nameuser to drop:

    mysql> DROP USER @host_name;

    Query OK, 0 rows affected (0.00 sec)

    If you receive the following error, it means that youtried to drop a user that did not exist:

    ERROR 1396 (HY000): Operation DROP USER failed for

    @wrong host_name

  • 8/7/2019 MySQL Review on the known Features

    30/31

    30Copyright HP Education

    Securing Your System

    With Unix-based systems you have the option ofrunning the mysql_secure_installation script toinitialize passwords and perform other security-related tasks.

    Unlike the mysql_install_db script, themysql_secure_installation script requires the

    server to be running

    Remember the five users the Unix-based systemscreate when running the mysql_install_db script.

    After running the mysql_secure_installation scriptthere is one user left who has a password.

  • 8/7/2019 MySQL Review on the known Features

    31/31

    31Copyright HP Education

    What we have seen :

    Installing MySQL

    GNU/Linux

    Post-install configuration

    Initializing the data directory Initializing the system tables

    Setting user passwords

    Getting rid of unsecure users

    How to start and stop mysqld automatically