Linux的Mysql8安装与启动
安装Mysql之前,先查看本机是否自带安装了mysql
安装rpm
yum install rpm
检查是否自带mysql
rpm -qa |grep mysql
如果系统已经安装了mysql,就需要先卸载旧版本
# 普通删除模式
rpm -e mysql
# 强力删除模式
rpm -e --nodeps mysql
使用yum命令下载Mysql
wget http://repo.mysql.com/mysql80-community-release-el7-5.noarch.rpm
rpm -ivh mysql80-community-release-el7-5.noarch.rpm
避免安装mysql时候报错,安装前添加
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
为了兼容SQL语句group by问题, 在/etc/my.cnf的【mysqld】添加如下
[mysqld] 在尾部追加一行
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
安装mysql
yum install mysql-server
安装完成之后需要给mysql添加授权,且MySQL不能用root作为用户以及用户组 mysql.mysql的意思是: 用户.用户组
chown -R mysql.mysql /var/lib/mysql/
chmod 777 -R /var/lib/mysql/
初始化mysql
mysqld --initialize
启动mysql
systemctl start mysqld 或者 service mysqld start
如果启动不了就再刷新一下 mysql权限设置:
chown -R mysql.mysql /var/lib/mysql/
查看mysql是否是开机自启
systemctl is-enabled mysqld
设置mysql开机自启
systemctl enable mysqld
查看mysql版本
mysqladmin --version
查看root的临时密码:
[root@node1 ~]# grep "A temporary password" /var/log/mysqld.log
2023-01-06T10:19:15.489917Z 1 [Note] A temporary password is generated for root@localhost: r:f:7;x0wcV<
如果查看不了临时密码或者临时密码失效
先在etc/my.cnf文件添加 skip_grant_tables;
[root@node1 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5430
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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 '123456';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.01 sec)
更改临时密码,更改用户root密码为123456
[root@node1 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5430
Server version: 8.0.31 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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 '123456';
添加授权所有远程用户链接
1:查看mysql数据库下的表user
2:修改root用户的Host为&,并且拥有全部权限
2:如果最终远程链接不上必须要注意看字段plugin信息修改为支持navicat链接
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| jeecg-boot |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.06 sec)
mysql> use mysql;
Database changed
mysql> select Host,User,plugin from user;
+-----------+------------------+-----------------------+
| Host | User | plugin |
+-----------+------------------+-----------------------+
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
| localhost | root | caching_sha2_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
以上过程若有不对,欢迎指正哈😊