Install MySql Server on CentOS Create Database and Restore from Dump

  1. Install MySQL: yum install mysql-server mysql php-mysql
  2. Start MySql:
    sudo /sbin/service mysqld start
  3. Set Password: Initially MySql has no possword in root so you have to set it-[root@localhost /]# mysql -uroot -p
    Enter password:  (press enter for blank password) after login exit command
    Now Set the password: [root@localhost /]# /usr/bin/mysqladmin -u root password ‘iict123’
  4. Login to MySql: [root@localhost /]# mysql -uroot -p
    Enter password:
    SELECT User, Host, Password FROM mysql.user;
  5. Create Database: (after login to mysql) mysql>create database admissio_kuet;
  6. Restore from Command line: [root@localhost /]# cd /home/IICT/
    [root@localhost IICT]# ls
    backupfile.sql Desktop Documents Downloads Music Pictures Public Templates Videos
    [root@localhost IICT]# mysql -uroot -p –one-database admissio_kuet < backupfile.sql
    Enter password: (enter mysql password)
    [root@localhost IICT]# (restore completed)
  7. Set MySql running on Restart Machine:

    Launch at restart:

    sudo chkconfig mysqld on

    Installing MySQL Server on CentOS

    MySQL is an open-source relational database. For those unfamiliar with these terms, a database is where an application keeps its data, and relational refers to how the data is organized and accessed within the database. SQL refers to the language used by application queries to retrieve and store data: Structured Query Language.

    MySQL is free and widely used, meaning that you can find a large amount of application support, tools, and community help for it. MySQL is a safe choice if you know that you need a database but don’t know much about all of the available the options.

    This article describes a basic installation of a MySQL database server on CentOS Linux, just enough to get you started. Remember that you might need to install other packages to let applications use MySQL, like extensions for PHP. Check your application documentation for details.

    Install MySQL

    Install the MySQL server through the CentOS package manager by running the following commands at a command prompt:

    sudo yum install mysql-server sudo /sbin/service mysqld start
    

    Then, run the following command:

    sudo /usr/bin/mysql_secure_installation
    

    Press enter to give no password for root when that program asks for it. To apply some reasonable security to your new MySQL server answer “yes” to all the questions that the program asks. In order, those questions enable you set the root password, remove anonymous users, disable remote root logins, delete the test database that the installer included, and then reload the privileges so that your changes will take effect.

    Allow access from other machines

    If you have iptables enabled and want to connect to the MySQL database from another machine, you need to open a port in your server’s firewall (the default port is 3306). You don’t need to do this if the application using MySQL is running on the same machine.

    If you do need to open a port, you can use the following rules in iptables to open port 3306:

    I INPUT -p tcp --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT -I OUTPUT -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT
    

    NOTE: The iptables command was deliberately left out of the iptables rules in the instructions below. Some people using distributions that do not have their own iptables service might instead have a rules file they can import using iptables-restore. The format of the lines in that file would be similar to the format listed below, iptables options with the iptables command in front of them. For this reason, the instructions in this article represent a compromise. It is easy to paste the lines into a rules file, and they can be used with the iptables command instead.

    Launch MySQL

    Now that MySQL is installed, you can verify that it’s running by trying to launch it:

    sudo /sbin/service mysqld start
    

    If MySQL is already running, you receive a message stating that.

    Launch at restart

    To ensure that the MySQL server will launch when the machine is restarted, run the following command:

    sudo chkconfig mysqld on
    

    That makes sure your machine will launch the MySQL server when it reboots.

    The mysql shell

    There is more than one way to work with a MySQL server, but this article focuses on the most basic and compatible approach: The mysql shell. At the command prompt, run the following command to launch the mysql shell and enter it as the root user:

    /usr/bin/mysql -u root -p
    

    When you’re prompted for a password, enter the one that you set at installation or, if you haven’t set one, just press enter to submit no password. The following mysql shell prompt should appear:

    mysql&gt;
    

    Set the root password

    Since you have just installed your MySQL database serve, the root account within MySQL has no password set yet. You should change that by running the following commands:

    /usr/bin/mysqladmin -u root password 'new-password'
    /usr/bin/mysqladmin -u root --password='new-password'  -h
    hostname-of-your-server 'new-password'
    

    NOTE: This article shows SQL commands in all capitals, but you can also type them in lowercase. The commands are shown capitalized by convention, to make them stand out from field names and other data that’s being manipulated.

    Find database users

    As mentioned in the preceding section, MySQL stores the user information in its own database. The name of the database is “mysql”. Inside that database, the user information is in a “table”, a dataset, named “User”. If you want to see what users are set up in MySQL table, or dataset, named “user”.

    SELECT User, Host, Password FROM mysql.user;
    

    Following are descriptions of the parts of that command:

    • The SELECT command tells MySQL that you are asking for data.
    • The User, Host, Password part tells MySQL what fields you want it to look in. Fields are categories for the data in a table. In this case, you are looking for the username, the host associated with the username, and the encrypted password entry.
    • The FROM mysql.user part of the command tells MySQL to get the data from the mysql database and the user table.
    • The command ends with a semicolon.

    Ending SQL Queries with a Semicolon

    All SQL queries end in a semicolon. MySQL does not process a query until you type a semicolon.

    This means that you can break up queries onto multiple lines to make them easier to read. For example, the preceding command also works if you enter it on multiple lines in the mysql shell, as follows:

    &gt;mysql&gt; SELECT User, Host, Password -&gt; FROM mysql.user;
    

    When you press enter after the Password part, you get a new line, so you can keep typing. The -> symbol indicates that you are still in the middle of a statement. You can type a semicolon by itself to end a command if you forget to type it on the same line as the command.

    User hosts

    Following is example output for the preceding query:

    SELECT User, Host, Password FROM mysql.user;
    +------------------+-----------+-------------------------------------------+
    | User | Host | Password                                                   | 
    +------------------+-----------+-------------------------------------------+
    | root | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19             |
    | root | demohost  | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19             |
    | root | 127.0.0.1 | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19             |
    |      | %         |                                                       |
    +------------------+-----------+-------------------------------------------+
    

    Users are associated with a host, specifically the host to which they connect. The “root” user in this example is defined for localhost, for the IP address of localhost, and the hostname of the server (“demohost” in this example). You usually need to set a user for only one host, the one from which you typically connect.

    If you’re running your application on the same machine as the MySQL server the host it connects to by default is “localhost”. Any new users that you create must have “localhost” in their “host” field.

    If your application connects remotely, the “host” entry that MySQL looks for is the IP address or DNS hostname of the remote machine (the one from which the client is coming).

    A special value for the host is %, as you can see in the preceding output for the blank, or anonymous, user (see the following section). The % symbol is a wildcard that applies to any host value. You usually don’t want to use that because it’s more secure to limit access specifically to trusted hosts.

    Anonymous users

    In the example output, one entry has a host value but no username or password. That’s an “anonymous user”. When a client connects with no username specified, it’s trying to connect as an anonymous user.

    You usually don’t want any anonymous users, but some MySQL installations include one by default. If you see one, you should either delete the user (refer to the username with empty quotes, like ”) or set a password for it. Both tasks are covered later in this series of articles.

    Create a database

    There is a difference between database server and an actual database, even though those terms are often used interchangeably. MySQL is a database server, meaning that it keeps track of databases and controls access to them. An actual database is where all the data goes is stored, and it is the database that applications are trying to access when they interact with MySQL.

    Some applications create a database as part of their setup process, but others require you to create a database and tell the application about it. Fortunately, creating a database is simple.

    To create a database, log in to the mysql shell and run the following command, replacing demodb with the name of the database that you want to create:

    CREATE DATABASE demodb;
    

    The database is created. You can verify its creation by running a query to list all databases. The following example shows the query and example output:

    SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | demodb             |
    | mysql              |
    +--------------------+
    3 rows in set (0.00 sec)
    

    Add a database user

    When applications connect to the database using the root user, they usually have more privileges than they need. You can create a new user that applications can use to connect to the new database. In the following example, a user named demouser is created.

    To create a new user, run the following command in the mysql shell:

    CREATE USER 'demouser'@'localhost' IDENTIFIED BY 'demopassword';
    

    You can verify that the user was created by running that “SELECT” query again:

    SELECT User, Host, Password FROM mysql.user;
    +------------------+-----------+-------------------------------------------+
    | User | Host | Password                                                   |
    +------------------+-----------+-------------------------------------------+
    | root     | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19         |
    | root     | demohost  | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19         |
    | root     | 127.0.0.1 | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19         |
    | demouser | localhost | *0756A562377EDF6ED3AC45A00B356AAE6D3C6BB6         |
    +------------------+-----------+-------------------------------------------+
    

    Grant database user permissions

    Right after you create a new user, it has no privileges. The user can be used to log in to MySQL, but it can’t be used to make any database changes. Give the user full permissions for your new database by running the following commmand:

    GRANT ALL PRIVILEGES ON demodb.* to demouser@localhost;
    

    Then, flush the privileges to make the change take effect.

    FLUSH PRIVILEGES;
    

    To verify that the privileges were set, run the following command:

    SHOW GRANTS FOR 'demouser'@'localhost';
    

    MySQL returns the commands needed to reproduce that user’s permissions if you were to rebuild the server. The “USAGE on .” part basically means that the user gets no privileges on anything by default. That command is overridden by the second command, which is the grant you ran for the new database.

    +-----------------------------------------------------------------------------------------------------------------+
    | Grants for demouser@localhost                                                                                   |
    +-----------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'demouser'@'localhost' IDENTIFIED BY PASSWORD '*0756A562377EDF6ED3AC45A00B356AAE6D3C6BB6' |
    | GRANT ALL PRIVILEGES ON `demodb`.* TO 'demouser'@'localhost'                                                    |
    +-----------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.00 sec)
    

    Revoking privileges

    Sometimes you might need to revoke (remove) privilges form a user, for different reason. For example: you were granting ALL privileges to ‘demouser’@’localhost’, but by accident (can happen to the best of us any time!) instead of granting them only on the demodb database, you granted them to all other databases too:

    +-----------------------------------------------------------------------------------------------------------------+
    | Grants for demouser@localhost                                                                                   |
    +-----------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'demouser'@'localhost' IDENTIFIED BY PASSWORD '*0756A562377EDF6ED3AC45A00B356AAE6D3C6BB6' |
    | GRANT ALL PRIVILEGES ON *.* TO 'demouser'@'localhost'                                                           |
    +-----------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.00 sec)
    

    After realising your mistake, you decided to do something to correct it. The easiet way is to use REVOKE statement, followed by GRANt statement to apply correct privileges.

    REVOKE ALL ON *.* FROM demouser@localhost;
    GRANT ALL PRIVILEGES ON demodb.* to demouser@localhost;
    SHOW GRANTS FOR 'demouser'@'localhost';
    
    +-----------------------------------------------------------------------------------------------------------------+
    | Grants for demouser@localhost                                                                                   |
    +-----------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'demouser'@'localhost' IDENTIFIED BY PASSWORD '*0756A562377EDF6ED3AC45A00B356AAE6D3C6BB6' |
    | GRANT ALL PRIVILEGES ON *.* TO 'demouser'@'localhost'                                                           |
    +-----------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.00 sec)
    

    Now your user has correct permission, and therefore your database server is slightly more secure. (Granting privileges like: ALL on . is deemed as a very bad practice). You should also read official MySQL documentation regarding possible privilege choices, to grant only those privileges truly needed, rather than using ALL.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s