1、CentOS7
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum localinstall -y mysql57-community-release-el7-8.noarch.rpm
yum install -y mysql-community-server
# 启动服务
systemctl start mysqld
# 停止服务
systemctl stop mysqld
# 重启服务
systemctl restart mysqld
# 服务状态
systemctl status mysqld
# 开机启动
systemctl enable mysqld
# 重载服务配置
systemctl daemon-reload
# 查看mysql初始密码
grep 'temporary password' /var/log/mysqld.log
# 连接mysql
mysql -uroot -p密码
# 修改密码【注意:后面的分号一定要跟上】
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码';
# 或
set password for 'root'@'localhost'=password('密码');
# 查看 mysql 初始的密码策略(已连接MySQL服务)
SHOW VARIABLES LIKE 'validate_password%';
# 设置密码策略为只验证密码长度(不验证特殊字符)
set global validate_password_policy=LOW;
# MySQL8.0以上
# set global validate_password.policy=LOW;
# 可设置为6位的密码
set global validate_password_length=6;
# MySQL8.0以上
# set global validate_password.length=6;
# 报错信息
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
# 关闭MySQL服务
systemctl stop mysqld
# 免密码登录:vim /etc/my.cnf,添加最后一行
skip-grant-tables
# 重启MySQL服务
systemctl restart mysqld
# 不使用密码登录MySQL
mysql -u root
# 刷新权限
mysql> flush privileges;
# 修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> flush privileges;
# 删除免密码登录:vim /etc/my.cnf,删除最后一行
skip-grant-table
# 重启MySQL服务
systemctl restart mysqld
whereis mysqld
默认配置文件路径:
配置文件:/etc/my.cnf
数据文件:/var/lib/mysql
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
vim /etc/my.cnf
# 配置默认编码为utf8,在[mysqld]下添加以下内容:
character_set_server=utf8
init_connect='SET NAMES utf8'
# 停止服务
systemctl stop mysqld
# 删除安装的包
yum erase $(rpm -qa | grep mysql-community-server)
# 删除配置文件
rm -rf /etc/my.cnf
# 删除数据文件
rm -rf /var/lib/mysql
# 删除日志文件
rm -rf /var/log/mysqld.log
2、Ubuntu16.04
sudo apt-get install mysql-server
# 启动
sudo service mysql start
# 停止
sudo service mysql stop
# 服务状态
sudo service mysql status
sudo vim/etc/mysql/mysql.conf.d/mysqld.cnf
# 添加该句:
character-set-server=utf8
sudo vim /etc/mysql/conf.d/mysql.cnf
# 添加以下:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation-server = utf8_general_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
# 修改完成后,重启mysql
sudo service mysql restart
ps aux | grep mysql
# 关闭守护进程mysql
sudo service mysql stop
# 卸载安装的软件包
sudo apt-get remove --purge mysql-server*
3、Windows10
# 安装目录
C:\PortableFiles\mysql-5.7.33-winx64\bin
[mysql]
default-character-set=utf8
[mysqld]
port = 3306
basedir=C:\PortableFiles\mysql-5.7.33-winx64
datadir=C:\PortableFiles\mysql-5.7.33-winx64\data
max_connections=1000
character-set-server=utf8
default-storage-engine=INNODB
# 安装
mysqld install
# 初始化
mysqld --initialize
# 安装MySQL的时候,在命令行中输入mysqld -install没有任何反应,可能是把vc_redist.x64.exe给卸载掉了
# 下载安装vc_redist.x64.exe
# https://docs.microsoft.com/zh-CN/cpp/windows/latest-supported-vc-redist?view=msvc-170
# 安全模式启动
mysqld --defaults-file="C:\PortableFiles\mysql-5.7.33-winx64\my.ini" --console --skip-grant-tables
# 另起终端
mysql -uroot -p
# 直接敲Enter键,成功进入mysql
use mysql;
update user
set authentication_string=password("123456")
where user = "root";
quit
-- 最后关闭安全模式启动的服务
重启MySQL服务
# 重启服务
net start mysql
mysql -uroot -p123456
-- 登录后,需要在正常模式下,再设一次root用户的密码
set password = password ('123456');
use mysql;
select host, user
from user;
update user
set host='%'
where user = 'root';
quit
# 启动服务
net start mysql
# 停止服务
net stop mysql
文章评论