centos下彻底删除MYSQL 和重新安装MYSQL的解压缩版

来源:互联网 发布:手机版淘宝的新品上架 编辑:程序博客网 时间:2024/05/24 04:15

centos下彻底删除MYSQL 和重新安装MYSQL


 删除Mysql


   yum remove  mysql mysql-server mysql-libs mysql-server;

    find / -name mysql 将找到的相关东西delete掉;

    rpm -qa|grep mysql(查询出来的东东yum remove掉)

安装Mysql


1、下载安装包

    去官网下载mysql的tar.gz的文件


2.检查库文件是否存在,如有删除。

[root@localhost ~]# rpm -qa | grep mysql


3.检查mysql组和用户是否存在,如无创建。


[root@localhost mysql]# rpm -qa | grep mysql
[root@localhost mysql]# cat /etc/group | grep mysql
mysql:x:1001:
[root@localhost etc]# cat /etc/passwd | grep mysql
mysql:x:991:1001::/home/mysql:/bin/bash

若没有就创建
[root@localhost mysql]# groupadd mysql          #创建一个mysql组
[root@localhost mysql]# useradd -r -g mysql mysql     #往组里面添加一个用户,
-r表示mysql是系统用户,不可用于登陆系统,-g表示组 mysql 第二个mysql表示用户;


4.解压tar.gz包,更改所属的组和用户


我的 mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz 文件是放在下面这个目录下面的
[root@localhost mysql]# pwd
/home/software/mysql
[root@localhost mysql]# tar -zxvf mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
mysql-5.7.10-linux-glibc2.5-x86_64/
mysql-5.7.10-linux-glibc2.5-x86_64/include/
mysql-5.7.10-linux-glibc2.5-x86_64/include/my_dir.h
mysql-5.7.10-linux-glibc2.5-x86_64/include/mysql.h
mysql-5.7.10-linux-glibc2.5-x86_64/include/my_byteorder.h
mysql-5.7.10-linux-glibc2.5-x86_64/include/my_list.h
...
...
mysql-5.7.10-linux-glibc2.5-x86_64/man/man1/mysqlshow.1
mysql-5.7.10-linux-glibc2.5-x86_64/man/man1/zlib_decompress.1
mysql-5.7.10-linux-glibc2.5-x86_64/man/man1/mysql_install_db.1

[root@localhost mysql]# mv mysql-5.7.10-linux-glibc2.5-x86_64 mysql5.7
[root@localhost mysql]# ls -l
total 535920
drwxr-xr-x. 9 7161 wheel      4096 Nov 30  2015 mysql5.7
-rwxrwxrwx. 1 root root  548774698 Dec 24  2015 mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
# 将 mysql5.7/ 目录包含所有的子目录和文件,所有者改变为mysql,所属组改变为mysql
[root@localhost mysql]# chown -R mysql mysql5.7/
[root@localhost mysql]# chgrp -R mysql mysql5.7/
[root@localhost mysql]# ls -l
total 535920
drwxr-xr-x. 9 mysql mysql      4096 Nov 30  2015 mysql5.7
-rwxrwxrwx. 1 root  root  548774698 Dec 24  2015 mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
[root@localhost mysql]# cd mysql5.7/



5.安装和初始化数据库



[root@localhost mysql5.7]# bin/mysql_install_db --user=mysql --basedir=/home/software/mysql/mysql5.7/ --datadir=/home/software/mysql/mysql5.7/data/
2017-05-02 22:04:13 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2017-05-02 22:04:17 [WARNING] The bootstrap log isn't empty:
2017-05-02 22:04:17 [WARNING] 2017-05-02T14:04:13.971980Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead

[root@localhost mysql5.7]# cp -a ./support-files/my-default.cnf  /etc/my.cnf
[root@localhost mysql5.7]# cp -a ./support-files//mysql.server /etc/init.d/mysqld
[root@localhost mysql5.7]# cd bin
[root@localhost bin]# ./mysqld_safe --user=mysql&
[1] 4687
[root@localhost bin]# ./mysqld_safe: line 522: /usr/local/mysql/data/mysqld_safe.pid: No such file or directory
awk: (FILENAME=- FNR=1) warning: error writing standard output (Broken pipe)
170502 22:07:37 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
touch: cannot touch ‘/usr/local/mysql/data/localhost.localdomain.err’: No such file or directory
chmod: cannot access ‘/usr/local/mysql/data/localhost.localdomain.err’: No such file or directory
touch: cannot touch ‘/usr/local/mysql/data/localhost.localdomain.err’: No such file or directory
chown: cannot access ‘/usr/local/mysql/data/localhost.localdomain.err’: No such file or directory
170502 22:07:37 mysqld_safe The file /usr/local/mysql/bin/mysqld
does not exist or is not executable. Please cd to the mysql installation
directory and restart this script from there as follows:
./bin/mysqld_safe&
See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information
./mysqld_safe: line 130: /usr/local/mysql/data/localhost.localdomain.err: No such file or directory
^C
[1]+  Exit 1                  ./mysqld_safe --user=mysql

 # 执行上面命令时报错:是因为我使用的是默认的配置文件默认的路径,而我的安装目录不是在默认路径/usr/local/mysql下,所以还要执行下面的命令:

[root@localhost etc]# less my.cnf
[root@localhost etc]# vi my.cnf
[root@localhost etc]# vi init.d/mysqld
[root@localhost etc]# mkdir -p /usr/local/mysql/bin
[root@localhost etc]# ln -s /home/software/mysql/mysql5.7/bin/mysqld /usr/local/mysql/bin/mysqld
[root@localhost bin]# ./mysqld_safe --user=mysql&
[1] 4904
[root@localhost bin]# 170502 22:14:42 mysqld_safe Logging to '/home/software/mysql/mysql5.7/data/localhost.localdomain.err'.
170502 22:14:42 mysqld_safe Starting mysqld daemon with databases from /home/software/mysql/mysql5.7/data
[root@localhost bin]# /etc/init.d/mysqld restart
Shutting down MySQL..170502 22:15:53 mysqld_safe mysqld from pid file /home/software/mysql/mysql5.7/data/localhost.localdomain.pid ended
 SUCCESS!
Starting MySQL. SUCCESS!
[1]+  Done                    ./mysqld_safe --user=mysql
# 把mysql设置为开机启动,启动层次是图形界面和命令行模式
[root@localhost bin]# chkconfig --level 35 mysqld on

6.初始化密码


mysql5.7会生成一个初始化密码,而在之前的版本首次登陆不需要登录。


[root@localhost bin]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2017-05-02 22:04:13
rG>oDYxpsA4u
[root@localhost bin]# ./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.10

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
# 会提示你去重新设置密码才能执行这个语句
# 重新设置密码
mysql> SET PASSWORD = PASSWORD('root');
Query OK, 0 rows affected, 1 warning (0.01 sec)
# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)






7.添加远程访问权限



mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> select host, user from user;
+-----------+-----------+
| host      | user      |
+-----------+-----------+
| localhost | mysql.sys |
| localhost | root      |
+-----------+-----------+
2 rows in set (0.00 sec)

mysql> update set host = '%' where user = 'root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set host = '%' where user = 'root'' at line 1
mysql> update set user host = '%' where user = 'root';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set user host = '%' where user = 'root'' at line 1
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host, user from user;
+-----------+-----------+
| host      | user      |
+-----------+-----------+
| %         | root      |
| localhost | mysql.sys |
+-----------+-----------+
2 rows in set (0.00 sec)

mysql> /etc/init.d/mysqld restart
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/etc/init.d/mysqld restart' at line 1
mysql> exit
Bye
[root@localhost bin]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!



0 0