参考:实战篇:手把手教你Linux安装Mysql(细致入微) - 云+社区 - 腾讯云
官网链接
环境:vmware
系统:centos 7
阿里云下载链接,选择自己使用版本
Index of /centos/https://mirrors.aliyun.com/centos/?spm=a2c6h.13651104.0.0.2b2512b2Yo87J4
mysql版本:mysql 5.7.20,官网链接:MySQL :: Download MySQL Community Server (Archived Versions)https://downloads.mysql.com/archives/community/
2.安装过程
2.1 安装centos系统,自行百度虚拟机安装centos教程
2.2 将mysql介质通过ftp或者xshell的文件传输上传到服务器中
2.2.1 创建soft文件夹,并将文件复制到该目录下并解压
[root@localhost ~]# ls
anaconda-ks.cfg mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# mkdir /soft
[root@localhost ~]# mv mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz /soft/
[root@localhost ~]# cd /soft/
[root@localhost soft]# ls
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@localhost soft]# tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@localhost soft]# mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql
[root@localhost soft]# cd mysql
[root@localhost mysql]# ll
total 36
drwxr-xr-x. 2 root root 4096 Oct 26 20:55 bin
-rw-r--r--. 1 7161 31415 17987 Sep 13 2017 COPYING
drwxr-xr-x. 2 root root 55 Oct 26 20:55 docs
drwxr-xr-x. 3 root root 4096 Oct 26 20:55 include
drwxr-xr-x. 5 root root 229 Oct 26 20:55 lib
drwxr-xr-x. 4 root root 30 Oct 26 20:55 man
-rw-r--r--. 1 7161 31415 2478 Sep 13 2017 README
drwxr-xr-x. 28 root root 4096 Oct 26 20:55 share
drwxr-xr-x. 2 root root 90 Oct 26 20:55 support-files
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -r -g mysql -s /bin/false mysql
2.2.2修改目录权限及所属主
[root@localhost mysql]# mkdir -p /data/mysql
[root@localhost mysql]# chown -R mysql:mysql /data
[root@localhost mysql]# chown -R mysql:mysql /soft
[root@localhost mysql]# chmod 750 /data
2.2.3配置环境变量
[root@localhost mysql]# cat <<EOF>> /root/.bash_profile
> export PATH=\$PATH:/soft/mysql/bin
> EOF
[root@localhost mysql]# source /root/.bash_profile
2.2.4 安装依赖(本服务器已经安装无需再装)
[root@localhost mysql]# yum install libaio
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.aliyun.com
Package libaio-0.3.109-13.el7.x86_64 already installed and latest version
Nothing to do
2.2.5 卸载已有mysql及mariadb
[root@localhost mysql]# rpm -qa | grep mysql
[root@localhost mysql]# rpm -qa | grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@localhost mysql]# rpm -e --nodeps $(rpm -qa|grep mariadb)
[root@localhost mysql]# rpm -qa | grep mariadb
2.2.6 初始化mysql
[root@localhost mysql]# mysqld --initialize --user=mysql --basedir=/soft/mysql
--datadir=/data/mysql
2021-10-27T01:11:54.917169Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp serve
2021-10-27T01:11:55.126813Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-10-27T01:11:55.163403Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-10-27T01:11:55.224847Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been sta
2021-10-27T01:11:55.225923Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-10-27T01:11:55.227094Z 1 [Note] A temporary password is generated for root@localhost: Bz0_0,k30n7q
给出root的初始密码为:Bz0_0,k30n7q
2.2.7创建my.cnf文件并做配置
[root@localhost etc]# cat <<EOF>/etc/my.cnf
> [mysqld]
> user=mysql
> basedir=/soft/mysql
> datadir=/data/mysql
> server_id=6
> port=3306
> socket=/tmp/mysql.sock
> ##客户端
> [mysql]
> socket=/tmp/mysql.sock
> prompt=lucifer [\\\\d]>
> EOF
2.2.8 尝试启动
[root@localhost etc]# /soft/mysql/support-files/mysql.server start
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
SUCCESS!
可以成功启动
2.2.9 将服务添加到自动启动目录
[root@localhost etc]# cp /soft/mysql/support-files/mysql.server /etc/init.d/mysqld
可以使用如下命令启动服务
[root@localhost init.d]# service mysqld start
添加配置文件可以使用systemctl操作
[root@localhost init.d]# cat <<EOF>>/usr/lib/systemd/system/mysqld.service
> [Unit]
> Description=MySQL Server
> Documentation=man:mysqld(8)
> Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
> After=network.target
> After=syslog.target
> [Install]
> WantedBy=multi-user.target
> [Service]
> User=mysql
> Group=mysql
> ExecStart=/soft/mysql/bin/mysqld --defaults-file=/etc/my.cnf
> LimitNOFILE = 5000
> EOF
2.2.10 由于第一次登录mysql需要修改root密码,因此直接执行如下命令:
[root@localhost init.d]# mysqladmin -uroot -pBz0_0,k30n7q password mysql
修改root账号密码为mysql,登录尝试
[root@localhost init.d]# mysql -uroot -pmysql
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 9
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
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的安装