参考博文:https://blog.csdn.net/jesse919/article/details/80662566 致谢!
Centos 6.10中安装MySQL 5.6
- 移除CentOS默认的mysql-libs
[root@localhost etc]# whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql
[root@localhost etc]# yum remove mysql-libs
在此过程中如果出现 Is this ok [y/N]: 输入y后回车即可
出现 Complete! 表示移除成功
- 清空dbcache
[root@localhost etc]# yum clean dbcache
Loaded plugins: fastestmirror
Cleaning repos: ISO
3 sqlite files removed
- 下载MySQL rpm安装包*(此过程因网络较慢,可能需要耐心等待)*
#先切换到你要将安装包下载的目录(个人喜好, 避免下载后不知道下载到哪里去了)
[root@localhost etc]# cd /home/myfiles/
#mysql 下载地址 http://repo.mysql.com/ 可根据需要自己选择, 此教程使用如下下载链接
[root@localhost myfiles]# wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
--2018-06-12 10:40:29-- http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
Resolving repo.mysql.com... 223.119.236.209
Connecting to repo.mysql.com|223.119.236.209|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5824 (5.7K) [application/x-redhat-package-manager]
Saving to: “mysql-community-release-el6-5.noarch.rpm”
100%[====================================================================================================================================================================================================================================>] 5,824 --.-K/s in 0s
2017-05-08 10:40:29 (298 MB/s) - “mysql-community-release-el6-5.noarch.rpm” saved [5824/5824]
[root@localhost myfiles]#
#安装下载好的rpm文件
[root@localhost myfiles]# rpm -ivh mysql-community-release-el6-5.noarch.rpm
Preparing... ########################################### [100%]
1:mysql-community-release########################################### [100%]
[root@localhost myfiles]#
#yum安装mysql-community-server
[root@localhost myfiles]# yum install mysql-community-server
在此过程中如果出现 Is this ok [y/N]: 输入y后回车即可
出现 Complete! 表示安装成功
- 以上步骤完成后即可启动MySql服务
#启动服务命令
[root@localhost myfiles]# service mysqld start
在此过程中出现 Starting mysqld: [ OK ]
表示启动成功
#停止服务命令
[root@localhost myfiles]# service mysqld stop
在此过程中出现 Stopping mysqld: [ OK ]
表示停止服务成功
#重启命令
[root@localhost myfiles]# service mysqld restart
在此过程中出现
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
表示重启服务成功
- 修改密码
默认密码是空的,为了安全起见,需要重新设置root密码
#登录mysql
[root@localhost ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
#选择mysql数据库
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
Database changed
#修改密码
mysql> update user set password=PASSWORD("YOUR_PASSWORD") where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
#使修改生效
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#退出mysql登录
mysql> quit
Bye
- 验证修改后的root密码
#用原来不需要密码的方式登录, 无法登录
[root@localhost ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
#使用修改后的密码登录, 登录成功
mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
- 修改mysql配置文件
my.cnf是mysql启动时加载的配置文件,一般会放在mysql的安装目录中,也可以放在其他目录加载。
安装mysql后,系统中会有多个my.cnf文件。(如果按照本文中的安装步骤,一般只会存在一个my.cnf,路径为 /etc/my.cnf)
使用locate my.cnf命令可以列出所有的my.cnf文件
[root@localhost ~]# locate my.cnf
/etc/my.cnf
1). 查看是否使用了指定目录的my.cnf
启动mysql后,我们查看mysql的进程,看看是否有设置使用指定目录的my.cnf文件,如果有则表示mysql启动时是加载了这个配置文件。
[root@localhost ~]# ps aux|grep mysql|grep 'my.cnf'
#如果指定了则返回类似如下信息
fdipzone 25174 0.0 0.0 3087244 600 ?? S 4:12下午 0:01.14 /usr/local/Cellar/mysql/5.6.24/bin/mysqld --defaults-file=/usr/local/Cellar/mysql/5.6.24/my.cnf --basedir=/usr/local/Cellar/mysql/5.6.24 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/5.6.24/lib/plugin --bind-address=127.0.0.1 --log-error=/usr/local/var/mysql/TerrydeMacBook-Air.local.err --pid-file=/usr/local/var/mysql/TerrydeMacBook-Air.local.pid
fdipzone 25064 0.0 0.0 2452824 4 ?? S 4:12下午 0:00.03 /bin/sh /usr/local/opt/mysql/bin/mysqld_safe --defaults-file=/usr/local/Cellar/mysql/5.6.24/my.cnf --bind-address=127.0.0.1 --datadir=/usr/local/var/mysql
可以看到/usr/local/Cellar/mysql/5.6.24/my.cnf就是mysql指定的启动加载的配置文件。
#如果上面的命令没有输出,表示没有设置使用指定目录的my.cnf。(如下, 如果按照本文中的安装步骤为此情况)
[root@localhost ~]# ps aux|grep mysql|grep 'my.cnf'
[root@localhost ~]#
2). 查看mysql默认读取my.cnf的目录
如果没有设置使用指定目录的my.cnf,mysql启动时会读取安装目录根目录及默认目录下的my.cnf文件。
#查看mysql启动时读取配置文件的默认目录
[root@localhost ~]# mysql --help|grep 'my.cnf'
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
[root@localhost ~]#
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf 这些就是mysql默认会搜寻my.cnf的目录,顺序排前的优先。
本文中则使用的为 /etc/my.cnf
3). 启动时没有使用配置文件
a. 如果没有设置使用指定目录my.cnf文件及默认读取目录没有my.cnf文件,表示mysql启动时并没有加载配置文件,而是使用默认配置。
b. 需要修改配置,可以在mysql默认读取的目录中,创建一个my.cnf文件(例如:/etc/my.cnf),把需要修改的配置内容写入,重启mysql后即可生效。
注意: 如果按照本文中的安装步骤则配置文件为 /etc/my.cnf
- 设置MySQL允许外网访问
MySQL 安装后默认外网是无法访问的, 需修改配置文件后才可以。
Navicat连接虚拟机中的MySQL
首先配置CentOS下防火墙iptables规则:
向其中加入下列规则:
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
# vim /etc/sysconfig/iptables
文件内容如下:
说明:防火墙开放http用的80端口和连接MySql的3306端口。
# service iptables restart
然后配置MySQL允许远程登陆:
先在CentOS中连接Mysql数据库
# mysql -uroot -proot
执行
> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
再执行下
> flush privileges;
不用重启MySql服务。最后配置工作完成,打开Windows下的Navicat就可以直接连接到CentOS下的数据库了。
查看虚拟机中的linux ip:
# ip addr
如下:192.168.231.128
打开Navicat进行连接测试:
按照对应的设置输入,测试连接成功。