Linux安装MySQL

完全卸载

  • 查看mysql安装情况
rpm -qa | grep -i mysql
  • 删除安装的软件
rpm -ev mysql-community-libs-5.7.27-1.el6.x86_64 --nodeps
  • 都删除成功后,查找相关的MySQL的文件
find / -name mysql
  • 删除全部文件
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql/mysql
rm -rf /etc/logrotate.d/mysql
rm -rf /usr/share/mysql
rm -rf /usr/bin/mysql
rm -rf /usr/lib64/mysql
  • 再次执行命令,如果没有显示则表示卸载完成
rpm -qa | grep -i mysql

安装MySQL

  • 更换yum源

    • 打开mirrors.aliyun.com,选择centos的系统,点击帮助
    • 执行命令
    yum install wget -y
    
  • 改变某些文件的名称

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  • 执行更换yum源的命令,注意centos6和centos7的数字需要更换
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  • 更新本地缓存
yum clean all
yum makecache
  • 查看系统是否自带安装了MySQL
yum list installed | grep mysql
  • 删除系统自带的mysql及其依赖(防止冲突)
yum -y remove mysql-libs.x86_64
  • 安装wget命令
yum install wget -y
  • 给CentOS添加rpm源,并且选择较新的源,注意centos6和centos7的数字需要更换
wget dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
  • 安装下载好的rpm文件,注意centos6和centos7的数字需要更换
yum install mysql-community-release-el7-5.noarch.rpm -y
  • 安装成功之后,会在/etc/yum.repos.d/文件夹下增加两个文件
mysql-community.repo
mysql-community-source.repo
  • 修改mysql-community.repo文件
#如果是centos6,7->6即可
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql




[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql




# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql




# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql




# Note: MySQL 5.7 is currently in development. For use at your own risk.
# Please read with sub pages: https://dev.mysql.com/doc/relnotes/mysql/5.7/en/
[mysql57-community-dmr]
name=MySQL 5.7 Community Server Development Milestone Release
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  • 使用yum安装mysql
yum install mysql-community-server -y

启动mysql服务并设置开机启动

#启动之前需要生成临时密码,需要用到证书,可能证书过期,需要进行更新操作 ,需要等待一段时间
yum update -y
#启动mysql服务
service mysqld start ->Redirecting to /bin/systemctl start mysqld.service
#设置mysql开机启动
chkconfig mysqld on ->注意:正在将请求转发到“systemctl enable mysqld.service”。
  • 获取mysql的临时密码
grep "password" /var/log/mysqld.log


2020-11-23T15:02:16.898641Z 1 [Note] A temporary password is generated for root@localhost: mBek<;V/F7&4
  • 使用密码登陆
mysql -uroot -p
#输入密码
  • 修改密码
set global validate_password_policy=0;
set global validate_password_length=1;
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
  • 修改远程访问权限
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
//刷新权限
flush privileges;
  • /etc/my.cnf 设置字符集为utf-8
#在[mysqld]部分添加:
character-set-server=utf8
#在文件末尾新增[client]段,并在[client]段添加:
default-character-set=utf8
posted @ 2021-01-07 22:34  天蓝隐湘  阅读(82)  评论(0编辑  收藏  举报