安装版本:mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
安装前,我们可以检测系统是否自带安装 MySQL:rpm -qa | grep mysql,如有就删除:
rpm -e mysql-libs-5.1.52-1.el6_0.1.x86_64//普通删除模式
rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64//强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
检查否存在 mariadb 数据库,如有,卸载之,卸载同上:
rpm -qa | grep mariadb
卸载:rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
解压:tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz,mv到指定目录,此处根据实际情况而定。安装目录一般都放在/usr/local下面,这里放在/home/zj/mysql下面。
添加系统mysql组和mysql用户:
groupadd mysql
useradd -r -g mysql mysql #useradd -r参数表示mysql用户是系统用户,不可用于登录系统
创建data目录,用户存放数据库文件或日志文件等:
cd /home/zj/mysql
mkdir data
sudo chown -R mysql.mysql /home/zj/mysql/data/
sudo chmod -R 777 /home/zj/mysql/data/
在/home/zj/mysql/support-files目录下创建my_default.cnf文件,内容如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir = /home/zj/mysql
datadir = /home/zj/mysql/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8
log-error = /home/zj/mysql/data/mysqld.log
pid-file = /home/zj/mysql/data/mysqld.pid
将该文件拷贝到mysql指定目录:cp support-files/my_default.cnf /etc/my.cnf
初始化数据库( mysqld):
./bin/mysqld --initialize --user=mysql --basedir=/home/zj/mysql/ --datadir=/home/zj/mysql/data/
初始化完成之后,查看日志中的临时密码:/home/zj/mysql/data/mysqld.log
把启动脚本放到开机初始化目录:cp support-files/mysql.server /etc/init.d/mysql
启动、停止、状态、重启mysql服务:service mysql start/stop/status/restart
登录mysql,密码为初始密码(临时密码):./bin/mysql -u root -p
将bin/mysql软连接到/usr/bin下面,方便直接使用mysql命令登录操作等:
sudo ln -s /home/zj/mysql/bin/mysql /usr/bin/
修改密码:
mysql> set password=password('123456');
mysql> grant all privileges on *.* to root@'%' identified by '123456';
mysql> flush privileges;
添加远程访问权限(经测试,可省略):
mysql> use mysql;
mysql> update user set host='%' where user = 'root';
mysql> flush privileges;
完毕!