MySQL8.0 installation tutorial under Linux (CentOS7) - super detailed

1. Download MySQL

Mysql official website download address: https://downloads.mysql.com/archives/community/
insert image description here
version select mysql-8.0.15 (x86 64-bit) version under Linux-Generic

2. Import the MySQL installation package to the linux system

We import the downloaded mysql installation package into the linux system. Here we take the /usr/local/ path under the CentOS-7-2009 linux system as an example:
insert image description here

#解压
tar -xvf mysql-8.0.15-linux-glibc2.12-x86_64.tar.xz
#解压好后重命名:
mv mysql-8.0.15-linux-glibc2.12-x86_64 mysql 

insert image description here
Since CentOS comes with mariadb, it will conflict with mysql, so you need to uninstall it first

# 查看mariadb安装信息:
rpm -qa | grep mariadb
# 卸载mariadb:
rpm -e mariadb-libs-5.5.68-2.el7.x86_64 --nodeps

insert image description here

# 卸载好后再执行命令 
rpm -qa | grep mariadb  
# 这个也执行下,确认没有再出现安装信息表示卸载干净了,此时可以开始安装mysql了
rpm -qa | grep mysql

3. MySQL custom configuration

If mysql installation does not specify the installation path or other attributes, it will follow the default configuration of mysql. If we need to customize some configurations, we need to manually modify the configuration of mysql. The default configuration file
path of mysql is /etc/my.cnf
if there is no one in the system , can be created manually: vim /etc/my.cnf
insert image description here
The content in the above picture is the configuration of mysql installed on my own computer, here is a detailed introduction

# client表示客户端的配置		
[client]	
# 客户端连接mysql的端口号,默认3306
port = 3306			
#为MySQL客户程序与服务器之间的本地通信指定一个套接字文件(注意,如何客户端有该配置,服务端也需配置和客户端相同的路径,否则启动会报找不到该文件)
socket=/usr/local/mysql/mysql.sock	

[mysqldump]
#支持较大数据库的转储,在导出非常巨大的表时需要此项。
quick
# 服务器和客户端之间最大能发送的可能信息包
max_allowed_packet = 16M

# mysqld表示mysql服务端配置
[mysqld]
# mysql的安装路径
basedir = /usr/local/mysql
# mysql的数据存储路径(如果没有需要手动创建,否则启动报错)
datadir = /usr/local/mysql/data
# 服务端id
server-id = 1
#为MySQL客户程序与服务器之间的本地通信指定一个套接字文件(注意,如何客户端有该配置,服务端也需配置和客户端相同的路径,否则启动会报找不到该文件)
socket = /usr/local/mysql/mysql.sock
# 为mysqld程序指定一个存放进程ID的文件
pid-file = /usr/local/mysql/mysqld.pid

# mysql默认数据库字符集类型
character-set-server = utf8mb4
# mysql默认数据库排序规则(utf8mb4_bin是区分大小写的)
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
# 表名以及字段名是否区分大小写(默认为1,即不区分,设为2则表示区分)
lower_case_table_names = 1

The above is some configuration information during my installation. There are many other configurations of mysql. For details, please refer to here

4. Initialize MySQL and start

After the mysql custom configuration is complete, you can start initialization. Mysql initialization needs to specify a user, so we need to create a specified user. The general creation command is as follows

  • Create a group command: groupadd group name
  • Create a user and specify a group: useradd -g group name username -p password (the password can not be set during creation, but it is best to set a password for security)
  • View a user group: groups username
  • Grant all operation rights to the specified path to the specified user: chown -R username: group name needs to grant the full path name of the operation right

If no user is specified in the mysql configuration, the user named mysql is specified by default, so let's take creating a mysql user as an example:
insert image description here
after the user is created, the initialization can begin

  • Initialize mysql general command: mysqld --initialize --username --mysql installation path
    --datadir=database storage path

The mysqld at the beginning of the note here is the mysqld file in the bin folder under the mysql installation path, so the execution of the initialization command needs to be executed under the path of the mysqld file, or specify the full path of the mysqld file; the user name is configured in my.cnf The user name configured in , if mysql is not configured, the user with the specified user name mysql is used by default. Take
the initialization under the mysql installation path as an example:

