CentOS 安装 MySQL
# CentOS7.6 安装 MySQL(rpm安装)
# 卸载历史版本MySQL
查看是否拥有历史版本
非首次安装需卸载历史版本MySQL,命令查看是否有安装MySQL历史版本组件
rpm -qa|grep mysql
1
查看MySQL服务状态
service mysqld status
1
暂停MySQL服务
service mysqld stop
1
卸载MySQL服务
需要把每个组件全部移除,注意可能会有组件的依赖前后顺序。
rpm -ev [需要移除组件的名称]
或者
rpm -e --nodeps [需要移除组件的名称] //此命令为强制卸载
1
2
3
2
3
# 安装MySQL
使用rpm来安装MySQL
因为CentOS 7默认安装的数据库是Mariadb,所以使用YUM命令是无法安装MySQL的,只会更新Mariadb。使用rpm来进行安装。可以在mysql的repo源仓库 (opens new window)右键复制指定版本的数据库。
wget http://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
1
安装mysql80-community-release-el7-1.noarch.rpm包
sudo rpm -ivh mysql80-community-release-el7-1.noarch.rpm
1
使用yum安装mysql服务
sudo yum install mysql-server
1
检查是否已经设置为开机启动MySQL服务
systemctl list-unit-files|grep mysqld
1
则表示已经设置为开机启动,如果没有设置为开机启动则执行
systemctl enable mysqld.service
1
查看MySQL是否启动未启动则执行启动服务命令
查看是否启动MySQL服务
ps -ef|grep mysql
启动服务
systemctl start mysqld.service
1
2
3
4
5
2
3
4
5
初始化MySQL
mysqld --initialize
1
查看MySQL初始默认密码
grep 'temporary password' /var/log/mysqld.log
1
复制 root@localhost: 后面的密码。登录mysql,并粘贴默认密码 因为MySQL8.0的更改,导致必须要重置密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Assam1314520..'\g; --设置root账号密码
1
如果设置密码时候出现提示
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
代表需要降低policy的等级后在执行
set global validate_password.policy=0;
1
开启MySQL远程连接
use mysql;
#修改root账户权限
update user set host = '%' where user = 'root';
#刷新权限
flush privileges;
1
2
3
4
5
2
3
4
5
创建新用户
create user '你的用户名'@'%' identified with mysql_native_password by '你的密码';
1
刷新权限
grant all on *.* to '你的用户名'@'%';
1
编辑 (opens new window)