linux安装mysql教程
一. 下载mysql
方法一:官网下载
1. 下载地址:https://downloads.mysql.com/archives/community/
2. 选择对应的系统和版本,下载
方法二,直接命令下载:
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
二. 安装准备
1. 查看系统是否已安装mysql,如有会显示对应的mysql(类似mysql-libs-5.1.52-1.el6_0.1.x86_64)
rpm -qa | grep mysql
2. 卸载已安装的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 // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
3. 查看是否已安装mariadb 数据库,有则卸载
rpm -qa | grep mariadb
rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64
三. 安装mysql
1. 将下载好的安装包放到/opt/mysql目录下,进入目录,解压文件并重命名
cd /opt/mysql
tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql-5.7.25
2. 添加系统mysql组和mysql用户
cat /etc/group | grep mysql
cat /etc/passwd | grep mysql
3. 没有则创建用户和组,如图,代表成功,useradd -r参数表示mysql用户是系统用户,不可用于登录系统
groupadd mysql
useradd -r -g mysql mysql
4. 创建data目录,并将/opt/mysql/mysql-5.7.25的所有者及所属组改为mysql
mkdir /opt/mysql/mysql-5.7.25/data
chown -R mysql.mysql /opt/mysql/mysql-5.7.25
5. 在/opt/mysql/mysql-5.7.25/support-files目录下创建my_default.cnf
touch /opt/mysql/mysql-5.7.25/support-files/my_default.cnf
vi /opt/mysql/mysql-5.7.25/support-files/my_default.cnf
1 # For advice on how to change settings please see 2 # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html 3 # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the 4 # *** default location during install, and will be replaced if you 5 # *** upgrade to a newer version of MySQL. 6 7 8 [mysqld] 9 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 10 11 basedir = /opt/mysql/mysql-5.7.25 12 datadir = /opt/mysql/mysql-5.7.25/data 13 port = 3306 14 socket = /tmp/mysql.sock 15 character-set-server=utf8 16 17 log-error = /opt/mysql/mysql-5.7.25/data/mysqld.log 18 pid-file = /opt/mysql/mysql-5.7.25/data/mysqld.pid
:wq 保存退出
6. 复制文档为/etc/my.cnf
cp support-files/my_default.cnf /etc/my.cnf
7. 初始化mysqld,初始化语句的目录根据自己创建的目录修改
cd /opt/mysql/mysql-5.7.25
./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql/mysql-5.7.25/ --datadir=/opt/mysql/mysql-5.7.25/data/
8. 查看初始化日志,是否显示了临时密码
cat /opt/mysql/mysql-5.7.25/data/mysqld.log
9. 把启动脚本放到开机初始化目录
cd /opt/mysql/mysql-5.7.25/
cp support-files/mysql.server /etc/init.d/mysql
10. 启动mysql服务
service mysql start
11.登录mysql,密码为初始密码
./bin/mysql -u root -p
12. 修改密码
mysql> set password=password('123456');
mysql> grant all privileges on *.* to root@'%' identified by '123456';
mysql> flush privileges;
13. 添加远程访问权限,其中“%”表示允许所有机器能访问root用户
mysql> use mysql;
mysql> update user set host='%' where user = 'root';
mysql> flush privileges;
报 ERROR 1062 (23000)的原因可以根据查询语句发现host已经有%,所以直接执行flush privileges;
14. 重启mysql 生效
service mysql restart
四. 配置环境变量
1. 修改/etc/profile文件
vim /etc/profile
2. 末尾输入如下语句,:wq保存退出
MYSQL_HOME=/opt/mysql/mysql-5.7.25(mysql的安装路径)
PATH=$PATH:$MYSQL_HOME/bin
export PATH MYSQL_HOME
3. 重新加载配置文件
source /etc/profile