参考: http://www.2cto.com/database/201501/371451.html
安装环境 CentOS版本:CentOS-7
因为之前安装过,没有成功,但是有之前安装的文件,要先卸载
网上找了一个卸载的过程如下:
a)查看系统中是否以rpm包安装的mysql:
[root@centos7 ~]# rpm -qa | grep -i mysql
MySQL-server-5.6.17-1.el6.i686
MySQL-client-5.6.17-1.el6.i686
b)卸载mysql
[root@centos7 ~]# rpm -e MySQL-server-5.6.17-1.el6.i686
[root@centos7 ~]# rpm -e MySQL-client-5.6.17-1.el6.i686
c)删除mysql服务
[root@centos7 ~]# chkconfig --list | grep -i mysql
[root@centos7 ~]# chkconfig --del mysql
d)删除分散mysql文件夹
[root@centos7 ~]# whereis mysql 或者 find / -name mysql
mysql: /usr/lib/mysql /usr/share/mysql
清空相关mysql的所有目录以及文件
[root@centos7 ~]#rm -rf /usr/lib/mysql
[root@centos7 ~]#rm -rf /usr/share/mysql
[root@centos7 ~]#rm -rf /usr/my.cnf
下载并安装 官网下载
1. 解压下载的zip包,会发现有以下几个rpm包:
MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-embedded-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-shared-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-shared-compat-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-test-advanced-5.6.22-1.el7.x86_64.rpm
2. 卸载MariaDB(这一步没操作过)
如果直接点击rpm包安装会得到错误提示。因为CentOS的默认数据库已经不再是MySQL了,而是MariaDB,为什么呢?
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。
查看当前安装的mariadb包:
[root@centos7 ~]# rpm -qa | grep mariadb
将它们统统强制性卸载掉:
[root@centos7 ~]# rpm -e --nodeps mariadb-libs-5.5.35-3.el7.x86_64
[root@centos7 ~]# rpm -e --nodeps mariadb-5.5.35-3.el7.x86_64
[root@centos7 ~]# rpm -e --nodeps mariadb-server-5.5.35-3.el7.x86_64
3. 安装MYSQL
按顺序执行
rpm -ivh MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm
rpm -ivh MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm
rpm -ivh MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm
完成..
4. 启动MYSQL
[root@centos7 mysql]#service mysql start
查看MySQL运行状态:
[root@centos7 mysql]# service mysql status
SUCCESS! MySQL running (14158)
5. 默认root用户登录MYSQL
[root@centos7 mysql]# mysql -u root -p
Enter password:
ERROR 1045 (28000):Access denied for user 'root'@'localhost' (using password: YES)
发现有有错误,然后在网上查了一下说使用下面命令修改root初始化密码:
[root@centos7 mysql]# /usr/bin/mysqladmin -u root password 'passok'
/usr/bin/mysqladmin: connect to server at'localhost' failed
error: 'Accessdenied for user 'root'@'localhost' (using password: NO)'
发现MYSQL数据库默认的root用户还是没办法设置密码进行登录,需要做一下操作:
重置MySQL中root用户密码及验证
还是不行,然后在网上又找到一个重置MySQL中root用户密码及验证的方法:
(1) 停止MySQL服务
[root@centos7 mysql]# service mysql stop
Shutting down MySQL.. SUCCESS!
(2) 输入绕过密码认证命令
[root@centos7 mysql]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
[1] 5807
150117 22:23:31 mysqld_safe Logging to '/var/lib/mysql/bogon.err'.
150117 22:23:31 mysqld_safe Starting mysqlddaemon with databases from /var/lib/mysql
(3) 输入登录用户命令
[root@centos7 mysql]# mysql -u root mysql
Reading table information for completion oftable and column names
You can turn off this feature to get aquicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version:5.6.22-enterprise-commercial-advanced MySQL Enterprise Server - AdvancedEdition (Commercial)
Copyright (c) 2000, 2014, Oracle and/or itsaffiliates. All rights reserved.
Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarksof their respective
owners.
Type 'help;' or '\h' for help. Type '\c' toclear the current input statement.
(4)添加用户并赋予权限
mysql> use mysql;
mysql> create database MyShop; //创建一个数据库
//创建一个用户www并具有操作MyShop数据库的权限
mysql> grant all privileges on MyShop.* to 'www'@'localhost' identified by '888888';
mysql> flush privileges;
来查看一下权限
mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM user;
+------------------------------+
| query |
+------------------------------+
| User: 'root'@'127.0.0.1'; |
| User: 'root'@'::1'; |
| User: 'root'@'centos7'; |
| User: 'www'@'localhost'; |
| User: 'root'@'localhost'; |
+------------------------------+
6 rows in set (0.00 sec)
mysql> show grants for 'www'@'localhost';
+----------------------------------------------------------------------------------------------------------------+
| Grants for www@localhost |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'www'@'localhost' IDENTIFIED BY PASSWORD '*8E02B7AE57F9A19E165EB45CD3F705BF66985B85' |
| GRANT ALL PRIVILEGES ON `MyShop`.* TO 'www'@'localhost' |
+----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
(5) 输入修改root密码SQL语句
mysql> udpate user SET Password=PASSWORD('root') where USER='root';
Query OK, 4 rows affected (0.04 sec)
Rows matched: 4 Changed: 4 Warnings: 0
(6) 输入数据刷新命令
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
(7) 退出
mysql> quit
Bye
(8) 启动MYSQL
[root@centos7 mysql]# service mysql start
Starting MySQL SUCCESS!
登录mysql,查看所有数据库:
[root@centos7 mysql]# mysql -u root -p
mysql> show databases;
ERROR 1820 (HY000):You must SET PASSWORD before executing this statement
提示要再设置一下密码:
mysql> SET PASSWORD = PASSWORD('passok');
Query OK, 0 rows affected (0.00 sec)
显示数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
进入数据库创建表、显示表:
mysql> use test;
Database changed
mysql> show tables;
Empty set (0.02 sec)
mysql>create table testTable(name char(15) not null,passwd char(15) not null);
Query OK, 0 rows affected (0.87 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| testTable |
+----------------+
1 row in set (0.00 sec)
mysql安装后三个主要的目录及其功能:
/var/lib/mysql 数据库文件
/usr/share/mysql 命令及配置文件
/usr/bin mysqladmin、mysqldump等命令
设置mysql开机自启动
设置开机启动服务选择使用chkconfig命令,可以看到我们永久性关闭iptables就用的这个命令,命令的格式如下:
chkconfig 功能说明:检查,设置系统的各种服务。
语法:chkconfig [--add][--del][--list][系统服务]或 chkconfig [--level <等级代号>][系统服务][on/off/reset]
--add 添加服务
--del 删除服务
--list 查看各服务启动状态
我这里安装好了mysql之后默认就是开机自启动的:
[root@centos7 mysql]# chkconfig --list mysql
注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
欲查看对特定 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysql 0:关 1:关 2:开 3:开 4:开 5:开 6:关
如果不是开机自启动,使用开启MySQL服务自动开启命令:
chkconfig mysqld on
chkconfig mysql on
搜集的安装过程中遇到的问题:
1: 启动时
[root@centos7 mysql]#service mysql start
得到错误:ERROR!The server quit without updating PID file
这里主要是因为:selinux惹的祸,如果是centos系统,默认会开启selinux。
解决方法是关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器。
然后再启动mysql就没问题了:
2:
FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:
初始化mysql数据库提示缺少Data:dumper模块解决方法
Data::Dumper
解决方法 :安装autoconf库
命令:yum-y install autoconf
安装完autoconf库,再运行
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/
3: 启动时:Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
没有权限:
chown -R mysql.mysql /var/lib/mysql ##该命令仅为示例,其中/data/mysql就是mysql配置文件中datadir的目录 ##若为空,则默认为mysql安装目录下的data文件夹下