Linux actual combat - the installation and configuration process of MySQL5.7 and MySQL8.0 in CentOS and Ubuntu respectively

Linux in practice: Deploying various software

MySQL5.7 version installed on CentOS system

Installation (requires root privileges or sudo privilege escalation)

  1. Configure the yum warehouse

    # 更新密钥
    rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
    
    # 安装Mysql yum库
    rpm -Uvh http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
    

    Since MySQL is not in the official repository of CentOS, we use the above rpm command:

    • Import the key for the MySQL repository
    • Configure the yum warehouse of MySQLQ
  2. Install MySQL using yum

    # yum安装Mysql
    yum -y install mysql-community-server
    
  3. After the installation is complete, start MySQL and configure it to start automatically at boot

    systemctl start mysqld		# 启动
    systemctl enable mysqld		# 开机自启
    

    After the MySQL installation is complete, it will be automatically configured as mysqlda service named: , which can be managed by systemctl

  4. Check the running status of MySQL

    systemctl status mysqld
    

configuration

Mainly configure the password of the administrator user root and configure the permission to allow remote login.

  1. Obtain the initial password of MySQL

    # 通过grep命令,在/var/log/mysqld.log文件中,过滤temporary password关键字,得到初始密码
    cat /var/log/mysqld.log | grep "temporary password"
    

    [In the returned results, ... root@localhost: xxxxxx, this xxxxxx is the root password]

  2. Log in to the MySQL database system

    # 执行
    mysql -uroot -p
    # 解释
    # -u,登陆的用户,MySQL数据库的管理员用户同Linux一样,是root
    # -p,表示使用密码登陆
    
    # 执行完毕后输入刚刚得到的初始密码,即可进入MySQL数据库
    
  3. Change root user password

    # 在MySQL控制台内执行
    ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';	-- 密码需要符合:大于8位,有大写字母,有特殊符号
    
  4. [Extended], configure a simple password for root

    We can set a simple password for root, such as 123456.

    Please note that this configuration is only for MySQL in the test environment or learning environment. If it is for official use, please do not set a simple password

    # 如果你想设置简单密码,需要降低Mysql的密码安全级别
    set global validate_password_policy=LOW; # 密码安全级别低
    set global validate_password_length=4;	 # 密码长度最低4位即可
    
    # 然后就可以用简单密码了(这里使用简单密码,为了方便,生产中不要这样)
    ALTER USER 'root'@'localhost' IDENTIFIED BY '简单密码';
    
  5. [Extended], configure root to run remote login

    By default, the root user does not enable remote login, and is only allowed to log in to the MySQL system on the Linux server where MySQL is located.

    Be aware that allowing root remote logins is a security risk

    # 授权root远程登录
    grant all privileges on *.* to root@"IP地址" identified by '密码' with grant option;  
    # IP地址即允许登陆的IP地址,也可以填写%,表示允许任何地址
    # 密码表示给远程登录独立设置密码,和本地登陆的密码可以不同
    
    # 刷新权限,生效
    flush privileges;
    
  6. Exit the MySQL console page

    # 退出命令
    exit
    
    # 或者通过快捷键退出:ctrl + d
    
  7. check port

    MySQL is bound to port 3306 by default, you can check the network status of MySQL through port occupation

    netstat -anp | grep 3306
    

At this point, MySQL is installed and available. Please keep the MySQL root password well.

MySQL8.0 version installed on CentOS system

Install

  1. Configure the yum warehouse

    # 更新密钥
    rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
    
    # 安装Mysql8.x版本 yum库
    rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm
    
  2. Install MySQL using yum

    # yum安装Mysql
    yum -y install mysql-community-server
    
  3. After the installation is complete, start MySQL and configure it to start automatically at boot

    systemctl start mysqld		# 启动
    systemctl enable mysqld		# 开机自启
    

    After the MySQL installation is complete, it will be automatically configured as mysqlda service named: , which can be managed by systemctl

  4. Check the running status of MySQL

    systemctl status mysqld
    

