配置文件

image-20231228173015131

image-20231228173015131

[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=D:\\MYSQL
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
#datadir=D:\\MYSQL\\data
#数据安全导出
secure_file_priv =""
# 允许最大连接数
max_connections=200
#允许连接失败的次数
max_connect_errors=10
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=UTF8MB4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#默认使用mysql_native_password插件认证
#defaul_authentication_plugin=mysql_native_password

[mysql]
default-character-set=UTF8MB4
[client]
port=3306
#客户端默认字符集
default-character-set=UTF8MB4

Windows10

①安装服务:
mysqld --install
②初始化: 
mysqld --initialize --console
③开启服务:
net start mysql
④关闭服务:
net stop mysql
⑤登录mysql:
mysql -u root -p

  Enter PassWord:(密码)

⑥修改密码:(by 接着的是密码)空着不填即为无密码
alter user 'root'@'localhost' identified by '';
⑦标记删除mysql服务:
sc delete mysql

Linux

下载软件

https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar

注:CentOS 对应版本为 Red Hat 版本。

清理旧版本

检测本地是否有已存在 mysql 的包:

rpm -qa | grep mysql

如果存在 MySQL 版本,请使用命令 rpm -e --nodeps {-file-name} 进行移除操作:

rpm -e --nodeps mysql-community-libs-8.0.22-1.el7.x86_64

查找相关目录:

find / -name mysql

删除相关目录:

rm -rf /var/lib/mysql

进入安装包目录:

cd /usr/local/mysql
解压安装包:
tar -xvf mysql-8.0.22-1.el7.x86_64.rpm-bundle.tar
依次安装:
rpm -ivh mysql-community-common-8.0.22-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.22-1.el7.x86_64.rpm  --force --nodeps
rpm -ivh mysql-community-libs-compat-8.0.22-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.22-1.el7.x86_64.rpm  --force --nodeps
rpm -ivh mysql-community-server-8.0.22-1.el7.x86_64.rpm  --force --nodeps
rpm -qa | grep mysql

这里会有一些依赖错误出现,原则上是缺啥装啥

查看错误日志

systemctl status mysqld.service
和
journalctl -xe
yum install openssl-devel
yum install -y libaio


yum remove mysql-libs
再安装
rpm -ivh mysql-community-libs-compat-8.0.22-1.el7.x86_64.rpm
启动 MySQL
启动服务:
systemctl start mysqld
查看服务状态:
systemctl status mysqld.service

设置登录密码
查询默认临时密码:

cat /var/log/mysqld.log | grep password

登录 MySQL 服务:

mysql -uroot -p

这里输入默认临时密码。

更改密码

之前,要设置一下密码登记,否则密码设置过于简单会失败:

密码检查等级,0/LOW、1/MEDIUM、2/STRONG

set global validate_password.policy=0;

密码的最短长度

set global validate_password.length=6;

密码至少要包含的小写字母个数和大写字母个数

set global validate_password.mixed_case_count=0;

设置密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '';
  1. 修改登录权限
    修改远程登录权限
USE mysql;
UPDATE mysql.user SET host = '%' WHERE user = 'root';
flush privileges;
SELECT host, user FROM user;

Ubuntu系统

1.安装mysql服务端

apt update
sudo apt install mysql-server



# 可使用密码配置脚本
sudo mysql_secure_installation

系统将要求您配置VALIDATE PASSWORD PLUGIN用来测试MySQL用户密码强度并提高安全性的密码:

mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:    #输入步骤4中设置的root用户密码
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration of the component.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y   #是否更改root用户密码,输入Y

New password:  #设置新的root用户密码

Re-enter new password:   #再次输入密码

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y  #确认使用已设置的密码,输入Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y   #是否删除匿名用户,输入Y
Success.

Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y   #禁止root远程登录,输入Y
Success.

By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y   #是否删除test库和对它的访问权限,输入Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y   #是否重新加载授权表,输入Y
Success.

All done!
修改密码
mysql

USE mysql;
# 完成后登录并开启远程登录
# 此处由于修改了host = '%'因此后面修改用户信息时应使用'root'@'%'
UPDATE mysql.user SET host = '%' WHERE user = 'root';
flush privileges;
SELECT host, user FROM user;


update user set plugin='mysql_native_password' where user='root'; # 修改其密码格式
select user,plugin,Grant_priv,authentication_string from user; # 查询其用户
flush privileges;
alter user 'root'@'%' identified with mysql_native_password by '';

sudo sed -i 's/^bind-address/#bind-address/' /etc/mysql/mysql.conf.d/mysqld.cnf
service mysql restart
添加新用户
# 添加新用户并设置密码
create user if not exists 'user'@'%' identified with mysql_native_password by '';


# 赋权所有权限
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;

忘记密码

停止MySQL服务

windows

net stop mysql
开启跳过密码验证登录的MySQL服务
mysqld --console --skip-grant-tables --shared-memory 

登录密码置为空

use mysql

update user set authentication_string='' where user='root';
关闭之后再修改密码即可

ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password';

配置文件

vim /etc/my.cnf

设置跳过密码验证

[mysqld]
skip-grant-tables

重启服务

service mysqld restart

登录mysql

mysql -u root -p

登录密码置为空

use mysql

update user set authentication_string='' where user='root';

配置文件

vim /etc/my.cnf

删除skip-grant-tables

删除skip-grant-tables

登录mysql

mysql -u root -p

修改密码

use mysql

ALTER USER 'root'@'%' IDENTIFIED BY 'your password';
最后修改:2024 年 07 月 26 日
反正没人给,你也爱给不给吧。