Centos7.6初始化安装MySQL8.0.27

一. 准备工作
 
1.查看系统版本
[root@Mysql8 ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
MySQL版本
---MySQL-mysql-8.0.27

2. 卸载mariadb
[root@Mysql8 ~]# rpm -qa|grep mariadb
mariadb-5.5.65-1.el7.x86_64
mariadb-libs-5.5.65-1.el7.x86_64

[root@Mysql8 ~]# rpm -e  --nodeps mariadb-5.5.65-1.el7.x86_64  mariadb-libs-5.5.65-1.el7.x86_64 
warning: /etc/my.cnf saved as /etc/my.cnf.rpmsave
 
3.配置my.cnf(可直接复制我的)
[root@Mysql8 ~]#vi /etc/my.cnf 
 
[mysqld]
##SERVER ID
server_id=1
##data directory
datadir=/mysql/data
##SOCKET & pid
socket=/tmp/mysql.sock
pid-file=/tmp/mysql.pid 
##logfile
log-error=/mysql/log/error.log
log_bin = /mysql/binlog/mysql-bin
binlog_format=ROW
##TRX mode
transaction-isolation = READ-COMMITTED
 
4.环境变量配置
[root@Mysql8 ~]# pwd
/root
 
添加以下环境变量
[root@Mysql8 ~]# vi .bash_profile 
 
PATH=$PATH:/usr/local/mysql/bin
 
生效当前环境变量
[root@Mysql8 ~]# source  .bash_profile 
 
查看生效结果 
[root@Mysql8 ~]#echo  $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin:/usr/local/mysql/bin:/root/bin:/usr/local/mysql/bin:/usr/local/mysql/bin
 
二.mysql 数据用户、文件目录初始化
 
1.创建mysql用户、组
 
[root@Mysql8 ~]#groupadd mysql
 
[root@Mysql8 ~]#useradd -r -g mysql -s /bin/false mysql
 
2.创建mysql数据文件目录权限
 
[root@Mysql8 ~]# mkdir -p /mysql/data  
[root@Mysql8 ~]# mkdir -p /mysql/log/
[root@Mysql8 ~]# mkdir -p /mysql/binlog/
[root@Mysql8 ~]# chown -R mysql:mysql  /mysql
[root@Mysql8 ~]# chmod -R 775 /mysql/
  
3.查看权限
 
[root@Mysql8 /]# ls -ld /mysql/
drwxrwxr-x. 5 mysql mysql 43 Mar 27 22:46 /mysql/
 
4.解压安装mysql server
 
将下载好的mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz文件放在目录/usr/local
 
[root@mysql local]#cd /usr/local
 
5.解压mysql压缩文件
 
[root@Mysql8 local]# tar xvf mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz 
 
6.创建mysql软链接文件
[root@Mysql8 local]# ln -s mysql-8.0.27-linux-glibc2.12-x86_64 mysql

7.创建mysql-file 赋权限
[root@Mysql8 local]# cd mysql

[root@Mysql8 local]#mkdir mysql-files
 
[root@Mysql8 local]#chown mysql:mysql mysql-files
 
[root@Mysql8 mysql]#chmod 750 mysql-files
 
 
三.初始化mysql server
 
1.初始化
[root@Mysql8 local]#cd /usr/local/mysql
 
[root@Mysql8 local]#bin/mysqld --initialize --user=mysql
 
2.配置ssl
[root@Mysql8 local]#bin/mysql_ssl_rsa_setup
 
3.使用mysqld_safe启动mysql
 
[root@Mysql8 local]#bin/mysqld_safe --user=mysql &
 
2022-03-27T15:56:14.655180Z mysqld_safe Logging to '/mysql/log/error.log'.
2022-03-27T15:56:14.687696Z mysqld_safe Starting mysqld daemon with databases from /mysql/data
 
4.查看3306端口,有3306端口证明mysql已经启动
[root@Mysql8 mysql]#  ss -ln |grep 3306
tcp    LISTEN     0      128      :::3306                 :::*                  
tcp    LISTEN     0      70       :::33060                :::* 

5.拷贝mysql.server启动文件到/etc/init.d/
[root@Mysql8 mysql]# cp support-files/mysql.server /etc/init.d/mysql.server
cp: overwrite ‘/etc/init.d/mysql.server’? y
 
四.查看密码
error log 目录在/mysql/log/中,进入/mysql/log/也可以查看error.log查找mysql初始化密码
 
[root@Mysql8 local]#cd /mysql/log/
[root@Mysql8 log]# tail -100 error.log
2022-03-27T15:55:58.235664Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Z=LILKr1.u%O
 
密码是随机码----> Z=LILKr1.u%O
 
五.登录mysql
 
使用Z=LILKr1.u%O密码登录
[root@Mysql8 log]# mysql -uroot -p
Enter password: 
 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27

Copyright (c) 2000, 2021, Oracle and/or its affiliates.
 
执行show database时会提示修改初始密码
 
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
 
 
六.修改初始化root密码
mysql密码的维度是用户名+主机,修改密码是需要根据用户名和主机的维度来一起修改
 
mysql> alter user 'root'@'localhost' identified by 'mysql123';
Query OK, 0 rows affected (0.01 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye

七.使用新修改的密码登录mysql
[root@Mysql8 mysql]# 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.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
 --安装结束!
 

你可能感兴趣的:(Linux系统编程,DataBase(数据库),dba,mysql,centos)