官术网_书友最值得收藏!

Installing relational databases with MySQL

In this recipe, we will learn how to install and configure the MySQL database on an Ubuntu server.

Getting ready

You will need access to a root account or an account with sudo privileges.

Make sure that the MySQL default port 3306 is available and not blocked by any firewall.

How to do it…

Follow these steps to install the relational database MySQL:

  1. To install the MySQL server, use the following command:
    $ sudo apt-get update
    $ sudo apt-get install mysql-server-5.7
    

    The installation process will download the necessary packages and then prompt you to enter a password for the MySQL root account. Choose a strong password:

  2. Once the installation process is complete, you can check the server status with the following command. It should return an output similar to the following:
    $ sudo service mysql status
    mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service
     Active: active (running) since Tue 2016-05-10 05:
    
  3. Next, create a copy of the original configuration file:
    $ cd /etc/mysql/mysql.conf.d
    $ sudo cp mysqld.cnf mysqld.cnf.bkp
    
  4. Set MySQL to listen for a connection from network hosts. Open the configuration file /etc/mysql/mysql.conf.d/mysqld.cnf and change bind-address under the [mysqld] section to your server’s IP address:
    $ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
    bind-address = 10.0.2.6
    

    Note

    For MySQL 5.5 and 5.6, the configuration file can be found at /etc/mysql/my.cnf

  5. Optionally, you can change the default port used by the MySQL server. Find the [mysqld] section in the configuration file and change the value of the port variable as follows:
    port = 30356
    

    Make sure that the selected port is available and open under firewall.

  6. Save the changes to the configuration file and restart the MySQL server:
    $ sudo service mysql restart
    
  7. Now open a connection to the server using the MySQL client. Enter the password when prompted:
    $ mysql -u root -p
    
  8. To get a list of available commands, type \h:
    mysql> \h
    

How it works…

MySQL is a default database server available in Ubuntu. If you are installing the Ubuntu server, you can choose MySQL to be installed by default as part of the LAMP stack. In this recipe, we have installed the latest production release of MySQL (5.7) from the Ubuntu package repository. Ubuntu 16.04 contains MySQL 5.7, whereas Ubuntu 14.04 defaults to MySQL version 5.5.

If you prefer to use an older version on Ubuntu 16, then use following command:

$ sudo add-apt-repository ‘deb http://archive.ubuntu.com/ubuntu trusty universe’
$ sudo apt-get update
$ sudo apt-get install mysql-server-5.6

After installation, configure the MySQL server to listen for connections from external hosts. Make sure that you open your database installation to trusted networks such as your private network. Making it available on the Internet will open your database to attackers.

There’s more…

Securing MySQL installation

MySQL provides a simple script to configure basic settings related to security. Execute this script before using your server in production:

$ mysql_secure_installation

This command will start a basic security check, starting with changing the root password. If you have not set a strong password for the root account, you can do it now. Other settings include disabling remote access to the root account and removing anonymous users and unused databases.

MySQL is popularly used with PHP. You can easily install PHP drivers for MySQL with the following command:

$ sudo apt-get install php7.0-mysql

See also

主站蜘蛛池模板: 上犹县| 黄龙县| 商丘市| 丁青县| 巩留县| 甘洛县| 东辽县| 徐州市| 资阳市| 公主岭市| 博罗县| 舒城县| 新泰市| 民勤县| 赣榆县| 灵山县| 榆林市| 北海市| 壶关县| 固原市| 沛县| 饶河县| 定州市| 阿城市| 涡阳县| 北宁市| 辉县市| 内江市| 汉源县| 江孜县| 中宁县| 日喀则市| 井陉县| 清河县| 景洪市| 安多县| 盈江县| 安平县| 广西| 金塔县| 湘潭市|