安装过程:
参考:https://blog.csdn.net/qq_45103475/article/details/123151050
查找mysql
[root@bogon ~]# whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql
删除目录
[root@bogon ~]# rm -rf /usr/lib64/mysql
[root@bogon ~]# whereis mysql
mysql: /usr/share/mysql
[root@bogon ~]# rm -rf /usr/share/mysql/
[root@bogon ~]# whereis mysql
mysql:[root@bogon ~]#
下载 mysql
[root@bogon ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
-bash: wget: 未找到命令
安装wget
[root@bogon ~]# yum install wget
[root@bogon ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[root@bogon ~]# yum -y install mysql57-community-release-el7-11.noarch.rpm
[root@bogon ~]#
[root@bogon ~]# yum repolist enabled | grep mysql.*
mysql-connectors-community/x86_64 MySQL Connectors Community 242
mysql-tools-community/x86_64 MySQL Tools Community 104
mysql57-community/x86_64 MySQL 5.7 Community Server 696
[root@bogon ~]# yum install mysql-community-server
[root@bogon ~]#
[root@bogon ~]# yum install mysql-community-server
已加载插件:fastestmirror
Determining fastest mirrors
* base: mirrors.ustc.edu.cn
* extras: mirrors.ustc.edu.cn
.......
[root@bogon ~]# systemctl start mysqld.service
[root@bogon ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2024-04-24 11:10:30 CST; 26s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 3802 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 3751 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 3805 (mysqld)
CGroup: /system.slice/mysqld.service
└─3805 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
4月 24 11:10:26 bogon systemd[1]: Starting MySQL Server...
4月 24 11:10:30 bogon systemd[1]: Started MySQL Server.
[root@bogon ~]#
[root@bogon ~]# grep "password" /var/log/mysqld.log
2024-04-24T03:10:27.639948Z 1 [Note] A temporary password is generated for root@localhost: -UecG(wsX7LT
#密码:-UecG(wsX7LT
[root@bogon ~]#
[root@bogon ~]# 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.44
Copyright (c) 2000, 2023, 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> grant all privileges on *.* to 'root'@'%' identified by 'Pass@123' with grant option;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> exit
Bye
[root@bogon ~]# firewall-cmd --state
running
[root@bogon ~]#
[root@bogon ~]# firewall-cmd --list-ports
[root@bogon ~]# firewall-cmd --add-port=3306/tcp --permanent
success
[root@bogon ~]# firewall-cmd --reload
success
[root@bogon ~]# firewall-cmd --list-ports
3306/tcp
[root@bogon ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.44
Copyright (c) 2000, 2023, 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>
mysql> status
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Pass@123';
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> status
--------------
mysql Ver 14.14 Distrib 5.7.44, for Linux (x86_64) using EditLine wrapper
Connection id: 4
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.44
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 14 min 57 sec
Threads: 1 Questions: 12 Slow queries: 0 Opens: 107 Flush tables: 1 Open tables: 100 Queries per second avg: 0.013
--------------
mysql>
修改my.cnf 配置文件(文件路径/etc/my.cnf)
[client]
default-character-set=utf8
character-set-server=utf8
collation-server=utf8_general_ci
[root@bogon ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
[root@bogon ~]#
解决:HeidiSql 连接数据库提示: Host ‘x.x.x.x’ is not allowed to connect to this MySQL server
参考:https://blog.csdn.net/m0_50836836/article/details/135954909
依次执行一下命令:
mysql -u root -P 3306 -p
use mysql;
#切换到mysql 数据库(mysql 数据库是一个特殊的系统数据库,它包含了许多关于 MySQL 服务器自身的信息,如表结构、用户权限、存储过程等。通常,直接在这个数据库中进行修改需要谨慎,因为不当的操作可能会影响到 MySQL 服务器的正常运行。)
update user set host = '%' where user = 'root';
解释:这条命令是更新 user 表中的记录,将 user 字段为 root 的用户的 host 字段设置为 %。在 MySQL 中,host 字段决定了哪些主机可以连接到该用户。% 是一个通配符,表示任何主机都可以连接。因此,这条命令的效果是允许 root 用户从任何主机连接到 MySQL 服务器。
flush privileges;
解释:这条命令是用来重新加载权限表的。在 MySQL 中,当你修改了权限相关的表(如 user 表)后,需要执行这条命令来使修改生效。如果不执行 FLUSH PRIVILEGES;,那么即使你修改了权限表,MySQL 也不会立即识别这些更改,直到下一次服务器重启。
运行过程
[root@localhost ~]# mysql -u root -P 3306 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, 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> 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> update user set host = '%' 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> exit
效果: