CentOS安装MySQL及其使用(总结整理)


安装MySQL


1先检查系统是否装有mysql

rpm -qa | grep mysql

2 下载mysql的repo源

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

如果报错: -bash: wget: 未找到命令,安装插件yum -y install wget

3 安装 mysql-community-release-el7-5.noarch.rpm包

sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

4 安装MySQL

sudo yum install mysql-server

5 重启MySQL服务

service mysqld start

6 查看MySQL是否启动成功

service mysqld status
netstat -nltp | grep mysqld


修改root密码(忘记root密码)


1 停止mysql服务

service mysqld stop

2 修改配置文件/etc/my.cnf

打开/etc/my.cnf,
在[mysqld]下添加skip-grant-tables,意思是忽略密码,保存并退出。

3 启动Mysql

service mysqld start

4 修改密码

输入命令mysql // 步骤1

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 3 to server version: 3.23.56
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> USE mysql; // 步骤2
Database changed
mysql> UPDATE user SET Password = password ('new-password') WHERE User = 'root'; // 步骤3
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0
mysql> flush privileges; // 步骤4 
Query OK, 0 rows affected (0.01 sec)
mysql> quit // 步骤5

5 改回配置,重启服务

打开/etc/my.cnf,
在[mysqld]下删除skip-grant-tables,保存并退出。

重启mysql,
service mysqld restart。


外网无法连接到MySQL的处理方法


使用mysql -u root -p登录到mysql中。

登录后执行如下命令:
use mysql;
select host from user where user=‘root’;

修改帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入MySQL后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从"localhost"改称"%":
update user set host = ‘%’ where user =‘root’;

刷新配置即可。
flush privileges;


参考资料


CentOS7.5 安装MySql的教程
CentOS下mysql忘记root密码解决办法
is not allowed to connect to this mysql server

Guess you like

Origin blog.csdn.net/xp178171640/article/details/118916058