configuration

Mainly modify the root password and allow root remote login

  1. Obtain the initial password of MySQL

    # 通过grep命令,在/var/log/mysqld.log文件中,过滤temporary password关键字,得到初始密码
    grep 'temporary password' /var/log/mysqld.log
    
  2. Log in to the MySQL database system

    # 执行
    mysql -uroot -p
    # 解释
    # -u,登陆的用户,MySQL数据库的管理员用户同Linux一样,是root
    # -p,表示使用密码登陆
    
    # 执行完毕后输入刚刚得到的初始密码,即可进入MySQL数据库
    
  3. change root password

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';	-- 密码需要符合:大于8位,有大写字母,有特殊符号,不能是连续的简单语句如123,abc
    
  4. [Extended], configure a simple password for root

    We can set a simple password for root, such as 123456.

    Please note that this configuration is only for MySQL in the test environment or learning environment. If it is for official use, please do not set a simple password

    set global validate_password.policy=0;		# 密码安全级别低
    set global validate_password.length=4;		# 密码长度最低4位即可
    
  5. Allow root to log in remotely and set a remote login password

    By default, the root user does not enable remote login, and is only allowed to log in to the MySQL system on the Linux server where MySQL is located.

    Be aware that allowing root remote logins is a security risk

    # 第一次设置root远程登录,并配置远程密码使用如下SQL命令
    create user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码!';	-- 密码需要符合:大于8位,有大写字母,有特殊符号,不能是连续的简单语句如123,abc
    
    # 后续修改密码使用如下SQL命令
    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
    
  6. Exit the MySQL console page

    # 退出命令
    exit
    
    # 或者通过快捷键退出:ctrl + d
    
  7. check port

    MySQL is bound to port 3306 by default, you can check the network status of MySQL through port occupation

    netstat -anp | grep 3306
    

At this point, MySQL is installed and available. Please keep the MySQL root password well.

MySQL5.7 version is installed in Ubuntu (WSL environment) system

The installation operation requires root privileges, you can:

  1. Switch to the root user via su

  2. Or add sudo before each command to temporarily elevate privileges

Install

  1. Download the apt repository file

    # 下载apt仓库的安装包,Ubuntu的安装包是.deb文件
    wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb
    

    image-20221016094103315

  2. Configure the apt repository

    # 使用dpkg命令安装仓库
    dpkg -i mysql-apt-config_0.8.12-1_all.deb
    

    Select in the pop-up box: ubuntu bionic(The code name of the Ubuntu 18.04 system is bionic, select the version library of 18.04 for installation)

    image-20221016094142343

    In the pop-up box select:MySQL Server & Cluster

    image-20221016094216377

    In the pop-up box select:mysql-5.7

    image-20221016094254397

    Final choice:ok

    image-20221016094306917

  3. Update the information of the apt repository

    # 首先导入仓库的密钥信息
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
    # 更新仓库信息
    apt update
    
  4. Check whether the warehouse of MySQL5.7 is successfully configured

    apt-cache policy mysql-server
    

    image-20221016094546943

    See the words as shown in the picture, that is success

  5. Install MySQL5.7

    # 使用apt安装mysql客户端和mysql服务端
    apt install -f -y mysql-client=5.7* mysql-community-server=5.7*
    

    Enter the root password in the pop-up box and select ok, the password is arbitrary

    image-20221016094941439

    Enter the root password again to confirm

    image-20221016094954505

  6. start mysql

    /etc/init.d/mysql start			# 启动
    /etc/init.d/mysql stop			# 停止
    /etc/init.d/mysql status		# 查看状态
    

    image-20221016095259172

  7. Initialize MySQL

    # 执行如下命令,此命令是MySQL安装后自带的配置程序
    mysql_secure_installation
    # 可以通过which命令查看到这个自带程序所在的位置
    root@DESKTOP-Q89USRE:~# which mysql_secure_installation
    /usr/bin/mysql_secure_installation
    
    1. enter password:

      image-20221016095458755

    2. Whether to enable the password verification plug-in, if you need to enhance the security of the password, enter yand press Enter, no need to press Enter directly (here choose to press Enter directly)

      image-20221016095537716

    3. Whether to change the root password, you need to press yEnter, you don’t need to press Enter directly (here you choose to press Enter directly)

      image-20221016095621386

    4. Whether to remove the anonymous user, remove the input and press yenter, or directly enter without removing (selected to remove here)

      image-20221016101232827

    5. Whether to enable remote login of the root user, prohibit input and press yreturn, and do not prohibit direct press return (here, direct press return is selected)

      image-20221016101324577

    6. Whether to remove the built-in test database, remove the input and press yEnter, or directly press Enter without removing it (here choose to press Enter directly)

      image-20221016101404392

    7. Whether to refresh the permission, refresh the input and press yEnter, and press Enter without refreshing (here choose to refresh)

      image-20221016101442459

  8. Login to MySQL

    mysql -uroot -p
    # 输入密码即可登陆成功
    

    image-20221016101524498