# 进入mysql安装路径
cd /usr/local/mysql
# 初始化mysql:
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

insert image description here
If there is an error log during initialization, you need to solve the error and re-initialize. Re-initialization needs to clear all the files under the database storage path, that is, clear the files under /usr/local/mysql/data/, otherwise it will report repeated initialization Error;
after the initialization is successful, a root user and its initial password will be generated as shown in the above figure, and mysql installation is successful at this time

After the initialization is successful, you need to manually start mysql. mysql provides the original startup script. In the support-files folder under the installation path, execute the following command to start mysql:

# 进入support-files:
cd /usr/local/mysql/support-files
# 启动mysql:
./mysql.server start

insert image description here

5. Connect to MySQL

After the initialization of mysql is completed, you can connect to access MySQL. To
connect to mysql locally, you need to enter the bin folder under the mysql installation path and execute the following command:

# 进入bin路径
cd /usr/local/mysql/bin/
# 连接mysql
./mysql -uroot -p 密码

Enter the initial password that has just been successfully initialized. After successfully connecting to mysql, you need to modify the initial password.

  • Common command to modify password: ALTER USER 'user'@'host address' IDENTIFIED WITH
    mysql_native_password BY 'password'

It should be noted that because the version of mysql8.0 is installed, the user table of this version does not have a password field, and the password is stored in the authentication_string field, so it needs to be modified by the above command to modify the password, where the with mysql_native_password in the command indicates
visualization The tool can be connected, if the command does not include this paragraph, the visualization tool will not be able to connect

After successfully changing the password, execute the exit command to exit and repeat the connection command again to enter a new password to connect. The following is the command I used to modify the password for accessing the root user on this machine on my computer:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码'

insert image description here
After the initial password is changed, mysql can be used normally at this time, but we may need remote access. Mysql disables remote access by default, so remote access needs to be enabled. Enabling remote access is actually granting specified users remote machines that can pass through The user's permissions to access the database

  • User authorization general command: grant #auth on #databaseName.#table to'#userName'@'#host';

Here is an analysis of the role of each ##
auth represents authority, as follows

  • all privileges all privileges
  • select query permissions
  • select,insert,update,delete add, delete, modify and check permissions
  • select, […] add… and other permissions

#databaseName represents the database name
#table represents the specific table, as follows

  • * stands for all tables
  • A, B represent the specific A, B list

#userName represents the user name
#host represents the access authority, as follows

  • % stands for wildcarding all host address permissions (remote access)
  • localhost is local permission (not remote access)
  • Specify special IP access rights such as 192.168.56.40

Specific authorized users can refer to this article

Here is an example of enabling remote access to the database through the root user and having hot permissions. After connecting to the database locally, execute the following command:

# 进入mysql库
use mysql;
# 修改root用户能通过所有远程计算机访问
update user set host = '%' where user = 'root';
# 查看root用户是否修改成功
select host, user, authentication_string, plugin from user;
# 授予远程通过root用户访问数据库的任何操作权限
GRANT ALL ON *.* TO 'root'@'%';
# 刷新状态
flush privileges;

insert image description here

At this time, the remote computer cannot be directly accessed, and both sides need to close their respective firewalls

# 关闭防火墙命令
systemctl stop firewalld.service
# 查看防火墙状态
firewall-cmd --state

insert image description here
At this point, the remote computer can access
insert image description here

6. Set MySQL to start automatically at boot

The mysql installed above needs to be started manually every time it is turned on, and it must be set to start automatically after booting and press the command to operate

# 将服务文件拷贝到init.d下,并重命名为mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 赋予可执行权限
chmod +X /etc/init.d/mysqld
# 将mysql添加到系统服务
chkconfig --add mysqld
# 显示服务列表
chkconfig --list

At this point, if you see the mysql service, and if 3, 4, and 5 are all on, then it is successful. If it is off, type in
insert image description here
the command

chkconfig --level 345 mysqld on

At this point, the setting is complete, you can restart the machine to verify

Guess you like

Origin blog.csdn.net/weixin_44947701/article/details/122381398
Recommended