CentOS7 MySQL数据库安装和配置

一、系统环境

yum update 升级以后的系统版本为:

[root@localhost tly]# cat /etc/redhat-release 
CentOS Linux release 7.1.1503 (Core) 

二、mysql安装

一般网上给出的资料都是

yum install mysql
yum install mysql-server
yum install mysql-devel

安装mysql和mysql-devel都成功,但是安装mysql-server失败,如下:

[root@localhost tly]# yum install mysql-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.sina.cn
 * extras: mirrors.sina.cn
 * updates: mirrors.sina.cn
No package mysql-server available.
Error: Nothing to do

查资料发现是CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了。

三、解决办法

方法一:安装mariadb

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

安装mariadb,大小59 M。

[root@localhost tly]# yum install mariadb-server mariadb 

mariadb数据库的相关命令是:

systemctl start mariadb  #启动MariaDB
systemctl stop mariadb  #停止MariaDB
systemctl restart mariadb  #重启MariaDB
systemctl enable mariadb  #设置开机启动

所以先启动数据库

[root@localhost tly]# systemctl start mariadb

然后就可以正常使用mysql了

[root@localhost tly]# mysql -u root -p
Enter password: 

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.41-MariaDB MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> 

安装mariadb成功后显示的是 MariaDB [(none)] > 。

方法二:官网下载安装 mysql-server

查看mysql版本:

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

无法使用 wget 命令:yum -y install wget # 安装 wget 

选择合适的版本,把文件下载到本地:yum localinstall mysql-community-release-el7-5.noarch.rpm

安装 MySQL 服务:

rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server

在mysql安装过程中如下内容:

Installed:
  mysql-community-client.x86_64 0:5.6.26-2.el7   mysql-community-devel.x86_64 0:5.6.26-2.el7          
  mysql-community-libs.x86_64 0:5.6.26-2.el7     mysql-community-server.x86_64 0:5.6.26-2.el7          

Dependency Installed:
  mysql-community-common.x86_64 0:5.6.26-2.el7 

Replaced:
  mariadb.x86_64 1:5.5.41-2.el7_0  mariadb-devel.x86_64 1:5.5.41-2.el7_0  mariadb-libs.x86_64 1:5.5.41-2.el7_0
  mariadb-server.x86_64 1:5.5.41-2.el7_0  

安装完成后启动MySQL:service mysqld start

查看密码:cat /var/log/mysqld.log | grep “password”

MySQL 配置文件:/var/log/mysqld.log

初次安装mysql,root账户没有密码。

[root@localhost tly]# mysql -u root 

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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 |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> 

设置密码:

mysql> set password for 'root'@'localhost' =password('password');
Query OK, 0 rows affected (0.00 sec)

不需要重启数据库即可生效。

MySQL 开启 general_log 跟踪数据执行过程:

# 设置general log保存路径
# 注意在Linux中只能设置到 /tmp 或 /var 文件夹下,设置其他路径出错
# 需要root用户才有访问此文件的权限
mysql> set global general_log_file='/tmp/general.log'; 

# 开启general log模式:mysql> set global general_log=on; 
# 关闭general log模式:mysql> set global general_log=off; 

在general log模式开启过程中,所有对数据库的操作都将被记录 general.log 文件

新建用户:CREATE USER 'imooc'@'%' IDENTIFIED BY '123456';

赋予权限:

GRANT ALL PRIVILEGES ON *.* TO 'imooc'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

GRANT select,insert,update,delete ON *.* TO 'imooc'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

FLUSH PRIVILEGES;

收回权限:

REVOKE ALL PRIVILEGES ON *.* FROM imooc;

FLUSH PRIVILEGES;

忘记 root 密码:在 /etc/my.cnf 加入 skip-grant-tables

use mysql;

update user set authentication_string=password('456789') where user='root';

重启mysql服务:

# service mysqld restart

所以安装完以后mariadb自动就被替换了,将不再生效。 

[root@localhost tly]# rpm -qa |grep mariadb
[root@localhost tly]# 

三、配置mysql

1、编码

mysql配置文件为 “/etc/my.cnf”,最后加上编码配置:

[mysql]
default-character-set =utf8

这里的字符编码必须和 /usr/share/mysql/charsets/Index.xml 中一致。

2、远程连接设置

把在所有数据库的所有表的所有权限赋值给位于所有IP地址的root用户。

设置root可以远程连接:
update  mysql.`user` set Host = '%' where User = 'root' and Host = 'localhost';
flush privileges; 或者重启服务 sudo service mysqld restart

mysql> grant all privileges on *.* to root@'%'identified by 'password';

如果是新用户而不是root,则要先新建用户:

mysql>create user 'username'@'%' identified by 'password';  

此时就可以进行远程连接了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值