Install the MySQL database 8.0 service instance

foreword

I tried to install the community version before , and the version I installed MySQL5.7today , and installed it in two ways, one is through the installation of the RPM package, and the other is the way of compiling.MySQL8.0

1. Preliminary preparation

View server IP

[root@localhost ~]# hostname -I
192.168.161.166 192.168.122.1 

Whether the network can be pinged

[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=53 time=20.0 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=53 time=41.4 ms

Turn off firewall and SELinux

[root@localhost ~]# iptables -F 
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

2. Install MySQL8.0 using RPM

MySQL5.7If you have learned from the previous installation articles, then for installation MySQL8, some installation processes are the same.
Linux installation MySQL5.7 database steps

2.1 Clear the mariadb file in the system

CentOS7The installation on the above MySQLneeds to clear mariadbthe relevant files and software packages first, otherwise MySQL8the official installation will be affected during the subsequent installation process.

[root@localhost ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost ~]# rpm -qa | grep mysql
[root@localhost ~]# rpm -e mariadb-libs-5.5.68-1.el7.x86_64 
错误:依赖检测失败:
	libmysqlclient.so.18()(64bit)(已安裝) postfix-2:2.10.1-9.el7.x86_64 需要
	libmysqlclient.so.18(libmysqlclient_18)(64bit)(已安裝) postfix-2:2.10.1-9.el7.x86_64 需要

If the above situation occurs, you can directly add --nodepsthe forced removal of the package

rpm -e mariadb-libs-5.5.68-1.el7.x86_64  --nodeps

Then delete some files that the mariadb service originally comes with the system, you can use it findto search

[root@localhost ~]# find / -name mariadb
[root@localhost ~]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/lib64/mysql
[root@localhost ~]# rm -rf /etc/selinux/targeted/active/modules/100/mysql /usr/lib64/mysql

2.2 Use Ali network source

After the files are cleared, you can proceed to the next step, which is to back up the built-in base source. Of course, it is also possible to delete it directly.

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak

Replace it with the network source, here replace it with Ali's source and use
the network source address: Alibaba Cloud CentOS source

[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#查看源的状态信息
[root@localhost yum.repos.d]# yum repolist
#清空之前的一些软件包信息
[root@localhost yum.repos.d]# yum clean all
#重新建立元缓存
[root@localhost yum.repos.d]# yum makecache 

2.3 Install MySQL8.0

Starting from this step is the formal installation of MySQL. First go to the official website to download the required package.
If you don’t know how to find it, you can directly use ctrl+F to search for the address of the package you need, then click, and then copy the full download link to the server. superior.
MySQL RPM package address: click here to
insert image description here
insert image description here
use wgetthe command to download rpmthe package

wget https://repo.mysql.com/mysql80-community-release-el7.rpm

After downloading, the default rpmpackage location is local and needs to be installed using the rpm command.
insert image description here

[root@localhost yum.repos.d]# rpm -ivh mysql80-community-release-el7.rpm 
警告:mysql80-community-release-el7.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql80-community-release-el7-7  ################################# [100%]

If you still don’t remember what ivh means, let’s talk about
i for installing software packages
v for displaying process
h for displaying progress bar

After performing the previous step, /etc/yum.repos.dthere is a source in the directory repo. At this time, you can use the yum command to install the program in this source.

[root@localhost yum.repos.d]# ls
CentOS-Base.repo       CentOS-Vault.repo
CentOS-Base.repo.bak   CentOS-x86_64-kernel.repo
CentOS-CR.repo         epel.repo
CentOS-Debuginfo.repo  mysql80-community-release-el7.rpm
CentOS-fasttrack.repo  mysql-community-debuginfo.repo
CentOS-Media.repo      mysql-community.repo
CentOS-Sources.repo    mysql-community-source.repo
yum install mysql-community-server -y

2.4 Start the service

The previous installation has ended, and it is time to start the service

[root@localhost yum.repos.d]# systemctl start mysqld
[root@localhost yum.repos.d]# systemctl enable mysqld

Find the original password for the service

[root@localhost yum.repos.d]# grep -aiw "Password" /var/log/mysqld.log 
2023-03-09T09:03:09.167404Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: p!c?qf/4gfqU

The colon is followed by the original MySQL password.
insert image description here
We need to continue to the next step, which is to modify the original password.

[root@localhost yum.repos.d]# mysql -uroot -p
Enter password:     #输入原始密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.32

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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.
#修改密码,密码尽可能的复杂些,至少需要8位以上。
mysql> alter user 'root'@'localhost' identified by 'Dianzan80/';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

Using RPMthe installation process is so much content, let's continue to see how to compile and install MySQL8.0.

3. Install MySQL8.0 with binary package

To install the database by compiling, you first need to know the location of the compressed package
and compile the official website address of the package: Click here

3.1 Download compressed package

downloaded MySQL8.0zip file

wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz

Unzip the package and move to /usr/local/the next

[root@localhost ~]# tar xf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz 
[root@localhost ~]# mv mysql-8.0.32-linux-glibc2.12-x86_64 /usr/local/mysql

3.2 Create a system user

Create a user to facilitate database management

[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -r -g mysql -s /bin/false mysql
[root@localhost ~]# chown -R mysql:mysql /usr/local/mysql

3.3 Setting mysql global variables

Modify the global variable information in /etc/profile, and add these two sentences at the end.

[root@localhost ~]# vim /etc/profile
[root@localhost ~]# tail -n 2 !$
tail -n 2 /etc/profile
export MYSQL_HOME=/usr/local/mysql    1
export PATH=$MYSQL_HOME/bin:$PATH     2
[root@localhost ~]# source !$
source /etc/profile

insert image description here

3.4 Initialize the database

First create a directory for storing data in advance, and create it /usr/local/mysql/under the directory

[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# mkdir data

It's time to initialize the database

[root@localhost mysql]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2023-03-09T09:30:27.217276Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2023-03-09T09:30:27.217395Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.32) initializing of server in progress as process 3831
2023-03-09T09:30:27.223941Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-03-09T09:30:28.700635Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-03-09T09:30:30.538920Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: tm!NuBMBR2r/
[root@localhost mysql]# echo $?
0

user indicates the database name
basedir indicates the location where the database is stored
datadir indicates the place where the data is stored

It is not clear whether the execution is successful or not, you can use it echo $?to judge, and 0 means no problem.
Another point to note is that an original password will be generated after initialization, which needs to be known and used to log in to the database later.
insert image description here

3.5 Setting configuration files

mysqlThere is a directory file in the directory by default , support-filesand the configuration file in the directory needs to be copied to the /etc/init.d/startup item of the service.

[root@localhost support-files]# ll
总用量 20
-rwxr-xr-x. 1 mysql mysql  1061 1216 23:34 mysqld_multi.server
-rw-r--r--. 1 mysql mysql  2027 1217 00:26 mysql-log-rotate
-rwxr-xr-x. 1 mysql mysql 10576 1217 00:26 mysql.server
[root@localhost support-files]# cp mysql.server /etc/init.d/mysql.server

In addition, you need to add a configuration file in /etc/the directory

vim /etc/my.cnf
#以下是文件内要添加的信息,文件默认有内容,可以删除修改成如下内容。

[client]

port = 3306
socket = /tmp/mysql.sock
[mysqld]

#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id = 1

#服务端口号 默认3306
port = 3306

#mysql安装根目录
basedir = /usr/local/mysql

#mysql数据文件所在位置
datadir = /usr/local/mysql/data

#临时目录
tmpdir  = /tmp

#设置socke文件所在目录
socket = /tmp/mysql.sock

#主要用于MyISAM存储引擎,如果多台服务器连接一个数据库则建议注释下面内容
skip-external-locking

#只能用IP地址检查客户端的登录,不用主机名
skip_name_resolve = 1

#事务隔离级别,默认为可重复读,mysql默认可重复读级别(此级别下可能参数很多间隙锁,影响性能)
transaction_isolation = READ-COMMITTED

#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server = utf8mb4

#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci

#设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'

#是否对sql语句大小写敏感,1表示不敏感
#lower_case_table_names = 1

#最大连接数
max_connections = 400

#最大错误连接数
max_connect_errors = 1000

#TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
explicit_defaults_for_timestamp = true

#SQL数据包发送的大小,如果有BLOB对象建议修改成1G
max_allowed_packet = 128M


#MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
#MySQL默认的wait_timeout  值为8个小时, interactive_timeout参数需要同时配置才能生效
interactive_timeout = 1800
wait_timeout = 1800

#内部内存临时表的最大值 ,设置成128M。
#比如大数据量的group by ,order by时可能用到临时表,
#超过了这个值将写入磁盘,系统IO压力增大
tmp_table_size = 134217728
max_heap_table_size = 134217728


#数据库错误日志文件
log_error = error.log

#慢查询sql日志设置
slow_query_log = 1
slow_query_log_file = slow.log
#开启binlog
log_bin=on

#检查未使用到索引的sql
log_queries_not_using_indexes = 1

#针对log_queries_not_using_indexes开启后,记录慢sql的频次、每分钟记录的条数
log_throttle_queries_not_using_indexes = 5

#作为从库时生效,从库复制中如何有慢sql也将被记录
log_slow_slave_statements = 1

#慢查询执行的秒数,必须达到此值可被记录
long_query_time = 8

#检索的行数必须达到此值才可被记为慢查询
min_examined_row_limit = 100

#mysql binlog日志文件保存的过期时间,过期后自动删除
expire_logs_days = 5

Then there is a question, my.cnfwhat is the use of this configuration file?

In fact, my.cnf is a management configuration file of MYSQL. By modifying the contents of this file, MYSQL can be optimized and the overall performance of MYSQL can be improved.

3.6 Start the database

The above information is configured, but don't forget to start the database.

service mysql.server start 

Finally, if you see one SUCCESS, it means that the startup is successful.

3.6 Modify the database management system password

Remember the initial password of the previous database No, it needs to be used here. insert image description here
If the database does not recognize the password, add single quotes to log in

[root@localhost mysql]# mysql -uroot -ptm!NuBMBR2r/
bash: !NuBMBR2r/: event not found
[root@localhost mysql]# mysql -uroot -p'tm!NuBMBR2r/'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.32

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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.

mysql> alter user 'root'@'localhost' identified by 'Dianzan001/!';
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

The process of compiling and installing has also been demonstrated, and then you can directly log in to the database with the password you just set for operations.

Summarize

The compilation of MySQL is relatively complicated, you can try to do it a few times, and you will be familiar with the steps. If you think the above content is okay, you can like it and support it!
insert image description here

Guess you like

Origin blog.csdn.net/rhn_111/article/details/129425641