1 下载安装文件
查看下系统环境
# uname -a
Linux xxx 4.14.0_1-0-0-45 #2 SMP Tue Oct 19 18:27:28 CST 2021 x86_64 x86_64 x86_64 GNU/Linux
# cat /etc/centos-release
CentOS Linux release 7.5.1804 (Core)
# ldd --version
ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.
官网 下载 mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar.xz
(因为上面查到的系统glibc是2.17,所以选择了相对应的版本)
2 准备工作
检查下是否安装过的残留文件
$ sudo su -
# rpm -qa | grep mysql
如果存在则删除
# rpm -e --nodeps mysql-xxxxxxxxx
检查是否存在残留的mysql相关配置文件,如果有非预期的,可以删除
# whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql
# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/share/mysql
/usr/lib64/mysql
检查是否存在计划运行mysql的用户和用户组,不存在则创建
# cat /etc/group | grep mysql
# cat /etc/passwd |grep mysql
# groupadd mysql
# useradd -r -g mysql mysql
# cat /etc/group | grep mysql
mysql:x:1002:
# cat /etc/passwd |grep mysql
mysql:x:996:1002::/home/mysql:/bin/bash
因为规划将mysql的数据存放在/home/disk1
,在该目录下创建mysql
目录,并将目录权限设为777
# cd /home/disk1
# mkdir mysql
# chmod 777 mysql
3 安装并启动
将上面下载的安装文件解压,移动到/usr/local/mysql
目录下
# tar -xvf mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar.xz
# ls
mysql-8.0.28-linux-glibc2.17-x86_64-minimal mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar.xz
# mv mysql-8.0.28-linux-glibc2.17-x86_64-minimal /usr/local/mysql
编辑配置文件,内容如下:
# vim /etc/my.cnf
[mysqld]
# datadir=/var/lib/mysql
# socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
# symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/home/disk1/mysql
socket=/tmp/mysql.sock
log-error=/home/disk1/mysql/mysql.err
pid-file=/home/disk1/mysql/mysql.pid
###character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true
[mysqld_safe]
log-error=/home/disk1/mysql/mariadb.log
pid-file=/home/disk1/mysql/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
初始化mysql
# cd /usr/local/mysql/
# ./bin/mysqld --initialize --user=mysql --datadir=/home/disk1/mysql --basedir=/usr/local/mysql
初始化需要一些时间,耐心等待。初始化完后,在mysql数据目录下会看到生成了一系列的文件。其中的mysql.err
文件中记录了root用户的初始密码。记住这个密码,初次登录mysql可以使用这个密码。
# cd /home/disk1/mysql/
# ls
auto.cnf ca-key.pem ca.pem client-cert.pem client-key.pem #ib_16384_0.dblwr #ib_16384_1.dblwr ib_buffer_pool ibdata1 ib_logfile0 ib_logfile1 #innodb_temp mysql mysql.err mysql.ibd performance_schema private_key.pem public_key.pem server-cert.pem server-key.pem sys undo_001 undo_002
# cat mysql.err
2022-09-24T09:14:21.867222Z 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.
2022-09-24T09:14:21.867339Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 31621
2022-09-24T09:14:21.927464Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-09-24T09:14:28.444781Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-09-24T09:14:48.505031Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 0HVtqbUo1z#w
将mysql.server
文件拷贝到/etc/init.d/mysql
目录下
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
这样就可以以服务的方式启动mysql了
# service mysql start
# ps -ef | grep mysql
root 44425 1 0 17:21 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/home/disk1/mysql --pid-file=/home/disk1/mysql/mysql.pid
mysql 44688 44425 6 17:21 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/disk1/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/home/disk1/mysql/mariadb.log --pid-file=/home/disk1/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 44893 21139 0 17:21 pts/0 00:00:00 grep --color=auto mysql
# lsof -i :3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 44688 mysql 24u IPv4 2942913256 0t0 TCP localhost:mysql (LISTEN)
登录mysql,修改密码
# ./bin/mysql -uroot -h 127.0.0.1 -p0HVtqbUo1z#w
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'mysql123';
Query OK, 0 rows affected (0.12 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.05 sec)
mysql> use mysql;
mysql> update user set user.Host='%' where user.User='root';
mysql> flush privileges;
上面在修改root用户密码时,使用了WITH mysql_native_password
参数,如果不加这个参数,远程登录时可能会报下面的错误:
$ mysql -hx.x.x.x -uroot -pmysql123 -P3306
Warning: Using a password on the command line interface can be insecure.
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: .../lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
现在本地和远程都可以登录并使用mysql了
$ mysql -hx.x.x.x -uroot -pmysql123 -P3306
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)