首頁  >  文章  >  資料庫  >  centos7 mysql資料庫安裝和配置的方法

centos7 mysql資料庫安裝和配置的方法

WBOY
WBOY轉載
2023-05-28 21:55:041468瀏覽

一、系統環境

yum update升級以後的系統版本為

[root@yl-web yl]# 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@yl-web yl]# 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取代了。

有兩種解決方法:

1、方法一:安裝mariadb

mariadb資料庫管理系統是mysql的一個分支,主要由開源社區在維護,採用gpl授權許可。開發這個分支的原因之一是:甲骨文公司收購了mysql後,有將mysql閉源的潛在風險,因此社區採用分支的方式來避免這個風險。 mariadb的目的是完全相容於mysql,包括api和命令列,使其能輕鬆成為mysql的替代品。

安裝mariadb,大小59 m。

[root@yl-web yl]# yum install mariadb-server mariadb

mariadb資料庫的相關指令是:

systemctl start mariadb #启动mariadb

systemctl stop mariadb #停止mariadb

systemctl restart mariadb #重启mariadb

systemctl enable mariadb #设置开机启动

所以先啟動資料庫

[root@yl-web yl]# systemctl start mariadb

然後就可以正常使用mysql了 

[root@yl-web yl]# 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)]> ,可能看起來有點不習慣。下面是第二種方法。

2、方法二:官網下載安裝mysql-server

# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
# rpm -ivh mysql-community-release-el7-5.noarch.rpm
# yum install mysql-community-server

安裝成功後重新啟動mysql服務。

# service mysqld restart

初次安裝mysql,root帳號沒有密碼。 

[root@yl-web yl]# 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>

不需要重新啟動資料庫即可生效。

在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

所以安裝完以後mariadb自動就被替換了,不再生效。

[root@yl-web yl]# rpm -qa |grep mariadb
[root@yl-web yl]#

三、設定mysql

1、編碼

mysql設定檔為/etc/my.cnf

最後加上編碼配置

[mysql]
default-character-set =utf8

這裡的字元編碼必須和/usr/share/mysql/charsets/index.xml中一致。

centos7 mysql数据库安装和配置的方法

2、遠端連線設定

把在所有資料庫的所有資料表的所有權限賦值給位於所有ip位址的root使用者。

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

如果是新使用者而不是root,則要先新建使用者

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

此時就可以進行遠端連線了。

以上是centos7 mysql資料庫安裝和配置的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除