CentOS7 安装MySQL
官方安装教程:
https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html
1.查看是否已安装
1.查看系统中是否已安装 MySQL 服务
$ rpm -qa | grep mysql
或
$ yum list installed | grep mysql
2.如果已安装则删除 MySQL 及其依赖的包
$ yum -y remove mysql-libs.x86_64
2.下载源
1.下载 mysql57-community-release-el7-8.noarch.rpm 的 YUM 源:
$ wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
2.安装 mysql57-community-release-el7-8.noarch.rpm:
$ yum localinstall mysql57-community-release-el7-8.noarch.rpm
3.检查mysql源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"
[root@sinotest1 jenkins]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 51
mysql-tools-community/x86_64 MySQL Tools Community 63
mysql57-community/x86_64 MySQL 5.7 Community Server 267
4.安装
1.安装 MySQL:
yum -y install mysql-community-server
安装失败(解决)
转载链接: https://blog.csdn.net/enterpc/article/details/122702133
wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
#执行这条语句的时候报错
yum -y install mysql-community-server
$> gpg --export -a 3a79bd29 > 3a79bd29.asc
$> rpm --import 3a79bd29.asc
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
启动mysql:
systemctl start mysqld
2.查找初始密码:
mysql安装完成之后,在 /var/log/mysqld.log
文件中给root生成了一个默认密码
grep 'temporary password' /var/log/mysqld.log
打印如下内容:
[root@sinotest1 jenkins]# grep 'temporary password' /var/log/mysqld.log
2018-06-07T02:46:39.003245Z 1 [Note] A temporary password is generated for root@localhost: BwE7u(JnJL%*
使用默认密码登录mysql进行修改root密码
[root@sinotest1 jenkins]# mysql -u root -p
Enter password:
修改root用户密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
或者
mysql> set password for 'root'@'localhost'=password('123456');
注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误。
使用符合规定的密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'P@ssw0rd';
通过msyql环境变量可以查看密码策略的相关信息:
mysql> show variables like '%password%';
mysql> show variables like '%password%';
+---------------------------------------+--------+
| Variable_name | Value |
+---------------------------------------+--------+
| default_password_lifetime | 0 |
| disconnect_on_expired_password | ON |
| log_builtin_as_identified_by_password | OFF |
| mysql_native_password_proxy_users | OFF |
| old_passwords | 0 |
| report_password | |
| sha256_password_proxy_users | OFF |
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+---------------------------------------+--------+
密码策略 | 策略说明 |
---|---|
validate_password_policy | 密码策略,默认为MEDIUM策略 |
validate_password_dictionary_file | 密码策略文件,策略为STRONG才需要 |
validate_password_length | 密码最少长度 |
validate_password_mixed_case_count | 大小写字符长度,至少1个 |
validate_password_number_count | 数字至少1个 |
validate_password_special_char_count | 特殊字符至少1个 |
上述参数是默认策略MEDIUM的密码检查规则。
策略 | 检查规则 |
---|---|
0 or LOW | Length |
1 or MEDIUM | Length; numeric, lowercase/uppercase, and special characters |
2 or STRONG | Length; numeric, lowercase/uppercase, and special characters; dictionary file |
修改密码策略
vim /etc/my.cnf
文件添加validate_password_policy配置,指定密码策略
# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件
validate_password_policy=0
如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:
vim /etc/my.cnf
validate_password = off
重新启动mysql服务使配置生效:
systemctl restart mysqld
重新登录mysql
[root@sinotest1 jenkins]# mysql -u root -p
Enter password:
修改成简单密码:
mysql> set password for 'root'@'localhost'=password('123456');
5.设置
1.设置访问权限
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户。
设置用户 root 可以在任意 IP 下被访问:
grant all privileges on *.* to root@"%" identified by "新密码";
设置用户 root 可以在本地被访问:
grant all privileges on *.* to root@"localhost" identified by "新密码";
刷新权限使之生效:
flush privileges;
为了安全起见,添加一个新的帐户:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'jeiker'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
使用新账号登录:
mysql -u jeiker -p
6、配置默认编码为utf8
修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:
vim /etc/my.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
重新启动mysql服务使配置生效:
systemctl restart mysqld
查看数据库默认编码:
mysql> show variables like '%character%';
默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
7.查看数据库
查看 MySQL 当前都内置了哪些数据库:
mysql> show databases;
我们发现其内置了如下一些数据库:
information_schema
mysql
performance_schema
sys
8.服务命令
1.启动 MySQL 服务:
$ systemctl start mysqld
2.开机运行服务
$ systemctl enable mysqld
3.查询服务是否开机启动
$ systemctl is-enabled mysqld
4.重新加载服务配置文件
$ systemctl reload mysqld
5.关闭 MySQL 服务:
$ systemctl stop mysqld
6.重启 MySQL 服务:
$ systemctl restart mysqld
7.查看 MySQL 的状态:
$ systemctl status mysqld
8.取消开机运行
$ systemctl disable mysqld