Cents OS 下 Mysql安装与权限

CentsOS7无外网环境下安装MySQL

Linux下mysql的配制文件在/etc/my.cnf

1.下载MySQL安装包(Linux版本)

mysql下载路径:
https://dev.mysql.com/downloads/mysql/

1.查看自己Linux系统版本:

cat /etc/RedHat-release

2.往下拉继续选择,根据自己系统相应的版本选择下载:(这里以MySQL5.7.20为例,可下载实时最新版本)

2.准备依赖包

a.相关支持:
perl-Data-Dumper-2.145-3.el7.x86_64.rpm
libaio-0.3.109-13.el7.x86_64.rpm
net-tools-2.0-0.22.20131004git.el7.x86_64.rpm
perl-Data-Dumper-2.145-3.el7.x86_64.rpm下载路径:
http://mirror.centos.org/centos/7/os/x86_64/Packages/perl-Data-Dumper-2.145-3.el7.x86_64.rpm
libaio-0.3.109-13.el7.x86_64.rpm下载路径:
http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-0.3.109-13.el7.x86_64.rpm
net-tools-2.0-0.22.20131004git.el7.x86_64.rpm下载路径:
http://mirror.centos.org/centos/7/os/x86_64/Packages/net-tools-2.0-0.22.20131004git.el7.x86_64.rpm

3.上传文件到服务器上

4.安装

4.1 安装依赖包

perl-Data-Dumper-2.145-3.el7.x86_64.rpm 、libaio-0.3.109-13.el7.x86_64.rpm和net-tools-2.0-0.22.20131004git.el7.x86_64.rpm

rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
rpm -ivh perl-Data-Dumper-2.145-3.el7.x86_64.rpm
rpm -ivh net-tools-2.0-0.22.20131004git.el7.x86_64.rpm

4.2 查看mariadb版本

rpm -qa | grep mariadb

4.3 卸载系统安装的mariadb

rpm -e --nodeps 查出来的相应的文件

4.4 再次通过rpm -qa | grep mariadb 命令查看是否卸载干净,确保卸载干净后,再往下操作。(这里的操作是因为在CentOS 7上已经有一个mariadb 而这个会与 MySQL的mariadb发生冲突。所以此时我们需要将系统自带的mariadb卸载掉,然后使用mysql自带的mariadb。)

4.5 安装mysql,先解压mysql,并查看安装包

tar -xf mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar 

4.6 继续安装

  rpm -ivh mysql-community-common-5.7.20-1.el7.x86_64.rpm 
  rpm -ivh mysql-community-libs-*
  rpm -ivh mysql-community-devel-5.7.20-1.el7.x86_64.rpm
 rpm -ivh mysql-community-client-5.7.20-1.el7.x86_64.rpm
 rpm -ivh mysql-community-server-5.7.20-1.el7.x86_64.rpm

至此,mysql安装成功

5.启动查看配置

5.1. 启动服务:

systemctl start mysqld.service

5.2.查看服务状态:

systemctl status mysqld.service

5.3.停止服务:

systemctl stop mysqld.service

6.登录MySQL 数据库

6.1. 由于我们没有设置Mysql数据库初始密码,我们需要查看系统给root用户的初始密码:

cat /var/log/mysqld.log | grep password

输入该命令后出现以下内容:
2017-11-04T05:56:08.454323Z 1 [Note] A temporary password is generated for root@localhost: ps1DY?%T.Vc4(这里为root初始随机密码,注意:这里是事例,登录时要填入自己系统产生的密码)

6.2. 登录MySQL数据库:

mysql -u root -p 

输入刚才查到的初始密码(复制即可),登录成功后发现输入任何命令都会提示你需要修改初始密码,所以接下来要修改初始密码。

7.修改root用户初始密码

7.1. 降低密码复杂度,登录MySQL数据库后输入命令:

(注意:在MySQL数据库每次输入完命令后要加;)

set global validate_password.policy=0;

7.2. 降低密码最小长度,将密码最小长度设置为4:

 set global validate_password.length=1;

7.3. 修改用户密码:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'KL1f~xXal8';
ALTER USER USER() IDENTIFIED BY 'Xiaoming250';
SET PASSWORD = PASSWORD('Q7sg=icKfz'); 
MySQL修改用户的密码主要有两种方法:ALTER USER 和SET PASSWORD
ALTER USER 
基本使用
ALTER USER testuser IDENTIFIED BY '123456';
修改当前登录用户(自己操作时用了这个命令,注意需要用当前用户登陆)
ALTER USER USER() IDENTIFIED BY '123456';
使密码过期
ALTER USER testuser IDENTIFIED BY '123456' PASSWORD EXPIRE;
使密码从不过期
ALTER USER testuser IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
按默认设置过期时间
ALTER USER testuser IDENTIFIED BY '123456' PASSWORD EXPIRE DEFAULT;
指定过期间隔
ALTER USER testuser IDENTIFIED BY '123456' PASSWORD EXPIRE INTERVAL 90 DAY;
在MySQL文档里,推荐使用ALTER USER修改用户密码
SET PASSWORD
使用SET PASSWORD的password有两种:
使用默认加密
SET PASSWORD FOR testuser = '123456'
使用PASSWORD()函数加密
SET PASSWORD FOR testuser = PASSWORD("123456")
注意:使用PASSWORD('auth_string')的方式已经被废弃,在以后的版本会把它移除,所以不建议使用它来修改密码。

