4.1 检查是否已安装mysql
rpm -qa | grep mysql
若已安装有mysql,可执行以下命令进行卸载
# 普通删除模式
rpm -e mysql
# 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
rpm -e --nodeps mysql
4.2 下载安装
下载地址:https://dev.mysql.com/downloads/repo/yum/
# 下载
wget -i -c http://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
# 运行程序
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
# 更新命令
yum update
# 安装
yum install mysql-server
# 权限配置
chown mysql:mysql -R /var/lib/mysql
# 初始化
mysqld --initialize
4.3 启动服务
systemctl start mysqld
相关操作:
# 查看运行状态
systemctl status mysqld
# 停止服务
systemctl stop mysqld
# 重启服务
systemctl restart mysqld
注:若启动服务提示以下错误内容,可先“/var/lib/mysql”文件权限
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
更改 “/var/lib/mysql” 文件权限
chown mysql:mysql -R /var/lib/mysql
4.4 查看版本
mysqladmin --version
4.5 修改用户密码
# 连接mysql,回车后出现"Enter password:"提示时,输入密码,然后回车确认
[root@host]# mysql -u root -p
# 查看临时密码,不清楚初始密码时可以通过临时密码进行连接
[root@host]# grep 'temporary password' /var/log/mysqld.log
# 设置密码
mysql> alter user 'root'@'localhost' identified by '123456';
注:首次登陆需要修改密码,否则会报以下错误
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
4.6 设置允许所有IP访问
# 登陆
[root@host]# mysql -u root -p
#
mysql> use mysql;
#
mysql> select Host,User from user;
# 更改
mysql> update user set host='%' where user='root' AND host='localhost';
# 重启服务
[root@host]# systemctl restart mysqld