Centos 6中安裝二進制數據庫最新版本mariadb-5.5.57

一:實驗準備;

1、rpm -qa mariadb*查看是否已經安裝有mariadb,爲了避免重複,若有的話就先將其刪除

2、yum info mariadb在Centos 7裏執行,查找官網的,其實也可以直接在網上找,然後進去官網後找到最新版本並下載mariadb-5.5.57-linux-x86_64.tar.gz 

3、將下載好的包"rz"拷進linux虛擬機中(我直接拷到了家目錄下)

二:實驗步驟;

1、創建mysql用戶;

[root@Centos6 ~]# getent passwd mysql   #先查看一下該用戶是否存在,若存在就不用動,若不存在就創建
[root@Centos6 ~]# useradd -d /var/lib/mysql -r -m -s /sbin/nologin mysql   #Centos 7裏我將數據庫放在了/app/mysqldb下了,這裏就放在/var/log/mysql下吧

2、解壓縮;

[root@Centos6 ~]# tar xvf mariadb-5.5.57-linux-x86_64.tar.gz -C /usr/local/  #因爲是已經編譯好的二進制文件,目錄是已經被制定好的,必須放在/usr/local,故加上-C解壓縮到指定目錄
[root@Centos6 ~]# cd /usr/local/                                             #進入到該目錄下
[root@Centos6 /usr/local]# ls
bin  games    lib    libexec                      share
etc  include  lib64  mariadb-5.5.57-linux-x86_64  sbin   src
[root@Centos6 /usr/local]# ln -s mariadb-5.5.57-linux-x86_64/ mysql          #因爲名字必須是mysql,所以將其創建軟鏈接
[root@Centos6 /usr/local]# cd mysql/                                         #然後進到mysql目錄下

3、創建my.cnf配置文件

[root@Centos6 /usr/local/mysql]# mkdir /etc/mysql/                          #編譯時默認配置文件放在/etc/mysql下,所以創建一個mysql的目錄
[root@Centos6 /usr/local/mysql]# ls support-files/                           #二進制文件裏已經定義好的配置文件放在/usr/local/mysql/support-files/下
binary-configure        my-large.cnf         mysql-log-rotate
magic                   my-medium.cnf        mysql.server
my-huge.cnf             my-small.cnf         SELinux
my-innodb-heavy-4G.cnf  mysqld_multi.server
[root@Centos6 /usr/local/mysql]# cp support-files/my-huge.cnf  /etc/mysql/my.cnf  #將二進制文件裏的配置文件(裏面有好幾個,可以選擇其中一個)複製到/etc/mysql/下,並改名爲my.cnf(名字規定必須是my.cnf)
[root@Centos6 /usr/local/mysql]# vim /etc/mysql/my.cnf                          #編輯配置文件
[mysqld]            #在[mysqld]裏面添加如下三行代碼,第一行是必須要加的,後兩行是爲了性能優化方便管理加的,可有可無
datadir           = /var/lib/mysql/          #指明數據庫存放路徑
innodb_file_per_table = on                   #使用的innodb數據庫引擎,並且使每個表單獨在一個文件裏,方便管理
skip_name_resolve  = on                      #爲了加速訪問速度,忽略地址的方向解析

4、創建系統數據庫;

  #數據庫被放置在/usr/local/mysql/scripts/mysql_install_db裏,所以要跑腳本,使其生成數據庫
  #而且這個腳本一定不能進去scripts裏跑,會出錯,在它的上一層跑
[root@Centos6 mysql]# scripts/mysql_install_db --user=mysql --datadir=/var/lib/mysql
[root@Centos6 mysql]# ls /var/lib/mysql/                 #驗證一下是否生成
aria_log.00000001  mysql-bin.000001  mysql-bin.000004    test
aria_log_control   mysql-bin.000002  mysql-bin.index
mysql              mysql-bin.000003  performance_schema
[root@Centos6 mysql]#

5、啓動服務;

[root@Centos6 mysql]# cp support-files/mysql.server /etc/init.d/mysqld    #將服務腳本放在特定的目錄下
[root@Centos6 mysql]# chkconfig --list mysqld                 #檢查mysqld服務是否存在,如下所示,存在但啓動(若不存在,就 chkconfig --add mysqld加上)
mysqld          0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@Centos6 mysql]#service mysqld start                                        #啓動服務,因爲/var/log/mysqld.log已經存在所以直接成功
Starting MySQL.171014 17:07:41 mysqld_safe Logging to '/var/log/mysqld.log'.
171014 17:07:41 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
..                                                         [  OK  ]
[root@Centos6 mysql]#      #有些機器第一次啓動失敗,缺少日誌文件(啓動成功的下面的代碼不用做)
[root@Centos6 mysql]# mkdir /var/log/mariadb/             #那麼就要單獨創建一下
[root@Centos6 mysql]# chown mysql /var/log/mariadb/       #爲了給mysql對目錄/var/log/mariadb/ 有寫權限,所以將其目錄的所有者改成mysql
[root@Centos6 mysql]# service mysqld start          #再重啓應該就沒問題了

6、編輯PATH變量;

[root@Centos6 mysql]#  vim /etc/profile.d/mysql.sh           #單獨放在一個配置文件裏
PATH=/usr/local/mysql/bin:$PATH

[root@Centos6 mysql]# . /etc/profile.d/mysql.sh               #使其生效

7、初始化、跑安全腳本,設置口令密碼;

[root@Centos6 mysql]# mysql_secure_installation               #安全腳本
/usr/local/mysql/bin/mysql_secure_installation: line 393: 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!
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):         #讓輸入密碼,因爲之前沒有設,所以直接回車
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y                                #是否設密碼,跑安全腳本就是爲了安全,所以“y”要設
New password:                                            #輸入密碼
Re-enter new password:                                   #再次輸入密碼
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y                        #是否刪除匿名用戶,刪除,這樣就不能匿名登錄了
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n                 #是否禁止遠程登錄,不用禁止
 ... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] n         #是否刪除test數據庫,刪不刪都行
 ... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y                   #使其立即生效
 ... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@Centos6 mysql]#

三:實驗測試;

[root@Centos6 mysql]# mysql -uroot -pmagedu            #-u接用戶名,-p接密碼(爲了安全建議直接-p後回車再輸入密碼)
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.57-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select user();                       #查看當前用戶名(可參考Centos 7中http://13172997.blog.51cto.com/13162997/1972311)
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.01 sec)
MariaDB [(none)]> show databases;                   #顯示數據庫信息
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.09 sec)
MariaDB [(none)]> use mysql                    #設mysql爲當前數據庫
Database changed
MariaDB [mysql]> show tables;                 #顯示當前數據庫中的表信息
+---------------------------+
| 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 [mysql]> desc user
    -> ;                             #不是mysql自身的命令必須加分號結束,此命令顯示user表結構
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field                  | Type                              | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host                   | char(60)                          | NO   | PRI |         |       |
| User                   | char(16)                          | NO   | PRI |         |       |
| Password               | char(41)                          | NO   |     |         |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N  
+------------------------+-----------------------------------+------+-----+---------+-------+
42 rows in set (0.05 sec)

MariaDB [mysql]> quit
Bye
[root@Centos6 mysql]#

四:實驗總結;

其實Centos 6中的安裝和Centos 7 算是一樣的步驟,只不過用的版本不一樣而已;

Centos 7裏說的稍微細一些,可以參考我的上篇文章http://13172997.blog.51cto.com/13162997/1972311




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章