解决系统自带mysql卸载不干净或因已存在系统自带mysql导致无法安装需要的mysql问题
系统:CentOS 6.9,MySQL:MySql 5.6
一、卸载
查看已经安装的服务:
rpm -qa|grep -i mysql
-i 作用是不区分大小写
可能出现结果:mysql-5.1.73-8.el6_8.x86_64
或者通过yum在线安装过的会出现:
mysql-libs-5.1.73-8.el6_8.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
mysql-server-5.1.73-8.el6_8.x86_64
mysql-5.1.73-8.el6_8.x86_64
执行如下命令
yum remove mysql mysql-server mysql-libs compat-mysql51
rm -rf /var/lib/mysql
rm /etc/my.cnf
查看是否还有mysql软件:
rpm -qa|grep mysql
有的话继续删除,可能出现的:
mysql-libs-5.1.73-8.el6_8.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
mysql-server-5.1.73-8.el6_8.x86_64
mysql-5.1.73-8.el6_8.x86_64
删除方式:
rpm -e 包名(即:上面的四个)//普通删除
rpm -e –nodeps 包名
查看残留的目录:
whereis mysql
或者:find / -name mysql
/etc/rc.d/init.d/mysql
/etc/logrotate.d/mysql
/usr/local/Java/mysql
/usr/bin/mysql
/usr/share/mysql
/usr/include/mysql
/usr/include/mysql/mysql
/usr/lib64/mysql
/var/lock/subsys/mysql
/var/lib/mysql
/var/lib/mysql/mysql
然后删除mysql目录:rm -rf 目录
最后执行whereis mysql
结果为:mysql:
卸载完成!
二:安装
以rmp安装包为例
下载地址: https://dev.mysql.com/downloads/mysql/5.6.html
MySQL-client-5.6.36-1.el6.x86_64.rpm
MySQL-devel-5.6.36-1.el6.x86_64.rpm
MySQL-server-5.6.36-1.el6.x86_64.rpm
1.安装rpm包
rpm -ivh MySQL-client-5.6.36-1.el6.x86_64.rpm
rpm -ivh MySQL-devel-5.6.36-1.el6.x86_64.rpm
rpm -ivh MySQL-server-5.6.36-1.el6.x86_64.rpm
或者
rpm -ivh MySQL-*
由于电脑或其他一些原因导致使用简化的安装命令会导致安装无法100%完成,只能逐个安装
2.执行安装语句:
[root @localhost opt]# rpm -ivh MySQL-*
Preparing… ########################################### [100%]
1:MySQL-server ########################################### [100%]
……………………………………………………………..
………………………省略中间过程…………………….
………………………………………………………………..
……………………….注意阅读下面内容………………..
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in ‘/root/.mysql_secret’.
You must change that password on your first connect,
no other statement but ‘SET PASSWORD’ will be accepted.
See the manual for the semantics of the ‘password expired’ flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database.
This is strongly recommended for production servers.
See the manual for more instructions.
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
3.复制配置文件
cp /usr/share/mysql/my-default.cnf /etc/my.cnf
4.初始化mysql
/usr/bin/mysql_install_db
5.启动mysql服务器
service mysql start
结果一:
Staring MySQL .......SUCCESS!
结果二:
Starting MySQL Logging to ‘/var/lib/mysql/localhost.err’...
...........SUCCESS!
6.查看当前运行状态:
service mysql status
7.查看初始密码
cat /root/.mysql_secret
8.使用初始密码进入mysql
mysql -uroot -p初始密码
9.修改初始密码(在mysql下)
set PASSWORD = PASSWORD(‘新密码’);
10.退出:
exit;
三、其他
1.设置开机启动
chkconfig mysql on
2.优化/etc/my.cnf配置
①打开my.cnf
vi /etc/my.cnf
②在配置文件中编辑
[mysqld]
port = 3306
default-storage-engine = InnoDB
character_set_server=utf8
character_set_client=utf8
collation-server=utf8_general_ci
lower_case_table_names=1 #表名大小写:0--区分,1--不区分
max_connections=1000 #设置最大连接,允许最大连接为16384
[client]
password = root
port = 3306
default-character-set=utf8
[mysql]
default-character-set = utf8
上述变量可能有不存在的导致登录时报错,可查看mysql日志根据错误修改配置
应用此优化配置后执行:rpm –qa|grep -i mysql 的结果如下
[root@localhost ~]# rpm -qa|grep -i mysql
MySQL-server-5.6.36-1.el6.x86_64
MySQL-devel-5.6.36-1.el6.x86_64
MySQL-client-5.6.36-1.el6.x86_64
3.关闭mysql服务器
service mysql stop
4.mysql默认安装位置
/var/lib/mysql/ #数据库目录
/usr/share/mysql #配置文件目录
/usr/bin #相关命令目录
/etc/init.d/mysql #启动脚本