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安装文件
将mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz文件上传到/usr/local/目录下解压 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
创建数据目录并赋予权限
在系统根目录下创建data目录和mysql目录
cd / mkdir -p data cd data mkdir -p mysql
授权并查看授权信息
chown mysql:mysql -R /data/mysql
配置my.cnf参数
在/etc目录下创建my.cnf文件,并进行配置: [mysqld] basedir=/usr/local/mysql datadir=/data/mysql socket=/tmp/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参数
/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql
初始化配置
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
注意:如果是报错或者重新初始化,需要清空/data/mysql/下的文件。
开启服务
将mysql加入服务 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql 设置开机自启 chkconfig mysql on 开启数据库服务 service mysql start
修改密码
/usr/local/mysql/bin/mysql -u root -p >>use mysql; >>update user set authentication_string=password('你的密码') where user='root'; >>flush privileges; >>exit;
重启mysql服务
service mysql restart
这是空密码进入,修改好密码之后,就用新的密码登录,同时将my.cnf文件中的skip-grant-tables注释掉
[mysqld] basedir=/usr/local/mysql datadir=/data/mysql socket=/tmp/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
允许远程连接
/usr/local/mysql/bin/mysql -u root -p >>use mysql; >>update user set host='%' where user = 'root'; >>flush privileges; >>eixt;
配置mysql环境变量
ln -s /usr/local/mysql/bin/mysql /usr/bin
设置开机自启动
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql chmod +x /etc/init.d/mysql chkconfig --add mysql 查看服务 chkconfig --list 看到对应服务名的3 4 5为open 或者 on 即可