1.下载mysql
2.删除MySql服务
使用管理员身份打开cmd,切换到mysql解压的bin目录,运行命令:
D:\MySql\bin>sc delete MySql
3.新建my.ini配置文件
在mysql程序的根目录下,新建一个my.ini空白文件,用记事本打开,将以下内容复制进去,保存:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = D:\MySQL
datadir = D:\MySQL\data
port = 3306
# server_id = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 128M
sort_buffer_size = 16M
read_rnd_buffer_size = 16M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir和datadir根据具体mysql解压的路径而定
4.重新生成data文件
这个操作会删除之前生成的data文件,如果有重要的数据表,请先备份好
回到cmd,重新生成data文件。运行:
D:\MySql\bin>mysqld --initialize-insecure --user=mysql
命令执行成功后mysql文件夹下会重新生成data文件夹
5.重新安装mysql服务,同时绑定my.ini配置文件
安装MySql服务,同时设置绑定my.ini配置文件。命令:
D:\MySql\bin>mysqld --install "MySql" --defaults-file="d:/mysql/my.ini"
6.启动服务
cmd命令:
D:\MySql\bin>net start mysql
7.重新设置密码
登录mysql:
D:\MySql\bin>mysql -u root -p
第一次登录不需要输密码,直接回车
修改root密码:
update mysql.user set authentication_string=password("你的密码") where user="root";
flush privileges
退出mysql
mysql> exit
重新使用新密码登录
8.后记-mysql修改用户密码的方法及命令
方法1: 用SET PASSWORD命令
首先登录MySQL。
格式:mysql> set password for 用户名@localhost = password('新密码');
例子:mysql> set password for root@localhost = password('123');
方法2:用mysqladmin
格式:mysqladmin -u用户名 -p旧密码 password 新密码
例子:mysqladmin -uroot -p123456 password 123
方法3:用UPDATE直接编辑user表
首先登录MySQL。
mysql> use mysql;
mysql> update user set password=password('123') where user='root' and host='localhost';
mysql> flush privileges;
方法4:在忘记root密码的时候,可以这样
以windows为例:
1. 关闭正在运行的MySQL服务。
2. 打开DOS窗口,转到mysql\bin目录。
3. 输入mysqld --skip-grant-tables 回车。--skip-grant-tables 的意思是启动MySQL服务的时候跳过权限表认证。
4. 再开一个DOS窗口(因为刚才那个DOS窗口已经不能动了),转到mysql\bin目录。
5. 输入mysql回车,如果成功,将出现MySQL提示符 >。
6. 连接权限数据库: use mysql; 。
6. 改密码:update user set password=password("123") where user="root";(别忘了最后加分号) 。
7. 刷新权限(必须步骤):flush privileges; 。
8. 退出 quit。
9. 注销系统,再进入,使用用户名root和刚才设置的新密码123登录。