Home > Database > Mysql Tutorial > body text

How to install MySQL database in Linux environment

WBOY
Release: 2023-05-31 10:46:21
forward
2727 people have browsed it

    1. Overview

    The advantage of MySQL binary installation is that it can be installed in any path and has good flexibility. It can also be installed on one server Multiple MySQL instances. The disadvantage of this method is that it is compiled, so the performance is not as good as the version compiled from source code, and the compilation parameters cannot be flexibly customized. If the user does not want to install the simplest but inflexible RPM package, nor does he want to install a complex and time-consuming source code package, then the compiled binary package will be the best choice.

    2. Preparation

    2.1 Download the mysql binary installation package

    mysql official website: https://dev.mysql.com/downloads/mysql/

    How to install MySQL database in Linux environment

    Because I downloaded the mysql-8.0.23-linux-glibc2.12-x86_64.tar version, if there is the latest version, just download the latest version.

    2.2 Import the mysql binary installation package

    Transfer the installation package to the linux system package directory through the Xftp tool:

    How to install MySQL database in Linux environment

    ##3.mysql Deployment

    Extract the mysql installation package

    --切换到安装目录
    cd /app
    --解压xz压缩文件
    tar -xvf /app/package/mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
    Copy after login

    Modify the mysql folder name

    --把mysql-8.0.23-linux-glibc2.12-x86_64修改为mysql文件夹名称
    mv /app/mysql-8.0.23-linux-glibc2.12-x86_64 /app/mysql
    Copy after login

    Create the data directory

    --在mysql根目录下创建data目录,用于存放数据
    mkdir /app/mysql/data
    Copy after login

    Create the mysql user group and mysql user

    --创建mysql用户组和mysql用户
    groupadd mysql
    useradd -g mysql mysql
    Copy after login

    Modify mysql directory permissions

    --修改mysql目录权限
    chown -R mysql.mysql /app/mysql/
    Copy after login

    Initialize database

    --先切换到mysql安装目录
    cd /app/mysql
    --初始化数据库
    bin/mysqld --initialize --user=mysql --basedir=/app/mysql --datadir=/app/mysql/data
    Copy after login

    How to install MySQL database in Linux environment

    Configure mysql

    --先切换到mysql.support-files目录
    cd /app/mysql/support-files
    --在mysql/support-files创建文件my-default.cnf
    touch my-default.cnf
    --复制配置文件到/etc/my.cnf
    cp -a ./my-default.cnf /etc/my.cnf
    --编辑my.cnf
    vim /etc/my.cnf
    Copy after login

    my.cnf Enter the following configuration content:

    [client]
    port=3306
    socket=/tmp/mysql.sock
    
    [mysqld]
    port=3306
    user=mysql
    socket=/tmp/mysql.sock
    basedir=/app/mysql
    datadir=/app/mysql/data
    Copy after login

    Configure environment variables

    --编辑profile文件
    vim /etc/profile
    --配置mysql环境变量
    PATH=/data/mysql/bin:/data/mysql/lib:$PATH
    export PATH
    --使mysql环境变量生效
    source /etc/profile
    --看环境变量是否生效
    echo $PATH
    Copy after login

    Start mysql

    cd /app/mysql/bin
    systemctl start mysqld
    or
    service mysql start
    Copy after login

    You may encounter the following error when starting mysql:

    Failed to start mysqld. service: Unit not found.

    How to install MySQL database in Linux environment

    or

    ##Starting MySQL.Logging to '/app/mysql/data/dengwu.err '.

    ... ERROR! The server quit without updating PID file (/app/mysql/data/dengwu.pid).

    How to install MySQL database in Linux environmentThe solution is as follows:

    --需要安装mariadb-server
    yum install -y mariadb-server
    --然后启动mariadb服务
    systemctl start mariadb.service
    --需要的可以添加mariadb服务开机启动
    systemctl enable mariadb.service
    Copy after login

    Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.

    How to install MySQL database in Linux environmentThe solution is as follows:

    chown -R mysql.mysql /app/mysql/
    Copy after login

    Starting MySQL... ERROR! The server quit without updating PID file (/app/mysql/data /dengwu.pid).

    The solution is as follows:
    --查看mysql进程
    ps -ef|grep mysqld
    --杀死mysql进程
    kill -9 mysql进程ID
    Copy after login

    Then restart mysql:

    How to install MySQL database in Linux environment4. Modification mysql password

    The root user logs in to mysql for the first time, because we don’t know the initial password. Under normal circumstances, we will reset a new password. The specific operations are as follows:

    --编辑my.cnf
    vim /etc/my.cnf
    Copy after login

    Enter the following command Line:

    default_authentication_plugin=mysql_native_password
    Copy after login

    If you forget your password, add:

    --跳过密码验证(等设置了密码就去掉)
    skip-grant-tables
    Copy after login

    Then log in to mysql:

    --登录mysql
    mysql -u root -p
    Copy after login

    Then enter the command to view the mysql user group:

    --查看mysql用户表
    select user,host,authentication_string from mysql.user;
    Copy after login

    How to install MySQL database in Linux environmentCheck that the root user has not enabled remote connection permissions. If not, execute the following command:

    --修改root用户可以远程连接
    update mysql.user set host='%' where user='root';
    Copy after login

    After enabling remote connection permissions, change the root user password:

    --如果host是localhost则@字符后面是localhost,反之则是%,以host结果为准
    --修改加密规则
    alter user 'root'@'%' identified by 'qwer1234' password expire never;
    --更新一下用户的密码
    alter user 'root'@'%' identified with mysql_native_password by 'qwer1234';
    --刷新权限
    flush privileges;
    --修改root用户密码
    alter user 'root'@'%' identified by 'qwer1234';
    Copy after login

    How to install MySQL database in Linux environmentIf the firewall is turned on, you need to add permission to allow mysql port access. The specific command is as follows:

    --允许访问
    firewall-cmd --permanent --zone=public --add-port=3306/tcp
    --重新加载
    firewall-cmd --reload
    --查看是否开通访问权限
    firewall-cmd --permanent --zone=public --query-port=3306/tcp
    Copy after login

    Then restart mysql:

    --重新启动mysql
    service mysql restart;
    Copy after login

    5. Configure mysql Alibaba Cloud Security Group Policy

    Log in to Alibaba Cloud->Security Group Rules->Access Rules->Inbound Direction->Manually add the following policy:

    How to install MySQL database in Linux environmentSuccessful connection using Navicat:

    The above is the detailed content of How to install MySQL database in Linux environment. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!