到mysql官方网站下载需要的mysql版本,我这里用的是5.7.37。
使用ssh工具上传文件到opt目录,解压文件
#tar -zvxf /opt/mysql-5.7.37-el7-x86_64.tar.gz
移动文件到/usr/local/mysql
#mkdir /usr/local/mysql
#cd /opt/mysql-5.7.37-el7-x86_64
#mv * /usr/local/mysql
确认下目录的文件是否正确
#ls
创建组和用户
#groupadd mysql
#useradd -r -g mysql mysql
给目录授权
#chown -R mysql.mysql .
建一个数据库的目录
#mkdir -p /opt/mysql/data
目录授权
#chown -R mysql.mysql /opt/mysql/data/
修改配置文件/etc/my.cnf
配置文件略。
注册服务
#cp -rf /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#chmod +x /etc/init.d/mysqld
#systemctl enable mysqld || chkconfig mysqld on
初始化数据库
#rm -rf /opt/mysql/data/*
#./bin/mysqld --initialize --basedir=/usr/local/mysql --datadir=/opt/mysql/data --user=mysql
设置环境变量
echo "export PATH=/usr/local/mysql/bin:$PATH" >>/etc/profile source /etc/profile
启动服务
#systemctl start mysql
#停止mysql
systemctl stop mysql
#跳过密码启动 mysqld_safe --skip-grant-tables
连接mysql mysql -u root -p,设置密码
SET PASSWORD = PASSWORD('123456');
flush privileges;
设置远程访问
grant all privileges on . to 'root'@'%' identified by '123456';
select host,user from mysql.user;
delete from mysql.user where user='root' and host='%'
update mysql.user set host = '%' where user = 'root';
flush privileges;