二进制安装mysql5.7.18
# 删除centos7系统自带的mariadb
yum remove mariadb-libs-5.5.60-1.el7_5.x86_64 -y
rpm -qa |grep mariadb
# 创建mysql用户
useradd -s /sbin/nologin mysql
# 解压二进制包
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
tar xf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
ln -s mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql
mkdir -p /data/mysql/{data,sock}
chown -R mysql.mysql /data/mysql/
chown -R mysql.mysql /usr/local/mysql/
# 设置mysql环境变量
vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile
# --initialize 参数:
1. 对于密码复杂度进行定制:12位,4种
2. 密码过期时间:180
3. 给root@localhost用户设置临时密码
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
# 无限制,无临时密码
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data
# 配置文件的准备
cat >/etc/my.cnf <<EOF
[mysqld]
#skip-name-resolve 禁用域名解析,生产环境建议开启
character-set-server = utf8mb4
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/data
socket=/data/mysql/sock/mysql.sock
server_id=6
port=3306
[mysql]
default-character-set = utf8mb4
socket=/data/mysql/sock/mysql.sock
[client]
default-character-set = utf8mb4
EOF
# systemd启动方式
cat >/etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
# 启动服务
systemctl start mysqld
systemctl enable mysqld
# 管理员密码的设定
mysqladmin -S /data/mysql/sock/mysql.sock -uroot -p password
Enter password: 无密码,直接回车
New password: 新密码
Confirm new password: 新密码
管理员忘记密码
vim /etc/my.cnf
[mysqld]
skip-grant-tables # 跳过授权表
skip-networking # 禁止远程登录
systemctl restart mysqld
mysql # 无密码登录
mysql>update mysql.user set authentication_string=PASSWORD('centos') where user = 'root'; -- 修改密码为centos 5.7.18用的是authentication_string 旧的字段是password
mysql>FLUSH PRIVILEGES; -- 重加载授权表
或者
mysql>set PASSWORD for 'root'@'localhost' = PASSWORD('centos1'); -- 修改密码为centos1
mysql>FLUSH PRIVILEGES; -- 重加载授权表
删除my.cnf刚刚加上的两行
skip-grant-tables
skip-networking
重新启动就行了