mysql5.7静默安装
mysql版本:mysql-5.7.22-linux-glibc2.12-x86_64
查看系统中是否已安装mariadb 服务
rpm -qa | grep mariadb
卸载 mariadb及其依赖的包
yum -y remove mariadb-libs-5.5.44-2.el7.centos.x86_64
防火墙设置
查看防火墙状态 systemctl status firewalld 关闭防火墙 systemctl stop firewalld 查看防火墙是否开机自启 systemctl is-enabled firewalld.service;echo $? 禁止防火墙开机启动 systemctl disable firewalld
上传MySQL安装文件
创建目录 mkdir -p /opt/software
将mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz文件上传到/opt/software/目录下
解压 tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
将文件重命名 mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql
添加用户和组
groupadd mysql useradd mysql -g mysql
要是再Ubuntu下添加用户、组,则使用如下命令:
useradd -d "/home/mysql" -m -s "/bin/bash" mysql
创建数据目录并赋予权限
在系统根目录下创建data目录和mysql目录
mkdir -p /data/mysql
授权并查看授权信息
chown mysql:mysql -R /data/mysql
配置my.cnf参数
在/etc目录下创建my.cnf文件,并进行配置: [mysqld] basedir=/opt/software/mysql datadir=/data/mysql socket=/opt/software/mysql/mysql.sock user=mysql port=3306 character-set-server=utf8mb4 # cancle password skip-grant-tables # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
[client]
port=3306
socket=/opt/software/mysql/mysql.sock
需要注意,创建的该文件不能是777权限,否则mysql会认为谁都能修改风险很高,配置的信息都会失效,直接提示[Warning] World-writable config file '/etc/my.cnf' is ignored.
初始化配置
/opt/software/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/opt/software/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
注意:如果是报错或者重新初始化,需要清空/data/mysql/下的文件。
开启服务
将mysql加入服务 cp /opt/software/mysql/support-files/mysql.server /etc/init.d/mysql 设置开机自启 chkconfig mysql on 开启数据库服务 service mysql start
查看服务
chkconfig --list
看到对应服务名的3 4 5为open 或者 on 即可
修改密码
/opt/software/mysql/bin/mysql -u root -p >>use mysql; >>update user set authentication_string=password('你的密码') where user='root'; >>flush privileges; >>exit;
mysql8版本开始修改密码命令,登录mysql,不要执行use mysql;先执行修改密码命令:
ALTER USER "root"@"localhost" IDENTIFIED BY "你的新密码";
或者
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
重启mysql服务
service mysql restart
这是空密码进入,修改好密码之后,就用新的密码登录,同时将my.cnf文件中的skip-grant-tables注释掉
[mysqld] basedir=/opt/software/mysql datadir=/data/mysql socket=/opt/software/mysql/mysql.sock user=mysql port=3306 character-set-server=utf8mb4 # cancle password #skip-grant-tables # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
重启mysql服务
service mysql restart
允许远程连接
/opt/software/mysql/bin/mysql -u root -p >>use mysql; >>update user set host='%' where user = 'root'; >>flush privileges; >>exit;
配置mysql环境变量
ln -s /opt/software/mysql/bin/mysql /usr/bin