centos7 mysql数据库安装和配置

时间:2021-07-16 02:21:50

 

一、系统环境

 

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

[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

 

一、系统环境

 

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

[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

 

 二、mysql安装

 

 

一般网上给出的资料都是

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

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

[[email protected] ~]# yum install mysql-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
No package mysql-server available.
Error: Nothing to do

 

 

  

 

 

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

 

有两种解决办法:

1、方法一:安装mariadb

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

安装mariadb,大小59 M。

[[email protected] ~]# yum install mariadb-server mariadb 

 

mariadb数据库的相关命令是:

启动MariaDB:
[[email protected] ~]# systemctl start mariadb
停止MariaDB:
[[email protected] ~]# systemctl stop mariadb
重启MariaDB:
[[email protected] ~]# systemctl restart mariadb
设置开机启动:
[[email protected] ~]# systemctl enable mariadb
取消开机启动:
[[email protected] ~]# systemctl disable mariadb

 

 

 

 

 

 

 

 

 

 

 

 

 

所以先启动数据库

[[email protected] ~]# systemctl start mariadb

然后就可以正常使用mysql了  

[[email protected] ~]# systemctl start mariadb

[[email protected] ~]# mysql -u root -p
Enter password:    (首次登录密码为空
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, 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)]> set password for ‘root‘@‘localhost‘ =password(‘redhat‘);
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>

 

 

 

 

 

安装mariadb后显示的也是 MariaDB [(none)]> ,可能看起来有点不习惯。下面是第二种方法。

 

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

[[email protected] ~]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

[[email protected] ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-release-el7-5 ################################# [100%]

 

[[email protected] ~]# ll /etc/yum.repos.d/
total 48
-rw-r--r-- 1 root root 1664 Sep 5 09:05 CentOS-Base.repo
-rw-r--r-- 1 root root 1309 Sep 5 09:05 CentOS-CR.repo
-rw-r--r-- 1 root root 649 Sep 5 09:05 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root 314 Sep 5 09:05 CentOS-fasttrack.repo
-rw-r--r-- 1 root root 630 Sep 5 09:05 CentOS-Media.repo
-rw-r--r-- 1 root root 1331 Sep 5 09:05 CentOS-Sources.repo
-rw-r--r-- 1 root root 6639 Sep 5 09:05 CentOS-Vault.repo
-rw-r--r-- 1 root root 951 Oct 2 2017 epel.repo
-rw-r--r-- 1 root root 1050 Oct 2 2017 epel-testing.repo
-rw-r--r-- 1 root root 1209 Jan 29 2014 mysql-community.repo
-rw-r--r-- 1 root root 1060 Jan 29 2014 mysql-community-source.repo

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 [[email protected] ~]# yum install mysql-community-server

 安装成功后重启mysql服务。

 [[email protected] ~]# service mysqld restart

 

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

[[email protected] ~]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.45 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 |
--------------------------
3 rows in set (0.00 sec)

mysql>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

设置密码

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

mysql>

 

 

 

 

 

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

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

centos7 mysql数据库安装和配置

 

 

 

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

[[email protected] ~]# rpm -qa |grep mariadb
[[email protected] ~]#

 

 

 

 

三、配置mysql

 

1、编码

mysql配置文件为/etc/my.cnf

最后加上编码配置

[[email protected] ~]# vim /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

[mysql]
default-character-set =utf8

!includedir /etc/my.cnf.d

 

 

 

 

 

 

 

 

 

 

 

 

 

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

[[email protected] ~]# vim /usr/share/mysql/charsets/Index.xml

centos7 mysql数据库安装和配置

 

 

 

2、远程连接设置

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

[[email protected] ~]# mysql -uroot -p
Enter password:     (密码是:redhat)
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 6
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

MariaDB [(none)]> grant all privileges on *.* to [email protected]‘%‘identified by ‘redhat‘;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]>

 

 

 

 

 

 

 

 

 

 

 

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

MariaDB [(none)]> create user ‘admin‘@‘%‘ identified by ‘Xuanbao123456‘;
Query OK, 0 rows affected (0.00 sec)

 

 

 

 

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

[[email protected] ~]# mysql -uadmin -p -h192.168.122.20
Enter password:     (密码是:Xuanbao123456)
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 7
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

MariaDB [(none)]>

评论( 0) © 2014 mamicode.com 版权所有 京ICP备13008772号-2 迷上了代码!