一、下载Mysql
下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
二、环境检查
检测系统是否自带mysql
rmp -qa|grep mysql
如果有进行强行卸载
rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64
查看是否安装mysql
mysql -version
三、mysql安装
用连接工具将下载的mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz上传到linux服务器的/usr/local目录下面
# 进入文件目录
cd /usr/local
# 解压
tar -zxvf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
# 解压后重命名为mysql
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
# 检查mysql组和用户是否存在,如果没有则创建
cat /etc/group|grep mysql
groupadd mysql
# useradd -r参数表示mysql用户是系统用户,不可用于登录系统
useradd -r -g mysql mysql
# 创建data目录
mkdir /usr/local/mysql/data
# 将/usr/local/mysql/的所有者及所属组改为mysql
chown -R mysql.mysql /usr/local/mysql/
在/usr/local/mysql/support-files目录下创建my_default.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8
log-error = /usr/local/mysql/data/mysqld.log
pid-file = /usr/local/mysql/data/mysqld.pid
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 拷贝,是否覆盖,是
cp my_default.cnf /etc/my.cnf
# 初始化Mysql
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
初始化完成之后查看日志,可获取初始密码
cat /usr/local/mysql/data/mysqld.log
把启动脚本放到开机初始化目录
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
启动mysql
service mysql start
四、修改密码
# 进入目录
cd /usr/local/mysql
# 登录 然后输入初始密码
./bin/mysql -u root -p
# 重置密码
mysql> set password=password('root');
mysql> grant all privileges on *.* to root@'%' identified by 'root';
mysql> flush privileges;
# 添加远程访问权限
mysql> use mysql;
mysql> update user set host='%' where user = 'root';
mysql> flush privileges;
# 退出登录
mysql> exit;
# 重启mysql生效
service mysql restart
如果找不到初始密码的文件,可以这么做:
# 修改my.cnf文件
vim /etc/my.cnf
# 添加内容
skip-grant-tables=1
停止权限验证skip-grant-tables=1,这样登录就不需要密码,直接回车
# 重启mysql
service mysql restart
# 登录 直接回车
./bin/mysql -u root -p
# 设置密码
update mysql.user set authentication_string=password('root') where user='root' ;
flush privileges;
grant select on `master`.* to root@localhost identified by 'root';
# 添加远程访问权限
mysql> use mysql;
mysql> update user set host='%' where user = 'root';
mysql> flush privileges;
# 退出登录
mysql> exit;
# 修改my.cnf文件
vim /etc/my.cnf
# 去掉内容
skip-grant-tables=1
# 重启mysql
service mysql restart
五、设置MySql自动启动
# 将服务文件拷贝到init.d下,并重命名为mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 赋予可执行权限
chmod +x /etc/init.d/mysqld
# 添加服务
chkconfig --add mysqld
# 显示服务列表
chkconfig --list
如果看到mysql的服务,并且3,4,5都是开的话则成功
如果是关,则键入
chkconfig --level 345 mysqld on
重启电脑验证是否生效
reboot
转自:https://blog.csdn.net/qq_39566521/article/details/125095611
标签:38,lunix,--,root,mysql5.7,mysqld,usr,mysql,local From: https://www.cnblogs.com/furenjian/p/16956016.html