centos 安装mysql数据库

一、环境

Linux操作系统:centos6.9

安装mysql版本: 5.6

 

二、安装步骤

1、检查和卸载centos原有的mysql

   ① 输入以下命令查看centos自带的mysql

[root@VM_0_11_centos ~]# rpm -qa | grep mysql
mysql-libs-5.1.73-8.el6_8.x86_64

  复制  mysql-libs-5.1.73-8.el6_8.x86_64

   ② 输入命令  删除 mysql-libs-5.1.73-8.el6_8.x86_64

[root@VM_0_11_centos ~]# rpm -e --nodeps mysql-libs-5.1.73-8.el6_8.x86_64
[root@VM_0_11_centos ~]# 

 

2、下载mysql5.6 的rpm包

     ① mysql 官网下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-5.6.40-1.el6.x86_64.rpm-bundle.tar

     ② 复制上面链接,在linux命令窗口输入,然后等待下载完成

[root@VM_0_11_centos ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-5.6.40-1.el6.x86_64.rpm-bundle.tar

3、解压tar文件 MySQL-5.6.40-1.el6.x86_64.rpm-bundle.tar 

[root@VM_0_11_centos ~]# tar -xvf MySQL-5.6.40-1.el6.x86_64.rpm-bundle.tar 
MySQL-shared-compat-5.6.40-1.el6.x86_64.rpm
MySQL-client-5.6.40-1.el6.x86_64.rpm
MySQL-embedded-5.6.40-1.el6.x86_64.rpm
MySQL-test-5.6.40-1.el6.x86_64.rpm
MySQL-server-5.6.40-1.el6.x86_64.rpm
MySQL-shared-5.6.40-1.el6.x86_64.rpm
MySQL-devel-5.6.40-1.el6.x86_64.rpm

4、 安装服务端

[root@VM_0_11_centos ~]# rpm -ivh MySQL-server-5.6.40-1.el6.x86_64.rpm

5、 安装客户端

[root@VM_0_11_centos ~]# rpm -ivh MySQL-client-5.6.40-1.el6.x86_64.rpm

三、配置mysql

1、 启动mysql服务

[root@VM_0_11_centos ~]# service mysql start
Starting MySQL.Logging to '/var/lib/mysql/VM_0_11_centos.err'.
 SUCCESS! 

2、 查看mysql随机生成的默认密码

[root@VM_0_11_centos ~]# cat /root/.mysql_secret

3、使用刚刚查询出来的密码登录mysql

[root@VM_0_11_centos ~]# mysql -u root -p

4、 设置新的mysql登录密码

mysql> set password = password('yazhishaw');
Query OK, 0 rows affected (0.00 sec)

5、 开启mysql远程登录的权限

mysql> grant all privileges on *.* to 'root' @'%' identified by 'yazhishaw';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

6、退出mysql客户端,设置开机自动启动mysql服务

mysql> exit;
Bye
[root@VM_0_11_centos ~]# chkconfig --add mysql
[root@VM_0_11_centos ~]# chkconfig mysql on

四、 开放linux的对外访问端口3306(mysql的默认端口)

[root@VM_0_11_centos ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[root@VM_0_11_centos ~]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]

(PS: 如果是购买的腾讯云、阿里云之类的服务器需要在控制台上设置安全组开放3306)

五、 设置mysql数据库的编码为utf8mb4

1、查找mysql配置文件my.cnf

[root@VM_0_11_centos ~]# find / -name my.cnf
/usr/my.cnf

2、 编辑mysql配置文件my.cnf  打开上面路径


[root@VM_0_11_centos ~]# vi /usr/my.cnf

3、修改配置文件内容,没有的添加上去

[client] 
default-character-set = utf8mb4 
[mysql] 
default-character-set = utf8mb4 
[mysqld] 
character-set-client-handshake = FALSE 
character-set-server = utf8mb4 
collation-server = utf8mb4_unicode_ci 
init_connect='SET NAMES utf8mb4'

4、 修改后保存配置文件,重启mysql服务器

[root@VM_0_11_centos ~]# service mysql restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS!

 

六、 测试远程连接mysql

1、 打开navicat 工具,远程连接mysql测试

 

七、mysql密码重置

1.修改MySQL的登录设置
vim /etc/my.cnf 
在[mysqld]的段中加上一句:skip-grant-tables 

 

2.重新启动mysqld 
# service mysqld restart 
Stopping MySQL: [ OK ] 
Starting MySQL: [ OK ]

3.登录并修改MySQL的root密码 
# mysql 
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 3 to server version: 3.23.56 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
mysql> USE mysql ; 
Database changed 
mysql> UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ; 
Query OK, 0 rows affected (0.00 sec) 
Rows matched: 2 Changed: 0 Warnings: 0 
mysql> flush privileges ; 
Query OK, 0 rows affected (0.01 sec) 
mysql> quit

4.将MySQL的登录设置修改回来 
# vim /etc/my.cnf 
将刚才在[mysqld]的段中加上的skip-grant-tables删除 
保存并且退出vim

5.重新启动mysqld 
# service mysqld restart 
Stopping MySQL: [ OK ] 
Starting MySQL: [ OK ]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值