So far, the MySQL5.7 version has been successfully installed on Ubuntu.

MySQL8.0 version is installed in Ubuntu (WSL environment) system

The latest version of Ubuntu 22.04, the built-in MySQL of this version of the software store is version 8.0

So it can be installed directly through apt

The installation operation requires root privileges, you can:

  1. Switch to the root user via su

  2. Or add sudo before each command to temporarily elevate privileges

Install

  1. If you have already installed MySQL5.7, you need to uninstall the warehouse information

    # 卸载MySQL5.7版本
    apt remove -y mysql-client=5.7* mysql-community-server=5.7*
    
    # 卸载5.7的仓库信息
    dpkg -l | grep mysql | awk '{print $2}' | xargs dpkg -P
    
  2. Update apt warehouse information

    apt update
    
  3. install mysql

    apt install -y mysql-server
    
  4. start mysql

    /etc/init.d/mysql start			# 启动
    /etc/init.d/mysql stop			# 停止
    /etc/init.d/mysql status		# 查看状态
    
  5. Log in to MySQL and set a password

    # 直接执行:mysql
    mysql
    
  6. set password

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
    
  7. exit the mysql console

    exit
    
  8. Initialize MySQL

    # 执行如下命令,此命令是MySQL安装后自带的配置程序
    mysql_secure_installation
    # 可以通过which命令查看到这个自带程序所在的位置
    root@DESKTOP-Q89USRE:~# which mysql_secure_installation
    /usr/bin/mysql_secure_installation
    
    1. enter password:

      image-20221016095458755

    2. Whether to enable the password verification plug-in, if you need to enhance the security of the password, enter yand press Enter, no need to press Enter directly (here choose to press Enter directly)

      image-20221016095537716

    3. Whether to change the root password, you need to press yEnter, you don’t need to press Enter directly (here choose to press Enter directly)

      image-20221016095621386

    4. Whether to remove anonymous users, remove the input and press yEnter, or directly press Enter without removing (choose to remove here)

      image-20221016101232827

    5. Whether to enable remote login of the root user, prohibit input and press yreturn, and do not prohibit direct press return (here choose direct press return)

      image-20221016101324577

    6. Whether to remove the built-in test database, remove the input and press yEnter, or directly press Enter if you do not remove it (here choose to press Enter directly)

      image-20221016101404392

    7. Whether to refresh the permission, refresh the input and press yEnter, and press Enter without refreshing (select refresh here)

      image-20221016101442459

  9. Log in to MySQL again (with the changed password)

    mysql -uroot -p
    

    image-20221016110414182

So far, the MySQL5.7 version has been successfully installed on Ubuntu.

Guess you like

Origin blog.csdn.net/qq_41954181/article/details/129972987