CentOS6.8 安装Mysql数据库

概述

 1. 查看系统中是否已经自带Mysql数据库
 2. 使用yum命令进行Mysql的安装
 3. Mysql数据库的初始化及相关配置
 4. 远程连接Mysql及授权

1.查看系统中是否已经自带Mysql数据库

[root@Sunzh ~]# rpm -qa | grep mysql   //查看是否已经安装了Mysql
mysql-libs-5.1.73-8.el6_8.x86_64
[root@Sunzh ~]# rpm -e mysql           //普通删除
error: package mysql is not installed
[root@Sunzh ~]# rpm -e --nodeps mysql  //强制删除
error: package mysql is not installed

2. 使用yum命令进行Mysql的安装

[root@Sunzh ~]# yum list | grep mysql  //查看yum上提供下载的mysql的版本信息
mysql-libs.x86_64                             5.1.73-8.el6_8               @base
apr-util-mysql.x86_64                         1.3.9-3.el6_0.1              base
asterisk-mysql.x86_64                         1.8.32.3-2.el6               epel
bacula-director-mysql.x86_64                  5.0.0-13.el6                 base
bacula-storage-mysql.x86_64                   5.0.0-13.el6                 base
collectd-mysql.x86_64                         4.10.9-5.el6                 epel
dmlite-plugins-mysql.x86_64                   1.10.3-1.el6                 epel
dovecot-mysql.x86_64                          1:2.0.9-22.el6               base
dpm-copy-server-mysql.x86_64                  1.10.0-5.el6                 epel
dpm-name-server-mysql.x86_64                  1.10.0-5.el6                 epel
dpm-server-mysql.x86_64                       1.10.0-5.el6                 epel
dpm-srm-server-mysql.x86_64                   1.10.0-5.el6                 epel
.........
[root@Sunzh ~]# yum install -y mysql-server mysql mysql-deve   //安装数据库
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
No package mysql-deve available.
Resolving Dependencies
.........
[root@Sunzh ~]# rpm -qi mysql-server   //查看数据库版本等信息
Name        : mysql-server                 Relocations: (not relocatable)
Version     : 5.1.73                            Vendor: CentOS
Release     : 8.el6_8                       Build Date: Fri 27 Jan 2017 06:25:43 AM CST
Install Date: Fri 03 Aug 2018 10:23:55 AM CST      Build Host: c1bm.rdu2.centos.org
Group       : Applications/Databases        Source RPM: mysql-5.1.73-8.el6_8.src.rpm
Size        : 25884131                         License: GPLv2 with exceptions
Signature   : RSA/SHA1, Fri 27 Jan 2017 06:35:28 AM CST, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://www.mysql.com
Summary     : The MySQL server and related files
Description :
MySQL is a multi-user, multi-threaded SQL database server. MySQL is a
client/server implementation consisting of a server daemon (mysqld)
and many different client programs and libraries. This package contains
the MySQL server and some accompanying files and directories.

3. Mysql数据库的初始化及相关配置

[root@Sunzh ~]# service mysqld start   //第一次启动mysql服务,首先会进行初始化的配置
Initializing MySQL database:  WARNING: The host 'Sunzh' could not be looked up with resolveip.
This probably means that your libc libraries are not 100 % compatible
with this binary MySQL version. The MySQL daemon, mysqld, should work
normally with the exception that host name resolving will not work.
This means that you should use IP addresses instead of hostnames
when specifying MySQL privileges !
Installing MySQL system tables...
OK
Filling help tables...
OK
.........
[root@Sunzh ~]# service mysqld start   //第二次不会
Starting mysqld:                                           [  OK  ]

设置开机自动启动

[root@Sunzh ~]# chkconfig --list | grep mysqld //未设置前的状态
mysqld          0:off   1:off   2:off   3:off   4:off   5:off   6:off

[root@Sunzh ~]# chkconfig mysqld on            //设置开机启动

[root@Sunzh ~]# chkconfig --list | grep mysqld //设置后的状态
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@Sunzh ~]# mysqladmin -u root password 'root' //第一次设置密码
[root@Sunzh ~]# mysql -u root -p   //登陆mysql服务器,密码是上面设置的root
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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>

4. 远程连接Mysql及授权

mysql> show databases;      //查看所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)
mysql> use mysql;       //使用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> show tables;     //显示所有的表
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
23 rows in set (0.00 sec)
mysql> select Host,User,Password from user\G;   //查看账户
*************************** 1. row ***************************
    Host: localhost
    User: root
Password: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
*************************** 2. row ***************************
    Host: sunzh
    User: root
Password:
*************************** 3. row ***************************
    Host: 127.0.0.1
    User: root
Password:
*************************** 4. row ***************************
    Host: localhost
    User:
Password:
*************************** 5. row ***************************
    Host: sunzh
    User:
Password:
5 rows in set (0.00 sec)
mysql> grant all privileges on *.* to root@'%' identified by "root";    //授权
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;    //刷新
Query OK, 0 rows affected (0.00 sec)
mysql> select Host,User,Password from user\G;   //再次查看账号,多了第六条
*************************** 1. row ***************************
    Host: localhost
    User: root
Password: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
*************************** 2. row ***************************
    Host: sunzh
    User: root
Password:
*************************** 3. row ***************************
    Host: 127.0.0.1
    User: root
Password:
*************************** 4. row ***************************
    Host: localhost
    User:
Password:
*************************** 5. row ***************************
    Host: sunzh
    User:
Password:
*************************** 6. row ***************************//%所有IP都可以访问
    Host: %
    User: root
Password: *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
6 rows in set (0.00 sec)

Navicat测试连接
这里写图片描述


注意:
服务需要开放3306端口,阿里云和腾讯云是在控制台添加防火墙规则的
这里写图片描述

猜你喜欢

转载自blog.csdn.net/Justszh/article/details/81382326
今日推荐