# 下载
$ wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# 安装 mysql 源
$ yum localinstall mysql57-community-release-el7-11.noarch.rpm
用下面的命令检查 mysql 源是否安装成功
$ yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 153
mysql-tools-community/x86_64 MySQL Tools Community 110
mysql57-community/x86_64 MySQL 5.7 Community Server 424
使用 yum install
命令安装
$ yum install -y mysql-community-server
启动/关闭服务的命令是 systemctl start|stop
$ systemctl start mysqld
用 systemctl status
查看 MySQL 状态
$ systemctl status mysqld
?.mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2020-06-18 10:38:20 CST; 38s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 13691 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 13629 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 13694 (mysqld)
CGroup: /system.slice/mysqld.service
?..13694 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Jun 18 10:38:13 VM_0_10_centos systemd[1]: Starting MySQL Server...
Jun 18 10:38:20 VM_0_10_centos systemd[1]: Started MySQL Server.
$ systemctl enable mysqld
# 重载所有修改过的配置文件
$ systemctl daemon-reload
mysql 安装完成之后,生成的默认密码在 /var/log/mysqld.log
文件中。使用 grep 命令找到日志中的密码。
$ grep 'temporary password' /var/log/mysqld.log
2020-06-18T02:38:15.404832Z 1 [Note] A temporary password is generated for root@localhost: kn;RYrESq9xz
首次通过初始密码登录后,使用以下命令修改密码:
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
flush privileges;
以后通过 update set 语句修改密码:
mysql> use mysql;
mysql> update user set password=PASSWORD('MyNewPass5!') where user='root';
mysql> flush privileges;
注意:mysql 5.7 默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 错误。
默认只允许root帐户在本地登录,如果要在其它机器上连接 mysql,必须添加一个允许远程连接的帐户。或者修改 root 为允许远程连接(不推荐) 。
添加一个允许远程连接的帐户:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'zuozewei'@'%' IDENTIFIED BY 'Zuozewei2020!' WITH GRANT OPTION;
修改 root 为允许远程连接(不推荐):
mysql> use mysql;
mysql> UPDATE user SET Host='%' WHERE User='root';
mysql> flush privileges;
mysql> quit
mysql 安装后默认不支持中文,需要修改编码。
修改 /etc/my.cnf
配置文件,在相关节点(没有则自行添加)下添加编码配置,如下:
$ vim /etc/my.cnf
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
重启 mysql 服务,查询编码:
$ systemctl restart mysqld
$ mysql -uroot -p
$ show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
/etc/my.cnf
/var/log/mysqld.log
/usr/lib/systemd/system/mysqld.service
/var/run/mysqld/mysqld.pid