Install MySQL 8.0.x under Linux system

Linux系统版本:CentOS 8.3 64MySQL版本:mysql-8.0.25-linux-x86_64

1. Download the MySQL installation package

2. Upload the MySQL installation package to the Linux server

  • Use Xftp to upload files (any other ftp software can be used) The storage path here is customized by me (can be uploaded directly to a customized directory)
  • Note: The default installation path of MySQL is /usr/local/mysql (if the path changes, subsequent configuration may have some impact)
    Insert image description here

3. Unzip the MySQL installation package

  • Enter the MySQL installation package storage path
cd 路径cd	
例: /usr/libo/environment
ll 查看路径下所有目录

Insert image description here

  • Extract the .xz file to .tar
xz -d .xz文件名称	
例:xz -d mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz

Insert image description here

  • Unzip the MySQL .tar installation package
tar -xvf .tar文件名称	
例:tar -xvf mysql-8.0.25-linux-glibc2.12-x86_64.tar

Insert image description here

  • Decompression completed

Insert image description here

  • Modify the MySQL file name (the name is too long, it is recommended to modify it)
mv 原目录名称 新目录名称
例:mv mysql-8.0.25-linux-glibc2.12-x86_64 mysql-8.0.25

Insert image description here

4. Create the MySQL data storage directory and authorize it

  • After entering the MySQL directory, use the command to create the MySQL data directory. The name can be chosen by yourself, for example: data
mkdir 目录名

Insert image description here

  • Authorize the MySQL directory (including the MySQL data storage directory)
chmod能改变权限,-R是目录下所有文件,777就是高权限(读、写、执行)
chmod -R 777 * 意思就是将当前目录下所有文件都给予777权限

Insert image description here

5. Create a MySQL group: Create a MySQL user and set a password

  • Add user, set password
添加用户:useradd 用户名
设置密码:passwd 用户名
  • Grant permissions to the mysql directory to the mysql user and mysql group
chown -R mysql:mysql /usr/libo/environment/mysql-8.0.25
  • Note: When setting the password, if it is less than 8 characters, a prompt will appear. Just fill it in again. It will not affect the password.

Insert image description here

6. Create the MySQL installation initialization configuration file my.cnf in /etc in the system root directory

  • The content is as follows:
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=/usr/libo/environment/mysql-8.0.25
# 设置mysql数据库的数据的存放目录
datadir=/usr/libo/environment/mysql-8.0.25/data
# 允许最大连接数
max_connections=10000
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8
  • Use commands or modify the my.cnf file directly in ftp
vi /etc/my.cnf
按下i健进入 insert模式进行修改,完成后按下esc键再后输入 :wq 保存并退出(英文模式下,注意wq前面还有冒号:)

Insert image description here

7.Initialize MySQL

  • Enter the bin directory of MySQL
cd /usr/libo/environment/mysql-8.0.25/bin
  • Execute the command and remember the random password produced (will be used when changing the password later)
./mysqld --initialize --console

Insert image description here

8. Start the MySQL service

  • Run the MySQL service
cd /usr/libo/environment/mysql-8.0.25/support-files
./mysql.server start

Insert image description here

  • Possible problems and solutions during startup:

1. When starting the MySQL service, an updating PID file error occurs.
Insert image description here

Solution: Re-authorize in the installation directory, and then start MySQL

chmod -R 777 /usr/libo/environment/mysql-8.0.25
chmod -R 777 /usr/libo/environment/mysql-8.0.25/data

2. When starting the MySQL service, it reports my_print_defaults: Command not found. Solution
Insert image description here
: Check and modify the MySQL installation directory in /etc/my.cnf.
Insert image description here
3. If it is confirmed that there is no problem with the MySQL installation path, it is likely that our my is not read. .cnf configuration file

As you can see in the mysql.server file, the default path of MySQL is /usr/local/mysql because we customize the path. So you need to establish a connection to the my.cnf file
Insert image description here

将mysql目录下的bin/my_print_defaults链接到/usr/bin目录下。

cd /usr/bin
sudo ln -s /usr/libo/environment/mysql-8.0.25/bin/my_print_defaults
为什么需要my_print_defaults呢?这是因为mysql.server执行时就是通过my_print_defaults来读取my.cnf配置变量的。

Reference blog: https://blog.csdn.net/u012794845/article/details/86577399

  • Add MySQL to the system process
MySQL加入系统进程中
cp mysql.server /etc/init.d/mysql
然后重启MySQL服务
service mysql restart

Insert image description here

  • Change login password
进入MySQL 密码是之前自动生成的密码 
./mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';

Insert image description here

  • Set up to allow remote login
use mysql	切换数据库
update user set user.Host='%' where user.User='root';	修改HOST为%
flush privileges;	修改成功后刷新权限

Insert image description here

  • Exit MySQL
quit;
  • Restart the MySQL service
 service mysql restart

Insert image description here

  • Test remote connection

Insert image description here

If you love life don't waste time, because time makes up life.

Guess you like

Origin blog.csdn.net/weixin_45377770/article/details/118544750