一、环境介绍
操作系统:CentOS 7
MySQL:5.7
二、环境准备
在新版本的CentOS7中,默认的数据库已更新为了Mariadb
1)查看已安装的 Mariadb 数据库版本。
rpm -qa|grep -i mariadb
2)卸载已安装的 Mariadb 数据库
rpm -qa|grep mariadb|xargs rpm -e --nodeps
说明:-e参数表示删除 –nodeps表示不校验依赖关系
卸载后确认是否完成。
三、安装MySQL
1、yum源安装
yum -y install mysql57-community-release-el7-10.noarch.rpm
或
wget https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
rpm -ivh mysql57-community-release-el7-10.noarch.rpm
2、安装mysql-community-server
yum -y install mysql-community-server
3、此方式安装完成后,启动报错,描述信息:Failed to start mysqld.service: Unit not found.
据说是CentOS7中已经不在推荐使用mysql,甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险。好吧,此安装方式弃了。
查阅了一众文章后,推荐的最多替代方案:安装MariaDB(MySQL 的一个分支版本)
emm…,至于其他方案感兴趣的自行了解,我打算死磕到底
下面进行第二种方式安装
解压版安装
1、上传mysql压缩包文件并解压
tar -zxvf mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz
2、重命名
mv mysql-5.7.40-linux-glibc2.12-x86_64 mysql
3、安装依赖库
yum -y install numactl
yum -y install libaio*
4、初始化数据库
mkdir -p /usr/local/mysql/
mkdir -p /usr/local/mysql/data
./bin/mysqld --initialize --user=msyql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --explicit_defaults_for_timestamp
**重要,记住这里的临时密码:
5、本地服务
cp -a ./support-files/mysql.server /etc/init.d/mysqld
# 给所有用户赋予执行权限
chmod a+x /etc/init.d/mysqld
6、修改配置文件
vim /etc/my.cnf
添加内容如下:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
# 作用是禁止域名解析:在mysql的授权表中就不能使用主机名了,只能使用IP
skip-name-resolve
# 设置3306端口
port = 3306
#设置远程访问ip
bind-address=0.0.0.0
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#设置查询操作等不区分大小写
lower_case_table_names=1
7、设置为开机启动
chkconfig --add /etc/init.d/mysqld
chkconfig mysqld on
8、启动MySQL
启动:service mysqld start
停止:service mysqld stop
重启:service mysqld restart
9、配置环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
让配置生效
source /etc/profile
10、登录
#1.登录mysql
[root@localhost ~]# mysql -uroot -p'&B;yCZpVI0Hi'
#mysql5.7以后对密码的强度是有要求的,必须是字母+数字+符号组成的,如果想设置简单密码例
如‘root’,需要做以下设置
#2.设置密码长度最低位数
mysql> set global validate_password_length=4;
#3.设置密码强度级别
mysql> set global validate_password_policy=0;
#4.修改密码
mysql> alter user 'root'@'localhost' identified by '123456';
validate_password_policy默认是1,即MEDIUM
11、添加远程访问授权
GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password';
命令说明:
- ALL PRIVILEGES 表示授予所有的权限,此处可以指定具体的授权权限
- *.*表示所有库中的所有表
- %表示是任意ip地址,可以指定具体ip地址
为root 用户添加远程登录的能力
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "123456";
刷新权限
mysql> flush privileges;
12、关闭防火墙
查看防火状态: systemctl status firewalld
暂时关闭防火墙: systemctl stop firewalld
永久关闭防火墙: systemctl disable firewalld
重启防火墙: systemctl enable firewalld
13、尝试连接
连接成功。
下一章:MySQL篇之二:MySQL搭建集群