检查之前是否卸载干净mysql,参考之前的一篇博客
1.下载安装文件
http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.7/ mysql5.7的 各个版本,选择linux-glibc2.12-x86_64版本
[root@iZ23hdndo4vZ tmp]# wget http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
2.解压安装包并重命名
[root@iZ23hdndo4vZ tmp]# tar -xzvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@iZ23hdndo4vZ local]# mv mysql-5.7.23-linux-glibc2.12-x86_64 mysql
3.在/user/local/mysql下创建data目录
[root@iZ23hdndo4vZ mysql]# mkdir data
4.创建用户mysql
useradd -M -s /sbin/nologin mysql
5.安装mysql服务
同时会生成初始的root密码
[root@iZ23hdndo4vZ bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US
2018-09-14T01:29:19.943304Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-14T01:29:22.889460Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-14T01:29:23.344284Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-14T01:29:23.467257Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9c5a6727-b7bd-11e8-950d-00163e000746.
2018-09-14T01:29:23.478545Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2018-09-14T01:29:23.481101Z 1 [Note] A temporary password is generated for root@localhost: WtuINdwKl9/I
[root@iZ23hdndo4vZ bin]#
关于随机密码,有的说会在root目录下生产一个随机的密码,文件名为 .mysql_secret。但如果找不到改文件或者初始化密码没有保存到文件中,而且发现忘记初始密码,可以使用下面两种方案处理:
方法一:
MySQL提供跳过访问控制的命令行参数,通过在命令行以此命令启动MySQL服务器:
safe_mysqld --skip-grant-tables&
即可跳过MySQL的访问控制,任何人都可以在控制台以管理员的身份进入MySQL数据库。
需要注意的是在修改完密码以后要把MySQL服务器停掉重新启动才会生效,先找到mysql.server 然后停止mysql服务。
mysqld_safe --skip-grant-tables --skip-networking &
mysql
use mysql;
update user set password=PASSWORD(“new-password”) where user=“root”;
flush privileges;
【Mysql 5.7 忘记root密码或重置密码的详细方法】
1、修改配置文件my.cnf 按i编辑
在[mysqld]中添加
skip-grant-tables
例如:
[mysqld]
skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
键盘 Esc 保存修改 :wq退出。
2、重启mysql服务
service mysqld restart
3、用户登录
mysql -uroot -p (直接点击回车,密码为空)
选择数据库
use mysql;
下面我们就要修改密码了
以前的版本我们用的是以下修改
update user set password=password(‘root’) where user=‘root’;
但是在5.7版本中不存在password字段,所有我们要用以下修改进行重置密码
update user set authentication_string=password(‘123456’) where user=‘root’;
执行
flush privileges;
4、退出mysql
quit;
5、将最开始修改的配置文件my.cnf中的skip-grant-tables删除
6、重启mysql
7、当你登陆mysql之后你会发现,当你执行命令时会出现
ERROR 1820 (HY000): You must reset your password using ALTER USER statement;
这是提示你需要修改密码
当你执行了
SET PASSWORD = PASSWORD(‘123456’);
如果执行成功后面的就不要看了,纯属浪费时间!
如果出现:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
你需要执行两个参数来把mysql默认的密码强度的取消了才行
set global validate_password_policy=0; set global validate_password_mixed_case_count=2;
这时你再执行
SET PASSWORD = PASSWORD(‘123456’);
6.设置开机启动
[root@iZ23hdndo4vZ mysql]# ls
bin COPYING data docs include lib man README share support-files
[root@iZ23hdndo4vZ mysql]# cd support-files/
[root@iZ23hdndo4vZ support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@iZ23hdndo4vZ support-files]# cp mysql.server /etc/init.d/mysql
[root@iZ23hdndo4vZ support-files]# chmod +x /etc/init.d/mysql
[root@iZ23hdndo4vZ support-files]# chkconfig --add mysql
[root@iZ23hdndo4vZ support-files]# chkconfig --list mysql
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
7.mysql 服务开启
[root@iZ23hdndo4vZ support-files]# service mysql start
Starting MySQL [ OK ]
8.进入mysql中
密码为第四步中生成的初始化密码
[root@iZ23hdndo4vZ bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23
Copyright © 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> set password=password(‘修改密码’);
Query OK, 0 rows affected, 1 warning (0.00 sec)
9.给外网ip配置访问权限
mysql> grant all privileges on . to ‘root’@’%’ identified by ‘hw@@2018’;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
其他
1.mysql -u root -p -bash:mysql:command not found 时解决方式
[root@iZ23hdndo4vZ bin]# mysql -u root -p
-bash: mysql: command not found
[root@iZ23hdndo4vZ bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin
[root@iZ23hdndo4vZ bin]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright © 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>
2.修改查看mysql的最大连接数
mysql> show variables like ‘max_connections’;
±----------------±------+
| Variable_name | Value |
±----------------±------+
| max_connections | 151 |
±----------------±------+
1 row in set (0.00 sec)
mysql> set globla max_connections=500;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘max_connections=500’ at line 1
mysql> set global max_connections=500;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘max_connections’;
±----------------±------+
| Variable_name | Value |
±----------------±------+
| max_connections | 500 |
±----------------±------+
1 row in set (0.00 sec)
3.查看mysql的所有远程连接情况
mysql> show processlist;
±—±-----±-------------------±-----±--------±-----±---------±-----------------+
| Id | User | Host | db | Command | Time | State | Info |
±—±-----±-------------------±-----±--------±-----±---------±-----------------+
| 6 | root | 112.17.87.192:2859 | NULL | Sleep | 821 | | NULL |
| 7 | root | localhost | NULL | Query | 0 | starting | show processlist |
±—±-----±-------------------±-----±--------±-----±---------±-----------------+
2 rows in set (0.00 sec)
mysql>
4.创建数据库导入sql脚本
mysql> create database ds9;
Query OK, 1 row affected (0.00 sec)
mysql> use ds9;
Database changed
mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> source /data/temp/ds9_v1.0_20180523.sql
Query OK, 0 rows affected (0.07 sec)
Query OK, 0 rows affected (0.07 sec)
Query OK, 0 rows affected (0.08 sec)
Query OK, 0 rows affected (0.06 sec)
Query OK, 0 rows affected (0.07 sec)
Query OK, 0 rows affected (0.07 sec)
Query OK, 0 rows affected (0.08 sec)
Query OK, 0 rows affected (0.06 sec)
Query OK, 0 rows affected (0.06 sec)
Query OK, 0 rows affected (0.08 sec)
Query OK, 0 rows affected (0.06 sec)
Query OK, 0 rows affected (0.05 sec)
Query OK, 0 rows affected (0.05 sec)
版权声明:本文为CSDN博主「MR_ROBIT」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hwhanwan/article/details/82697201