MySQL8安装&启停&修改密码

108 阅读4分钟

下载

链接:cdn.mysql.com/archives/my…

将下载的 mysql 通过文件传输工具移到 /usr/local

创建目录并解压 : mkdir mysql && tar -xvf mysql-8.0.26-1.el7.x86_64.rpm-bundle.tar -C mysql

[root@localhost mysql]# ll
总用量 789340
-rw-r--r--. 1 7155 31415  47836256 7月   2 2021 mysql-community-client-8.0.26-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   4694644 7月   2 2021 mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415    634632 7月   2 2021 mysql-community-common-8.0.26-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   6806272 7月   2 2021 mysql-community-devel-8.0.26-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415  23638156 7月   2 2021 mysql-community-embedded-compat-8.0.26-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   4243808 7月   2 2021 mysql-community-libs-8.0.26-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   1264168 7月   2 2021 mysql-community-libs-compat-8.0.26-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 454689776 7月   2 2021 mysql-community-server-8.0.26-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 264457624 7月   2 2021 mysql-community-test-8.0.26-1.el7.x86_64.rpm

安装

[root@localhost mysql]# rpm -ivh mysql-community-common-8.0.26-1.el7.x86_64.rpm 

[root@localhost mysql]# rpm -ivh mysql-community-client-plugins-8.0.26-1.el7.x86_64.rpm

[root@localhost mysql]# rpm -ivh mysql-community-libs-8.0.26-1.el7.x86_64.rpm 
# yum remove mysql-libs -y

[root@localhost mysql]# rpm -ivh mysql-community-libs-compat-8.0.26-1.el7.x86_64.rpm

[root@localhost mysql]# rpm -ivh mysql-community-devel-8.0.26-1.el7.x86_64.rpm 
# yum install openssl-devel -y

[root@localhost mysql]# rpm -ivh mysql-community-client-8.0.26-1.el7.x86_64.rpm 


[root@localhost mysql]# rpm -ivh mysql-community-server-8.0.26-1.el7.x86_64.rpm 

启停服务

systemctl start mysqld

systemctl restart mysqld

systemctl stop mysqld

查看自动生成的密码 grep 'temporary password' /var/log/mysqld.log

[root@localhost mysql]# mysql -uroot -p

修改密码

mysql> set global validate_password.policy =0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password.length=6;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER USER() IDENTIFIED BY '123123';
Query OK, 0 rows affected (0.11 sec)

忘记密码

第一步 :跳过 MySQL 的密码认证过程

[root@localhost bin]# vim /etc/my.cnf

[mysqld]后面任意一行添加skip-grant-tables用来跳过密码验证的过程

第二步 :重启 mysql

[root@localhost mysql57]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!

第三步 :登录 mysql

进入mysql/bin目录,启动 mysql

[root@localhost bin]# ./mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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>

第四步 :使用 sql 语句修改密码

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 authentication_string=password("123") where user="root";
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

第五步 :重新编辑 my.cnf

去掉[mysqld]后面的skip-grant-tables并重启 mysql

[root@localhost bin]# vim /etc/my.cnf
[root@localhost bin]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost bin]# ./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.21

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

需要重新修改一下密码

mysql> alter user 'root'@'localhost' identified by '123123';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

配置账号权限

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>  CREATE USER 'dduser'@'localhost' IDENTIFIED BY '123123';
Query OK, 0 rows affected (0.09 sec)

mysql> grant ALL PRIVILEGES on *.* to 'dduser'@'localhost';
Query OK, 0 rows affected (0.12 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

重启 mysql

[root@localhost bin]# service mysql restart;
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!

设置防火墙

  • 配置防火墙开启 3306 端口
[root@localhost bin]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[root@localhost bin]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:   [  OK  ]
[root@localhost bin]# /etc/rc.d/init.d/iptables restart
iptables: Setting chains to policy ACCEPT: filter         	[  OK  ]
iptables: Flushing firewall rules:                     	[  OK  ]
iptables: Unloading modules:                       	[  OK  ]
iptables: Applying firewall rules:                    	[  OK  ]
  • 临时关闭防火墙
[root@localhost bin]# service iptables stop
  • 永久关闭防火墙 重启后永久生效
[root@localhost bin]# chkconfig iptables off