一、安装Mysql服务
1.1、下载二进制安装包
官方:二进制安装包下载
[root@localhost ~]# cd /data/pkgs
[root@localhost pkgs]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
[root@localhost pkgs]# tar axf mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
[root@localhost pkgs]# mv mysql-8.0.30-linux-glibc2.12-x86_64 mysql
1.2、创建MySQL用户和用户组
[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -M -g mysql -s sbin/nologin mysql
1.3、创建MySQL目录并授权
#创建MySQL数据目录
[root@localhost ~]# mkdir -p /data/pkgs/mysql/data
[root@localhost ~]# chown mysql:mysql /data/pkgs/mysql/data -R
#创建MySQL日志目录
[root@localhost ~]# mkdir -p /data/pkgs/mysql/logs
[root@localhost ~]# chown mysql:mysql /data/pkgs/mysql/logs -R
#家目录授权
[root@localhost ~]# chown mysql:mysql /data/pkgs/mysql -R
1.4、配置MySQL环境变量
[root@localhost ~]# echo 'MYSQL_HOME=/data/pkgs/mysql' >> /etc/profile.d/mysql.sh
[root@localhost ~]# echo 'PATH=$PATH:$MYSQL_HOME/bin' >> /etc/profile.d/mysql.sh
[root@localhost ~]# echo 'export PATH' >> /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh
1.5、创建my.cnf配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
basedir=/data/pkgs/mysql
datadir=/data/pkgs/mysql/data
log-error=/data/pkgs/mysql/logs/mysql.log
socket=/data/pkgs/mysql/mysql.sock
pid-file=/data/pkgs/mysql/mysqld.pid
port=53000
[client]
socket=/data/pkgs/mysql/mysql.sock
1.6、初始化Mysql数据库
[root@localhost mysql]# mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize
1.7、配置systemctl mysqld服务
#编辑mysqld.service文件
[root@localhost ~]# vim /etc/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=/data/pkgs/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=65535
1.8、启动Mysql服务
#启动mysql服务并设置开机自启
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
二、登录MySQL
说明:在MySQL初始化(initialize)完成后会自动为MySQL的root用户生成一个随机初始化密码,记录在MySQL的log-error日志文件中
[root@localhost ~]# grep password /data/pkgs/mysql/logs/mysql.log
如下图所示:
三、修改root用户临时密码
mysql> ALTER USER USER() IDENTIFIED BY 'Lolaage@sdjw2022';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Lolaage@sdjw2022';
Query OK, 0 rows affected (0.01 sec)
如下图所示:
四、配置数据库用户登录权限
说明:查看user表中用户远程登录授权,host字段是localhost,说明用户只能本地登录
mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
如下图所示:
总结:整理不易,如果对你有帮助,可否点赞关注一下?
更多详细内容请参考:《Linux运维篇:Linux系统运维指南》