当前位置:Gxlcms > 数据库问题 > centos7 mysql数据库安装和配置

centos7 mysql数据库安装和配置

时间:2021-07-01 10:21:17 帮助过:21人阅读


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

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

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

 

[root@mysql ~]# 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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 [root@mysql ~]# yum install mysql-community-server

 安装成功后重启mysql服务。

 [root@mysql ~]# service mysqld restart

 

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

[root@mysql ~]# 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安装过程中如下内容:

技术图片

 

 

 

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

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

 

 

 

 

三、配置mysql

 

1、编码

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

最后加上编码配置

[root@mysql ~]# 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中一致。

[root@mysql ~]# vim /usr/share/mysql/charsets/Index.xml

技术图片

 

 

 

2、远程连接设置

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

[root@mysql ~]# 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 root@‘%‘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)

 

 

 

 

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

[root@c1 ~]# 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)]>

centos7 mysql数据库安装和配置

标签:令行   启动   redhat   reserve   data   log-error   ide   red   char   

人气教程排行