7.4. 修改成功后输入命令刷新:

FLUSH PRIVILEGES; 

至此数据库安装完毕,再输入相应命令就没有问题了

7.8.解决允许远程连接问题

1.   通过命令进入mysql数据库,然后输入命令进入mysql数据库:
       use mysql;  
2.   登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改成"%" :
update user set host = '%' where user = 'root';
注意:(用户名可以根据实际情况自己指定)
3.   查看root用户修改结果:
select host, user from user;  
4.   刷新:
      FLUSH   PRIVILEGES;
5. 远程连接问题
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root@123' WITH GRANT OPTION;

7.9.查看配置文件地址

  1. 查看相关配置文件:
      find / -iname '*.cnf' -print

7.10 创建数据库、用户、授权

  • 创建用户
CREATE USER `test_zl`@`127.0.0.1` IDENTIFIED BY 'KL1f~xXal9';

  • 创建数据库
CREATE DATABASE `test_zl` CHARACTER SET 'utf8' COLLATE 'utf8_bin';

  • 授权
1.用root用户进入shell: mysql -h127.0.0.1 -uroot -proot
2.创建数据库:create database front character set utf8 collate utf8_bin;
3.授权数据库: grant all on front.* to front@'%'IDENTIFIED BY 'front';
如果是mysql8 需要用以下授权方法:
 create user 'front'@'%' identified by 'front'
 grant all privileges on middle.* to 'middle'@'%';
4.权限生效: flush privileges;
5.GRANT Alter, Alter Routine, Create, Create Routine, Create Temporary Tables, Create User, Create View, Delete, Drop, Event, Execute, File, Grant Option, Index, Insert, Lock Tables, Process, References, Reload, Replication Client, Replication Slave, Select, Show Databases, Show View, Shutdown, Super, Trigger, Update ON *.* TO `test_zl`@`127.0.0.1`;

10.参考网址
3. MySQL无外网安装过程:
https://www.linuxidc.com/Linux/2017-11/148286.htm
4. MySQL修改密码复杂度:
https://www.cnblogs.com/ivictor/p/5142809.html
5. MySQL解决远程连接问题:
https://jingyan.baidu.com/article/455a9950a4fea0a16727787e.html
6. MySQL创建新用户并授权:
https://www.jb51.net/article/31850.htm

卸载MYSQL

查询mysql依赖并且删除

查询mysql相应安装信息:rpm -qa | grep mysql

删除命令:
rpm -ev MySQL-client-5.5.25a-1.rhel5 
rpm -ev MySQL-server-5.5.25a-1.rhel5

如果上述命令未能删除可如下:
rpm -ev MySQL-client-5.5.25a-1.rhel5 --nodeps

再次查询:rpm -qa | grep mysql
以下是删除的截图

Cents OS 下 Mysql安装与权限_第1张图片
Cents OS 下 Mysql安装与权限_第2张图片

查询mysql文件夹并且删除

find / -name mysql
查找结果如下:
/var/lib/mysql
/var/lib/mysql/mysql
/etc/selinux/targeted/active/modules/100/mysql
执行删除命令:
 rm -rf /var/lib/mysql
 rm -rf /var/lib/mysql/mysql
 rm -rf /etc/selinux/targeted/active/modules/100/mysql

Cents OS 下 Mysql安装与权限_第3张图片

查询mysql配制文件并且删除

 find / -iname '*.cnf' -print
 rm -rf /etc/my.cnf
 最后再执行检查:rpm -qa|grep -i mysql
 如果没有了,说明卸载干净了

Cents OS 下 Mysql安装与权限_第4张图片

Linux下修改mysql 的datadir

运维给了两台新环境,结果装完Mysql后跑了两天kafka就堵了,追查发下,是因为业务处理时,mysql数据所在的磁盘满了,只给分了1.9个G是个什么鬼啊。另一块200G的没有挂载,mysql是默认安装没记得有路径需要改,发现文件记在了/var/lib/mysql下,100%了,就是这个原因导至了kafka积累。干~~~ 修改mysql datadir,或是把文件重新挂载,先unmount 再mount

找到my.cnf配置文件

find / -iname '*.cnf' -print

在这里插入图片描述

修改配制文件

修改之前长这样

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

修改后的样子如下:


# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/data/mysql
socket=/data/mysql/mysql.sock

log-error=/data/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
socket=/data/mysql/mysql.sock

注:上面的[client] socket=/data/mysql/mysql.sock如果不加的话,启动后无法用mysql -uroot -ppassword登陆,会报错ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ;总之默认通过systemctl start mysql 后,用mysql 命令会找到默认路径/var/lib/mysql/mysql.sock下。所以需要在配制文件中显示声明出来就好了。

copy数据文件到新的“家”

把已有的数据文件copy到指定路径,并创建log文件夹

cp -rf /var/lib/mysql /data/mysql
mkdir /data/log

Cents OS 下 Mysql安装与权限_第5张图片

赋权限

(cp  -a /var/lib/mysql /data/ )

(chown -R mysql:mysql /data/mysql)

(chown -R mysql:mysql /data)

在这里插入图片描述

清除原路径数据文件并重启

rm -rf /var/lib/limb
systemctl stop mysqld.service
systemctl start mysqld.servcie

你可能感兴趣的:(配置学习相关)