Linux centos 6.5 安装、卸载、启动 Mysql 并异常处理
一、Linux centos 6.5 安装 Mysql 5.1.73(yum命令)
1.1 检测该服务器中是否有Mysql。
# yum list installed | grep mysql
2.1 删除系统自带Mysql
# yum -y remove mysql-libs.x86_64
2.2 使用命令查看yum上提供的mysql数据库可下载的版本:
yum list | grep mysql
然后我们可以通过输入 yum install -y mysql-server mysql mysql-devel 命令将mysql mysql-server mysql-devel都安装好(注意:安装mysql时我们并不是安装了mysql客户端就相当于安装好了mysql数据库了,我们还需要安装mysql-server服务端才行)
yum install -y mysql-server mysql mysql-deve
2.3 查看版本
rpm -qi mysql-server
2.4 启动mysql服务
service mysqld start
2.5 设置是否开机自启动
chkconfig --list | grep mysqld //命令来查看mysql服务是不是开机自动启动 chkconfig mysqld on //命令来将其设置成开机启动,下次不用再设置 mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
2.6 为root账户设置密码
mysql数据库安装完以后只会有一个root管理员账号,但是此时的root账号还并没有为其设置密码,在第一次启动mysql服务时,会进行数据库的一些初始化工作,在输出的一大串信息中,我们看到有这样一行信息 :
/usr/bin/mysqladmin -u root password 'new-password' // 为root账号设置密码
所以我们可以通过 该命令来给我们的root账号设置密码(注意:这个root账号是mysql的root账号,非Linux的root账号)
2.7 登录mysql
mysql -u root -p
二、Linux centos 6.5 安装 Mysql 8.0 (rpm命令)Mysql5.7 相同
2.1、检测该服务器中是否有Mysql。删除,同上
2.2 网上下载mysql8.0
2.3 上传并解压到指定文件夹下面(注意的是不是 -zxvf 而是-xvf)
tar -xvf mysql-8.0.13-1.el6.x86_64.rpm-bundle.tar -C /usr/local/
2.4 按照指定顺序进行安装(ivh中, i-install
安装;v-verbose
进度条;h-hash
哈希校验)
[root@centos03 mysql8]# rpm -ivh mysql-community-common-8.0.13-1.el6.x86_64.rpm warning: mysql-community-common-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ########################################### [100%] 1:mysql-community-common ########################################### [100%] [root@centos03 mysql8]# rpm -ivh mysql-community-libs-8.0.13-1.el6.x86_64.rpm warning: mysql-community-libs-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ########################################### [100%] 1:mysql-community-libs ########################################### [100%] [root@centos03 mysql8]# rpm -ivh mysql-community-libs-compat-8.0.13-1.el6.x86_64.rpm warning: mysql-community-libs-compat-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ########################################### [100%] 1:mysql-community-libs-co########################################### [100%] [root@centos03 mysql8]# rpm -ivh mysql-community-client-8.0.13-1.el6.x86_64.rpm warning: mysql-community-client-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ########################################### [100%] 1:mysql-community-client ########################################### [100%] [root@centos03 mysql8]# rpm -ivh mysql-community-server-8.0.13-1.el6.x86_64.rpm warning: mysql-community-server-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ########################################### [100%] 1:mysql-community-server ########################################### [100%] [root@centos03 mysql8]#
三、启动Mysql和基本操作
# 查看mysql是否启动 service mysqld status # 启动mysql service mysqld start # 停止mysql service mysqld stop # 重启mysql service mysqld restart
mysql安装完成之后我们是没有设置密码的,但是mysql为我们设置了一个临时的密码,我们可以查看mysql的日志知道这个临时密码。
3.1 查看临时密码
grep password /var/log/mysqld.log
3.2 添加用户,设置密码,给与相应权限
mysql -u root -p
3.3 忘记密码 操作步骤
(1)vim /etc/my.cnf
在[mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程,如下图所示:
保存文档并退出:
(2)重启MySQL。/etc/init.d/mysql restart(有些用户可能需要使用/etc/init.d/mysqld restart)
(3)输入#mysql即可进入mysql。
mysql> use mysql; mysql> update user set password=password("你的新密码") where user="root"; mysql> flush privileges; mysql> quit
在第二各种,Mysql5.7 改了字段
update mysql.user set authentication_string=password('root') where user='root' ;即可
(4)编辑my.cnf,去掉刚才添加的内容,然后重启MySQL。完事!
四、异常处理 初始化 MySQL 数据库: [失败]
4.1 检测命令是否 [ERROR] Aborting
(1)mysqld --initialize --user=mysql
[root@centos03 mysql57]# mysqld --initialize --user=mysql 2018-11-28T16:32:29.300355Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2018-11-28T16:32:29.307011Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting. 2018-11-28T16:32:29.307039Z 0 [ERROR] Aborting
(2)删除残留数据 rm -rf /var/lib/mysql/*
[root@centos03 mysql57]# rm -rf /var/lib/mysql/* [root@centos03 mysql57]# mysqld --initialize --user=mysql [root@centos03 mysql57]# service mysqld start 正在启动 mysqld: [确定]
总结别人经验
4.2 查找初始密码(我这边之所以有两个密码,应该就是之前没卸载干净,直觉应该用后边的新密码登录)
[root@mini2 upload]# grep 'temporary password' /var/log/mysqld.log 2017-08-29T05:00:02.613275Z 1 [Note] A temporary password is generated for root@localhost: tzqjMvkHH3+C 2017-08-29T05:31:51.340338Z 1 [Note] A temporary password is generated for root@localhost: Adgwggdr%1e2
[root@mini2 upload]# mysql -uroot -p
修改密码并允许其他机器上客户端登录
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option
五、卸载Mysql
1.关闭MySQL服务
[root@vm-xiluhua][/etc]$ service mysqld stop Redirecting to /bin/systemctl stop mysqld.service
2.卸载MySQL软件包
2.1.查询系统下的所有mysql软件包
[root@vm-xiluhua][/etc]$ rpm -aq | grep -i mysql mysql-community-server-5.7.15-1.el7.x86_64 mysql-community-libs-5.7.15-1.el7.x86_64 mysql-community-common-5.7.15-1.el7.x86_64 mysql-community-client-5.7.15-1.el7.x86_64
2.2.卸载系统下的所有mysql软件包
[root@vm-xiluhua][/etc]$ rpm -ev mysql-community-server-5.7.15-1.el7.x86_64 软件包准备中... mysql-community-server-5.7.15-1.el7.x86_64 警告:/etc/my.cnf 已另存为 /etc/my.cnf.rpmsave [root@vm-xiluhua][/etc]$ rpm -ev mysql-community-client-5.7.15-1.el7.x86_64 软件包准备中... mysql-community-client-5.7.15-1.el7.x86_64 # 忽略依赖 --nodeps [root@vm-xiluhua][/etc]$ rpm -ev mysql-community-libs-5.7.15-1.el7.x86_64 --nodeps 软件包准备中... mysql-community-libs-5.7.15-1.el7.x86_64 [root@vm-xiluhua][/etc]$ rpm -ev mysql-community-common-5.7.15-1.el7.x86_64 软件包准备中... mysql-community-common-5.7.15-1.el7.x86_64
3.删除MySQL安装文件夹
查看剩下有哪些文件夹
[root@vm-xiluhua][/etc]$ find / -name "mysql" /var/lib/mysql /var/lib/mysql/mysql /usr/lib/python2.7/site-packages/clufter/filters/cluster/rm/mysql /usr/lib/ocf/resource.d/heartbeat/mysql /usr/lib64/perl5/vendor_perl/auto/DBD/mysql /usr/lib64/perl5/vendor_perl/DBD/mysql /usr/share/resource-agents/ocft/configs/mysql /usr/share/mysql /usr/local/mysql
删除
[root@vm-xiluhua][/etc]$ rm -rf /var/lib/mysql
再次查看还剩下有哪些
[root@vm-xiluhua][/etc]$ find / -name "mysql" /usr/lib/python2.7/site-packages/clufter/filters/cluster/rm/mysql # 非直属包下的,就留着 /usr/lib/ocf/resource.d/heartbeat/mysql # 非直属包下的,就留着 /usr/lib64/perl5/vendor_perl/auto/DBD/mysql # 非直属包下的,就留着 /usr/lib64/perl5/vendor_perl/DBD/mysql # 非直属包下的,就留着 /usr/share/resource-agents/ocft/configs/mysql # 非直属包下的,就留着 /usr/local/mysql #也可以删除,因为这本还有进一步测试,所以留着