CentOS 7.0 MySQL 5.6.19安装与卸载脚本小结

来源:互联网 发布:蜂窝移动网络 4g 编辑:程序博客网 时间:2024/06/05 13:28

前言

最近实践在CentOS7中通过rpm的方式安装MySQL,期间遇到安装成功但没有/root/.mysql_secret文件导致没有初始随机密码登陆,与mariadb冲突,缺少pid文件无法启动等问题,通过查阅网上资料也一一解决了,现在将这些做总结记录一下;

软件环境

系统环境:[root@centos7 scripts]# uname -aLinux centos7 3.10.0-123.el7.x86_64MySQL版本:MySQL-client-5.6.19-1.linux_glibc2.5.x86_64.rpmMySQL-server-5.6.19-1.linux_glibc2.5.x86_64.rpm

安装MySQL脚本

#!/bin/bash# Program:#       This script is to install MySQL# History:#       2016/01/20 yuu function installMysqlServer(){        # 解决与mariadb冲突问题        yum remove -y mysql-libs        #不安装这个会导致没有secret文件以及启动mysql报错        yum install -y perl-Module-Install.noarch        rpm -ivh Packages/MySQL-server* Packages/MySQL-client*        # 开放端口3306        /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT        service mysql start}installMysqlServer

卸载MySQL脚本

#!/bin/bash# Program:#       This script is to uninstall MySQL completely.# History:#       2016/01/20 yuu function uninstallMySQL(){        echo "### begin to uninstall mysql ###"        service mysql stop #systemctl stop mysql.service        chkconfig --del mysql        # 使用rpm -qa | grep -i mysql可以查看到安装到的mysql的包,        # -i表示忽略大小写        yum -y remove mysql mysql-server mysql-libs mysql-devel        # 该句非常重要,不删除下次重装不会更新随机密码        rm -rf /var/lib/mysql        rm -rf /usr/lib64/mysql        rm -rf /usr/share/mysql        rm -rf /root/.mysql_secret        rm -rf /usr/my.cnf        echo "uninstall mysql finished"}uninstallMySQL

MySQL部分其它操作

# 开启了mysql服务之后就可以登陆进去修改密码:# 查看随机密码[root@centos7 Setup]# cat /root/.mysql_secret # The random password set for the root user at Wed Jan 20 22:38:27 2016 (local time): _Bw4BzVinm7EY0aD[root@centos7 Setup]# mysql -uroot -pEnter password:  # 输入/root/.mysql_secret中的随机密码登陆# 登陆进去之后修改密码Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> SET PASSWORD = PASSWORD('123456');Query OK, 0 rows affected (0.00 sec)# 允许root用户远程登陆数据库[root@centos7 ~]# mysql -uroot -p123456 -e "update mysql.user set host = '%' where host = 'localhost'"                  
# 查看上面修改远程访问后数据库表的字段值[root@centos7 ~]# mysql -uroot -p123456 -e "select * from mysql.user \G"Warning: Using a password on the command line interface can be insecure.*************************** 1. row ***************************                  Host: %                  User: root# 这时需要重启mysql才能远程登陆生效,注意开启3306端口,否则不能远程访问[root@centos7 ~]# service mysql restart安装脚本里面的:[root@centos7 ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT     # 让MySQL执行存在的.sql文件# 其中testDB为数据库名称,test.sql为当前目录对应的sql文件[root@centos7 ~]# mysql -uroot -p123456 testDB < test.sql

参考资料:

http://blog.csdn.net/typa01_kk/article/details/49057073
http://my.oschina.net/zhangjie830621/blog/466279
http://dev.mysql.com/doc/refman/5.7/en/mysql-install-db.html

0 0
原创粉丝点击