一、安装mysql源
1、创建mysql目录 /home/mysql
2、进入mysql目录 cd /home/mysql
3、下载mysql源安装包(可在http://dev.mysql.com/downloads/repo/yum/ 获取源安装包地址)
wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@172 mysql]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
Length: 25680 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-11.noarch.rpm’
100%[================ ======>] 25,680 --.-K/s in 0.1s
2017-12-13 09:49:40 (250 KB/s) - ‘mysql57-community-release-el7-11.noarch.rpm’ saved [25680/25680]
4、安装mysql源
[root@172 mysql]# yum localinstall mysql57-community-release-el7-11.noarch.rpm
Loaded plugins: fastestmirror, langpacks
.......
Total size: 31 k
Installed size: 31 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql57-community-release-el7-11.noarch 1/1
Verifying : mysql57-community-release-el7-11.noarch 1/1
Installed:
mysql57-community-release.noarch 0:el7-11
Complete!
5、检查mysql源安装情况
[root@172 mysql]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 42
mysql-tools-community/x86_64 MySQL Tools Community 55
mysql57-community/x86_64 MySQL 5.7 Community Server 227
执行命令后返回上述结果表示mysql源安装成功。
二、安装mysql
1、安装mysql
[root@172 mysql]# yum install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: centos.ustc.edu.cn
* extras: ftp.sjtu.edu.cn
........
Installed:
mysql-community-server.x86_64 0:5.7.20-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:5.7.20-1.el7 mysql-community-common.x86_64 0:5.7.20-1.el7
mysql-community-libs.x86_64 0:5.7.20-1.el7
Complete!
2、启动msyql systemctl start mysqld
3、查看启动状态
[root@172 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 2017-12-14 06:02:51 EST; 2min 13s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 9605 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 9524 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 9607 (mysqld)
CGroup: /system.slice/mysqld.service
└─9607 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Dec 14 06:02:33 *.*.*.* systemd[1]: Starting MySQL Server...
Dec 14 06:02:51 *.*.*.* systemd[1]: Started MySQL Server.
4、设置开机启动 systemctl enable mysqld
systemctl daemon-reload
三、配置mysql
1、修改密码策略
mysql5.7默认带有密码查检策略,密码默认策略要求:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误。
mysql的密码策略相关信息,可以在登录mysql后,通过命令show variables like ‘%password%’查看。
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个
mysql共有以下几种密码策略
策略 | 检查规则 |
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 |
可通过修改/etc/my.cnf文件来达到修改密码策略目的,具体方式修改方式如下:
# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件
validate_password_policy=0
# 如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:
validate_password = off
# 重新启动mysql服务使配置生效:
systemctl restart mysqld
2、修改root密码
mysql安装后,系统会自动初始化一个密码,通过如下命令查看:
[root@172 mysql]# grep 'temporary password' /var/log/mysqld.log
2017-12-14T11:02:37.459592Z 1 [Note] A temporary password is generated for root@localhost: ju/,8f1;O!J8
# 密码为localhost:后的字符,本例为:ju/,8f1;O!J8
使用root和默认密码登录mysql mysql -uroot -p
,然后输入默认密码登录。
修改root密码,可通过以下两种方式:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'your password';
mysql> set password for 'root'@'localhost'=password('your password');
3、配置默认编码
通过修改/etc/my.cnf文件,在[mysqld]下添加编码配置,如下:
[mysqld]
character_set_server=utf8
init_connect=’SET NAMES utf8’
重启服务生效(systemctl restart mysqld
)
重启后查看数据库默认编码格式如下:
mysql> 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/ |
4、设置远程访问
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,官方建议添加一个允许远程连接的帐户,尽量不使用root账号远程连接。添加用户账号命令如下:
mysql> CREATE USER 'test' IDENTIFIED BY 'test';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' IDENTIFIED BY 'test' WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'localhost' IDENTIFIED BY 'test' WITH GRANT OPTION;
四、卸载mysql
1、关闭mysql服务systemctl stop mysqld
2、查看已经安装的mysql组件
[root@172 ~]# rpm -qa | grep -i mysql
mysql-community-libs-compat-5.7.20-1.el7.x86_64
mysql-community-libs-5.7.20-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-client-5.7.20-1.el7.x86_64
mysql-community-server-5.7.20-1.el7.x86_64
mysql-community-common-5.7.20-1.el7.x86_64
3、卸载已经安装过的组件
yum -y remove mysql-community-client-5.7.20-1.el7.x86_64
yum -y remove mysql-community-common-5.7.20-1.el7.x86_64
# 卸载comm时,libs-compat会跟随卸载,此步可不操作
yum -y remove mysql-community-libs-compat-5.7.20-1.el7.x86_64
yum -y remove mysql57-community-release-el7-11.noarch
# 卸载client时,server会跟随卸载,此步可不操作
yum -y remove mysql-community-server-5.7.20-1.el7.x86_64
4、删除mysql目录
# whereis查找
[root@172 ~]# whereis mysql
mysql: /usr/share/mysql
# 删除残留
rm -rf /usr/share/mysql
# find查找
[root@172 ~]# find / -name mysql
/home/mysql
/etc/selinux/targeted/active/modules/100/mysql
/etc/selinux/targeted/tmp/modules/100/mysql
/var/lib/mysql
/var/lib/mysql/mysql
# 删除残留
rm -rf /home/mysql
rm -rf /etc/selinux/targeted/active/modules/100/mysql
rm -rf /etc/selinux/targeted/tmp/modules/100/mysql
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql/mysql
# 删除配置文件
rm –rf /usr/my.cnf
rm -rf /root/.mysql_sercret
5、确认卸载 rpm -qa | grep -i mysql