centos6 安装mysql 5.6 数据库及配置

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(1702)   2023-03-28 11:29:14

一、用户切换

通过远程工具链接到centos6.5服务器后,切换至root用户。如果已经是root用户则忽略该步骤。

切换root用户步骤:

执行命令:

su –

回车

输入root密码

 

密码输入成功则已经切换至root用户,失败则提示鉴定错误。重新执行上面的命令直到密码正确进入root用户

切换成功后可以见到由$变成了#

二、检查系统中是否存在MySQL或者MySQL依赖

检查当前系统是否已经安装MySQL或者MySQL的相关库,如果已经安装MySQL数据库则询问相关人员是否允许卸载重新安装。如果存在MySQL的依赖库则必须先卸载,否则后面可能会因为当前依赖库的版本与后面安装的版本不一致导致安装冲突,最终导致安装失败。

查看系统是否安装MySQL相关

命令:

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

mysql-libs-5.1.71-1.el6.x86_64

[root@localhost ~]#

删除历史MySQL相关包

当前系统没有安装MySQL但是有一个依赖包,将其先卸载

卸载命令:

[root@localhost ~]# rpm -e --nodeps  mysql-libs-5.1.71-1.el6.x86_64

查看是否已经卸载,可执行上面的查看命令

三、安装MySQL数据库

通过yum安装MySQL(注意系统必须连接外网)

