centos7 mysql5.6的安装与卸载
卸载
- yum方式
查看yum是否安装过mysql
yum list installed mysql*
如或显示了列表,说明系统中有MySQL
yum卸载
根据列表上的名字
yum remove mysql-community-client mysql-community-common mysql-community-libs mysql-community-libs-compat mysql-community-server mysql57-community-release
rm -rf /var/lib/mysql
rm /etc/my.cnf
- rpm方式
rpm查看安装
rpm -qa | grep -i mysql
rpm 卸载
rpm -e mysql57-community-release-el7-9.noarch
rpm -e mysql-community-server-5.7.17-1.el7.x86_64
rpm -e mysql-community-libs-5.7.17-1.el7.x86_64
rpm -e mysql-community-libs-compat-5.7.17-1.el7.x86_64
rpm -e mysql-community-common-5.7.17-1.el7.x86_64
rpm -e mysql-community-client-5.7.17-1.el7.x86_64
cd /var/lib/
rm -rf mysql/
清除余项
whereis mysql
mysql: /usr/bin/mysql /usr/lib64/mysql /usr/local/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
#删除上面的文件夹
rm -rf /usr/bin/mysql
删除配置
rm –rf /usr/my.cnf
rm -rf /root/.mysql_sercret
剩余配置检查
chkconfig --list | grep -i mysql
chkconfig --del mysqld
根据上面的列表,删除 ,如:mysqld
mysql 的安装
1. 添加 MySQL YUM 源
根据自己的操作系统选择合适的安装源,和其他公司一样,总会让大家注册账号获取更新,注意是 Oracle 的账号,如果不想注册,下方有直接下载的地址,下载之后通过rpm -Uvh
安装。
$wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm'
$sudo rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
$yum repolist all | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 36
mysql-tools-community/x86_64 MySQL Tools Community 47
mysql57-community/x86_64 MySQL 5.7 Community Server 187
2. 选择安装版本
如果想安装最新版本的,直接使用 yum 命令即可
$sudo yum install mysql-community-server
如果想要安装 5.6 版本的,有2个方法。命令行支持 yum-config-manager 命令的话,可以使用如下命令:
$ sudo dnf config-manager --disable mysql57-community
$ sudo dnf config-manager --enable mysql56-community
$ yum repolist | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 36
mysql-tools-community/x86_64 MySQL Tools Community 47
mysql56-community/x86_64 MySQL 5.6 Community Server 327
或者直接修改 /etc/yum.repos.d/mysql-community.repo 这个文件
# 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=1 #表示当前版本是安装
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0 #默认这个是 1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
通过设置 enabled 来决定安装哪个版本。
设置好之后使用 yum 安装即可。
3. 启动 MySQL 服务
$sudo service mysqld start
$sudo systemctl start mysqld #CentOS 7
$sudo systemctl status mysqld
对于 MySQL 5.7 版本,启动的时候如果数据为空的,则会出现如下提示
The server is initialized.
An SSL certificate and key files are generated in the data directory.
The validate_password plugin is installed and enabled.
A superuser account ‘root’@‘localhost’ is created. A password for the superuser is set and stored in the error log file.To reveal it, use the following command:
sudo grep ‘temporary password’ /var/log/mysqld.log
单的说就是服务安装好了,SSL 认证的文件会在 data 目录中生存,密码不要设置的太简单了,初始密码通过下面的命令查看,赶紧去改密码吧。
安装提示,查看密码,登录数据库,然后修改密码:
$ mysql -uroot -p #输入查看到的密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!'
如果安装是5.6版本的数据库,那么默认的root密码是空,直接按回车进入即可。
依次通过以下命令修改root用户名密码:
mysql>use mysql;
mysql>update user set password=password('root') where user='root';
mysql>flush privileges;
4. 设置远程登录
mysql默认只能本机登录,通过以下命令,设置允许远程登录:
mysql>GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password' WITH GRANT OPTION;
your username 和 your password 改成 mysql 数据库的用户和密码