MySQL
在线安装MySQL
1.下载
[root@localhost ~]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
--2018-01-08 16:57:46-- https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
正在解析主机 dev.mysql.com (dev.mysql.com)... 137.254.60.11
正在连接 dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm [跟随至新的 URL]
--2018-01-08 16:57:48-- https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
正在解析主机 repo.mysql.com (repo.mysql.com)... 23.1.165.122
正在连接 repo.mysql.com (repo.mysql.com)|23.1.165.122|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:25680 (25K) [application/x-redhat-package-manager]
正在保存至: “mysql57-community-release-el7-11.noarch.rpm”
100%[==================================================================================================================================================================================================>] 25,680 --.-K/s 用时 0.1s
2018-01-08 16:57:48 (232 KB/s) - 已保存 “mysql57-community-release-el7-11.noarch.rpm” [25680/25680])
[root@localhost ~]#
2.安装
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
执行完成后会在/etc/yum.repos.d/目录下生成两个repo文件mysql-community.repo mysql-community-source.repo
3.选择安装版本
[root@cnetos yum.repos.d]# vim /etc/yum.repos.d/mysql-community.repo
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
- 上边是只显示部分内容,找到对应的 Community Server 版本,将其“enabled=0”属性的值改为“enabled=1”
- 并将原来的server版本对应的“enabled=1”改为“enabled=0”
4.添加仓库
[root@cnetos yum.repos.d]#sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
5.安装
[root@cnetos yum.repos.d]#yum install mysql-server
GPG密钥过期:
The GPG keys listed for the "MySQL 5.7 Community Server" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.
Failing package is: mysql-community-libs-compat-5.7.38-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
解决办法
更新最新的GPG密钥
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
然后重新运行安装命令
[root@cnetos yum.repos.d]#yum install mysql-server
6.启动
systemctl start mysqld
7.修改root登录密码
获取root默认密码(由于Mysql安全策略升级,安装完成后系统自动设置了一个随机密码)
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2018-01-08T09:21:45.780623Z 1 [Note] A temporary password is generated for root@localhost: auw;Nj7J!j/J
[root@localhost ~]#
8.登录Mysql
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
9.修改密码
由于Mysql默认要求设置密码复杂度高(必须包含 大小写字母、数字、符号)
mysql> alter user 'root'@'localhost' identified by '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>
关闭Mysql密码校验规则,允许设置简单密码
#在Mysql配置文件最后加入:validate_password = off
[root@cnetos yum.repos.d]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
validate_password = off
-
然后重启服务器
-
systemctl restart mysqld
10.重新修改密码
mysql> set password=password("root"); ---->双引号印着的部分为密码
Query OK, 0 rows affected (0.00 sec)
mysql>
11.修改编码
mysql>show variables like '%character%'; --->查看编码
#可以看到database和server编码都为latin1,我们修改为utf8
#如果/etc/my.cnf 没有该文件那么
#1.将my-default.cnf 拷贝到 /etc/my.cnf 根据情况找到自己的目录
cp /usr/share/doc/mysql-community-server-5.6.31/my-default.cnf /etc/my.cnf
#2.编辑/usr/my.cnf在下增加
[client]
default-character-set = utf8
#3.编辑/usr/my.cnf在下增加
[mysqld]
character_set_server = utf8
#4.重启MySql服务 这句代码和之前使用的net start mysql作用应该是一样的 开启服务 centos7使用以下代码
systemctl restart mysqld
12.允许远程登陆
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';
FLUSH PRIVILEGES;