1. 程式人生 > >學習筆記之mariadb的入門操作

學習筆記之mariadb的入門操作

由於CentOS 7 將mysql全部都改成了mariadb。所以在CentOS 下yum安裝mysql是沒有用的。雖然還是有一大堆軟體包叫做mysql。

mysql-community-release.noarch         el7-5                       installed    
php-mysql.x86_64                       5.4.16-23.el7_0.3           @RHEL        
akonadi-mysql.x86_64                   1.9.2-4.el7                 RHEL         
dovecot-mysql.x86_64                   1:2.2.10-4.el7_0.1          RHEL         
libdbi-dbd-mysql.x86_64                0.8.3-16.el7                RHEL         
mysql-connector-java.noarch            1:5.1.25-3.el7              RHEL         
mysql-connector-odbc.x86_64            5.2.5-6.el7                 RHEL         
qt-mysql.i686                          1:4.8.5-8.el7               RHEL         
qt-mysql.x86_64                        1:4.8.5-8.el7               RHEL   

不過不用擔心。mariadb和mysql幾乎是一樣的。首先,mariadb就是由mysql的創始人負責維護的。而mariadb就是mysql創始人女兒的名字。

首先第一步,安裝mariadb!!!

1.安裝mariadb

[[email protected] ftp]# yum -y install mariadb mariadb-server
已載入外掛:product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

2.改密碼

下面的操作之前一定要記得先啟動mariadb

systemctl  start  mariadb

注意,這裡的root和linux的root沒半毛錢關係.maria的初始密碼預設是空的。需要你改一下的。不改也能登陸,就是不安全而已,你的庫不要密碼就能登陸,很可怕

   改密碼

[[email protected] ~]# mysqladmin -uroot -p password zhelitianmima
Enter password: 
[[email protected] ~]# 
後面那個zhelitianmima是你要改的密碼。Enter password那裡直接回車就好了


   登陸mariadb

補充一點。mysql的密碼都在mysql這個庫裡面,有一張表叫user,這裡管理了可以登陸資料庫的使用者


MariaDB [(none)]> SHOW TABLES FROM mysql;
+---------------------------+
| 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                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

MariaDB [(none)]> SELECT User,Host,Password FROM mysql.user;
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *3C0631D77CED7755AD5677173D2155BE7D1E514F |
| root | ns.server |                                           |
| root | 127.0.0.1 |                                           |
| root | ::1       |                                           |
|      | localhost |                                           |
|      | ns.server |                                           |
| suse | %         | *45AC16EC077811822DE5C6C4DDB499641E9C817E |
+------+-----------+-------------------------------------------+
7 rows in set (0.00 sec)


再教一個改密碼的方式吧(就是mysql_secure_installation這個命令)

[[email protected] ~]# mysql_secure_installation 
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


介紹幾個基本的命令

show databases;							顯示資料庫
use mysql;							進入資料庫
show tables;							顯示資料庫中的表
desc user;							檢視user表的資料結構
flush privileges;						重新整理資料庫資訊
select host.user,password from user;				查詢user表中的host,user,password欄位

show databases;                  顯示資料庫
use mysql;                            進入資料庫
show tables;                         顯示示資料庫中的表
desc user;                            檢視user表的資料結構
flush privileges;                    重新整理資料庫資訊
select host.user,password from user;     查詢user表中的host,user,password欄位

create database westos;                        建立westos資料庫
use westos;                            
create table linux(                                   建立表,username,password欄位
username varchar(15) not null,
password varchar(15) not null
);
select * from mysql.user;                           查詢mysql庫下的user表中的所以
alter table linux add age varchar(4);          新增age欄位到linux表中
ALTER TABLE linux DROP age                刪除age欄位
ALTER TABLE linux ADD age  VARCHAR(5)  AFTER name        在name欄位後新增欄位age


有個問題。要是忘了資料庫的超級使用者密碼怎麼辦呢

[[email protected] ~]# systemctl stop mariadb
[[email protected] ~]# mysqld_safe --skip-grant-tables &>/dev/null &
[3] 2660
[[email protected] ~]# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
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)]> 



看吧不用輸入密碼就直接可以進入mysql了(就不叫mariadb了。叫mysql順口了)
接下來就改mysql那個資料庫裡的user下的root的密碼吧


就是這個sql語句(我將密碼改為了123,放心是加密的,不信看看)

MariaDB [(none)]> SELECT User,Host,Password FROM mysql.user;
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ns.server | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | 127.0.0.1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root | ::1       | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
|      | localhost |                                           |
|      | ns.server |                                           |
| suse | %         | *45AC16EC077811822DE5C6C4DDB499641E9C817E |
+------+-----------+-------------------------------------------+
7 rows in set (0.00 sec)

為了安全,看看埠上有沒有mysql的埠

[[email protected] etc]# nmap localhost

Starting Nmap 6.40 ( http://nmap.org ) at 2016-11-27 22:24 CST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000026s latency).
Other addresses for localhost (not scanned): 127.0.0.1
Not shown: 995 closed ports
PORT     STATE SERVICE
21/tcp   open  ftp
22/tcp   open  ssh
25/tcp   open  smtp
80/tcp   open  http
3306/tcp open  mysql

Nmap done: 1 IP address (1 host up) scanned in 0.12 seconds

改了之後退出來殺掉所有跟mysql相關的程序。咋殺我就不說了阿



那我要是不想別人遠端登陸我的資料庫怎麼辦呢

修改/etc/my.cnf  在第11行加上skip-networking=1,再重啟mysql。

systemctl restart mariadb

  1 [mysqld]
  2 datadir=/var/lib/mysql
  3 socket=/var/lib/mysql/mysql.sock
  4 # Disabling symbolic-links is recommended to prevent assorted security risks
  5 symbolic-links=0
  6 # Settings user and group are ignored when systemd is used.
  7 # If you need to run mysqld under a different user or group,
  8 # customize your systemd unit file for mariadb according to the
  9 # instructions in http://fedoraproject.org/wiki/Systemd
 10
 11 skip-networking=1
 12 [mysqld_safe]
 13 log-error=/var/log/mariadb/mariadb.log
 14 pid-file=/var/run/mariadb/mariadb.pid
 15 #
 16 # include all files from the config directory
 17 #
 18 !includedir /etc/my.cnf.d
 19 

關於資料庫的sql語句就不在這裡細說了。直接說如何直接以圖形的方式管理資料庫吧

1.現在網上下一個phpMyAdmin-3.4.0-all-languages.tar.bz2這個軟體包

2.再在yum安裝php php-mysql http 這3款軟體

3.在/var/www/html下解壓縮phpMyAdmin

4.在phpMyAdmin-3.4.0-all-languages這個目錄中的config.sample.inc.php下找個

$cfg['blowfish_secret'] = ‘隨便填個值’ 並將config.sample.inc.php    改名為config.inc.php


關閉防火牆。



得到上面的圖形

以圖形方式管理資料庫