Installing mysql server on Centos 7 在 Linux上安装

You should install only one MySQL version on your CentOS 7 server. If you are not sure which version to install consult the documentation of the applications you’re going to deploy on your server.

Install MySQL 8.0 on CentOS 7

At the time of writing this article, the latest version of MySQL is version 8.0. To install it on your CentOS 7 server follow the steps below:

  1. Enable the MySQL 8.0 repository with the following command:

    sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
     
  2. Install MySQL 8.0 package with yum:

    sudo yum install mysql-community-server
     

    During the installation yum may prompt you to import the MySQL GPG key. Type y and hit Enter.

Install MySQL 5.7 on CentOS 7

To install the previous stable release of MySQL, MySQL version 5.7 on a CentOS 7 server, follow the steps below:

  1. Enable the MySQL 5.7 repository with the following command:

    sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

  2. Install MySQL 5.7 package with:

    Install MySQL as any other package using yum:

    sudo yum install mysql-community-server

    Starting MySQL

    Once the installation is completed, start the MySQL service and enable it to automatically start on boot with:

    sudo systemctl enable mysqldsudo systemctl start mysqld

    We can check the MySQL service status by typing:

    sudo systemctl status mysqld

    ● mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Wed 2018-05-23 11:02:43 UTC; 14min ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
      Process: 4293 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
     Main PID: 4310 (mysqld)
       Status: "SERVER_OPERATING"
       CGroup: /system.slice/mysqld.service
               └─4310 /usr/sbin/mysqld

    Securing MySQL

    When the MySQL server is started for the first time, a temporary password is generated for the MySQL root user. You can find the password by running the following command:

    sudo grep 'temporary password' /var/log/mysqld.log
     

    The output should look something like this:

    2018-05-23T10:59:51.251159Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: q&0)V!?fjksL
     

    Make note of the password, because the next command will ask you to enter the temporary root password.

    Run the mysql_secure_installation command to improve the security of our MySQL installation:

    sudo mysql_secure_installation
     
    Securing the MySQL server deployment.
    
    Enter password for user root:
     

    After entering the temporary password you will be asked to set a new password for user root. The password needs to be at least 8-character long and to contain at least one uppercase letter, one lowercase letter, one number and one special character.

    The existing password for the user account root has expired. Please set a new password.
    
    New password:
    
    Re-enter new password:
     

    The script will also ask you to remove the anonymous user, restrict root user access to the local machine and remove the test database. You should answer “Y” (yes) to all questions.

    Connecting to MySQL from the command line

    To interact with MySQL through the terminal we will use the MySQL client which is installed as a dependency of the MySQL server package.

    To log in to the MySQL server as the root user type:

    mysql -u root -p
     

    You will be prompted to enter the root password you have previously set when the mysql_secure_installation script was run.

    Once you enter the password you will be presented with the mysql shell as shown below:

    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 11
    Server version: 8.0.11 MySQL Community Server - GPL
    
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
     

    Create a Database

    Once you are connected to the MySQL shell, you can create a new database by typing the following command:

    CREATE DATABASE new_database;
     
    Query OK, 1 row affected (0.00 sec)
     

    Create Tables

    Now that we created a database we can create a table to store some data.

    Before running the SQL statements for creating a table we need to connect to the database:

    use new_database;
     

    In this example we will create a simple table named contacts with three fields, idnameand email:

    CREATE TABLE contacts (
      id INT PRIMARY KEY, name VARCHAR(30), email VARCHAR(30) );
     
    Query OK, 1 row affected (0.00 sec)
     

    Conclusion

    In this tutorial, we’ve shown you how to install and secure a MySQL server on a CentOS 7 server. We have also shown you how to connect to the MySQL shell and how to create a new database and table.

     

转载于:https://www.cnblogs.com/sundy818/p/10059547.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值