3.1首先安装MySQLyum源(MySQL yum源官方地址https://dev.mysql.com/downloads/repo/yum/

安装命令:

[root@bogon ~]# rpm -Uvh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

Retrieving http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

Preparing...                ########################################### [100%]

   1:mysql-community-release########################################### [100%]

[root@bogon ~]#

安装MySQL yum源

或者

命令:

yum install -y http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

 

3.2首先使用yum查看可以安装的MySQL版本

命令:

[root@localhost ~]# yum list|grep mysql

查看可以选择安装的MySQL版本

 

3.3通过yum安装mysql5.6.37版本

安装命令:

[root@localhost ~]# yum install -y mysql-community-server.x86_64

安装MySQL5.6

四、启动MySQL服务

启动MySQL服务命令

[root@localhost ~]# service mysqld start

重点日志:

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:



  /usr/bin/mysqladmin -u root password 'new-password'

  /usr/bin/mysqladmin -u root -h bogon password 'new-password'



Alternatively you can run:



  /usr/bin/mysql_secure_installation

初次启动MySQL服务,会提示执行相关命令设置root用户密码

启动MySQL服务

五、配置MySQL

5.1设置MySQL的默认密码

根据初次的启动提示,进行设置默认密码,当前版本为5.6.37设置默认密码的方式如下:

将默认密码设置为root

[root@localhost ~]# /usr/bin/mysqladmin -u root password root

 

5.2登录root用户

登录root命令:

[root@localhost ~]# mysql -uroot –proot

root用户登录

退出登录命令

Mysql>exit

5.3配置MySQL的配置文件my.cnf

5.3.1配置数据库字符集utf8mb

编辑my.cnf文件

[root@localhost ~]# vi /etc/my.cnf

默认内容

默认配置文件内容

 

配置字符集utf8mb4

配置后内容如下:

[client]
default-character-set = utf8mb4



[mysql]
default-character-set = utf8mb4


[mysqld]

character-set-client-handshake = FALSE

character-set-server = utf8mb4

collation-server = utf8mb4_general_ci

init_connect='SET NAMES utf8mb4'

修改后内容:

设置字符集

注意:MySQL版本5.5以上才支持utf8mb4

重启MySQL服务
命令
:

[root@bogon ~]# service  mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@bogon ~]#

5.3.2配置MySQL最大连接数

编辑my.cnf文件,

[root@localhost ~]# vi /etc/my.cnf

[mysqld]节点中添加

max_connections = 1000

设置最大链接数

该配置标识MySQL数据库的最大连接数为1000
注意:配置文件方式修改最大连接数需要重启MySQL服务。某些时候不能重启则可以mysql的root用户登录设置

 

mysql> show variables LIKE '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set (0.01 sec)

mysql> set global max_connections=500;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables LIKE '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 500   |
+-----------------+-------+
1 row in set (0.00 sec)

mysql> exit

重启MySQL服务

命令:

[root@bogon ~]# service  mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@bogon ~]#

5.3.3配置数据库连接超时时间

编辑my.cnf文件,

[root@localhost ~]# vi /etc/my.cnf

[mysqld]节点中添加

wait_timeout = 15811200

设置超时时间

该配置标识数据库连接会话超时时间为半年,防止应用中数据库连接池的闲置连接超时。

 

重启MySQL服务

命令:

[root@bogon ~]# service  mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@bogon ~]#

5.4创建数据库以及用户

5.4.1登录MySQL数据库

首先通过root用户登录MySQL数据库

命令:

[root@bogon ~]# mysql -uroot –proot

命令说明:

-u 后面跟MySQL数据库用户名

-p 后面跟MySQL数据库用户密码

 

5.4.2查看MySQL数据库中已有的数据库

命令:

mysql> show databases;

查看默认数据库

5.4.3创建数据库

首先通过创建test数据库来熟悉命令

命令:

create database test default character set utf8mb4 collate utf8mb4_unicode_ci;

注意:Linux系统中MySQL区分大小写

创建数据库

创建成功后可查看

查看数据库

数据库已经成功创建

 

5.4.4创建数据库用户

为上面创建的test库添加一个数据库用户,并赋予这个用户拥有test库的DBA权限

命令:

mysql> grant all privileges on test.*  to testuser@'%' identified by '123456';

mysql> grant all privileges on test.*  to testuser@'localhost' identified by '123456';
grant all privileges on test.*  to test@'%' identified by '123456' WITH GRANT OPTION;

添加 WITH GRANT OPTION;表示该用户可以给其他用户进行grant授权。
注意:某些版本例如5.6.37localhost%没有交集需要单独配localhost否则本地无法登陆

创建MySQL用户

刷新权限

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

退出当前root用户,使用新创建的testuser用户登录数据库

命令:

[root@bogon ~]# mysql -p192.168.8.167 -utestuser -p123456

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 35

Server version: 5.6.37 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> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| test               |

+--------------------+

2 rows in set (0.00 sec)

mysql>

测试用户登录

 

5.4.5删除数据库用户

首先root用户登录数据库

命令:

[root@bogon ~]# mysql -uroot –proot

切换至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>

删除用户

命令:

mysql> delete from user where User='testuser' and Host='localhost';

Query OK, 1 row affected (0.00 sec)

刷新权限:

命令:

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

退出后再次使用testuser登录将会失败:

[root@bogon ~]# mysql -utestuser -p123456
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'testuser'@'localhost' (using password: YES)
[root@bogon ~]#




注意:
1.5.6默认关闭了通过ln链接方式的存储路径修改。在/etc/my.cnf文件中可以查看到以下配置和说明

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

如果要使用ln链接方式修改存放路径注意这里配置值为1,并且关闭selinux

2.新装的mysql 5.6(yum方式) 修改数据存放路径

  1. 需要先启动mysql服务。生成的默认的/var/lib/mysql文件夹
  2. 停止mysql服务
  3. 将生成的mysql文件夹移动到新的你需要移动的地方,并配置/etc/my.cnf文件。将路径指定到新的地方
  4. 启动mysql服务

直接配置/etc/my.cnf文件有较大几率失败
 

地址:https://www.leftso.com/article/224.html

相关阅读

centos6.5安装MySQL 5.6版本,并配置数据了链接数量数据库默认字符集utf8mb4支持Emoji表情4字节内容
MySQL5.6配置双机互为主备
centos7中mysql配置my.cnf字符集utf-8,mysql5.6中文乱码
MySQL5.6数据库双机主从热备配置
yum安装jdk1.8-CentOS6.5,centos6,jdk8
Centos MySql数据库找回root密码
如何将MySQL数据目录更改为CentOS 7上的新位置
centos6 yum安装redis3.29,centos6 源码安装redis以及常用配置
centos6配置开机启动服务
MySQL5.7.20压缩包安装配置流程