以下内容纯属个人操作遇到的问题记录,如有不同,请另行查阅
1、安装MySQL5.7
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server
2.查看MySQL等
[root@VM-4-6-centos ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.39, for Linux (x86_64) using EditLine wrapper
#启动mysql 并查看状态
[root@VM-4-6-centos ~]# systemctl enable mysqld
[root@VM-4-6-centos ~]# systemctl start mysqld.service
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@VM-4-6-centos ~]#
[root@VM-4-6-centos ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: activating (start-pre) since Sun 2022-09-25 23:10:17 CST; 1s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 470457 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPT>
Cntrl PID: 470478 (mysqld_pre_syst)
Tasks: 2 (limit: 10810)
Memory: 124.2M
CGroup: /system.slice/mysqld.service
├─470478 /bin/bash /usr/bin/mysqld_pre_systemd
└─470498 /usr/libexec/platform-python -Es /usr/sbin/semanage fcontext -a -e /var/lib/mysql /var>
Sep 25 23:10:17 VM-4-6-centos systemd[1]: Stopped MySQL Server.
Sep 25 23:10:17 VM-4-6-centos systemd[1]: Starting MySQL Server...
#启动发现报错
[root@VM-4-6-centos ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
此处原因多种,这种只是自己的解决办法,如未解决,请自行查阅
[root@VM-4-6-centos ~]# chown -R mysql.mysql /var/run/mysqld
[root@VM-4-6-centos ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@VM-4-6-centos ~]# rm -rf /var/lib/mysql/
[root@VM-4-6-centos ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
[root@VM-4-6-centos ~]# systemcyl status mysqld.service
-bash: systemcyl: command not found
[root@VM-4-6-centos ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-09-25 23:29:17 CST; 17s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 476943 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPT>
Process: 476879 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 476945 (mysqld)
Tasks: 27 (limit: 10810)
Memory: 338.4M
CGroup: /system.slice/mysqld.service
└─476945 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Sep 25 23:29:11 VM-4-6-centos systemd[1]: Starting MySQL Server...
Sep 25 23:29:17 VM-4-6-centos systemd[1]: Started MySQL Server.
登录失败后,设置vim /etc/my.cnf 添加 skip-grant-tables在 [mysqld] 下方
保存后,重启MySQL
[root@VM-4-6-centos ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@VM-4-6-centos ~]# vim /etc/my.cnf
[root@VM-4-6-centos ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@VM-4-6-centos ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@VM-4-6-centos ~]# systemctl restart mysqld
[root@VM-4-6-centos ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.39 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>
修改密码等
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user, host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)
mysql> ^C
mysql> update user set host = '%' where user = 'root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select user, host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)
mysql> show variables like 'sql_safe_updates';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| sql_safe_updates | OFF |
+------------------+-------+
1 row in set (0.00 sec)
mysql> set sql_safe_updates = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> update user set authentication_string='' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'root'@'%' identified by 'Xxxxxxxx#123';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
设置完密码后,删除 vim /etc/my.cnf 的 skip-grant-tables 后重启MySQL
注意要开放3306端口
如果报错:
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'
-- 查询一下安全模式开关
show variables like 'sql_safe_updates';
-- 关闭安全模式
set sql_safe_updates = 0;
-- 关闭安全模式下,才可以执行authentication_string为空
update user set authentication_string='' where user='root';
-- authentication_string空了的情况下,才可以真正修改密码
-- 刷新权限表
flush privileges;
-- 修改密码
alter user 'root'@'%' identified by 'S32*sdf312@';
select user, host from user;