獲取二進制格式MariaDB安裝包,可去官網下載.html
由於是實驗環境,因此選擇了最新版.mysql
mariadb-10.2.12-linux-x86_64.tar.gzlinux
解壓到 /usr/local,並給解壓出的文件夾建立軟鏈接sql
]# tar xf mariadb-10.2.12-linux-x86_64.tar.gz -C /usr/local/ ]# cd /usr/local/ ]# ln -sv mariadb-10.2.12-linux-x86_64/ mysql ]# ll mysql lrwxrwxrwx. 1 root root 29 Jan 23 10:33 mysql -> mariadb-10.2.12-linux-x86_64/
爲mariaDB建立一個系統用戶,屬主爲root,數組爲mysql數據庫
]# groupadd -r mysql ]# cat /etc/group | grep mysql mysql:x:985: ]# cd mysql
ll查看發現全部文件的屬主屬組都只顯示id號vim
針對軟鏈接,要用下面的方式修改其中的文件屬性centos
]# chown -R root:mysql ./* ]# ll total 180 drwxrwxr-x. 2 root mysql 4096 Nov 14 22:34 bin ......
爲了管理方便,專門指定並建立幾個專用目錄數組
存放數據庫的目錄/mdata/data安全
存放配置文件的目錄/etc/mysqlbash
]# mkdir -pv /mdata/data
修改這個目錄的屬主屬組爲mysql
]# useradd -r mysql -g mysql -s /bin/nologin ]# chown -R mysql.mysql /mdata ]# mkdir /etc/mysql
mariaDB的主配置文件是/etc/my.cnf.其中include包含了/etc/my.cnf.d這個目錄
]# cat /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d
support-files/目錄下有5個針對不一樣系統內存設置的配置文件模板
]# ls /usr/local/mysql/support-files/my-* my-huge.cnf my-innodb-heavy-4G.cnf my-large.cnf my-medium.cnf my-small.cnf
其中
my-small.cnf適用於內存只有64M;
my-medium.cnf適用於內存只有256M;
my-large.cnf適用於內存只有512M;
my-huge.cnf適用於更大內存,如2G、4G;
my-innodb-heavy-4G.cnf適用於4G內存;
實驗用主機有2g內存,因此使用my-huge.cnf
把my-huge.cnf複製到自定義配置文件目錄下並重命名爲my.cnf,並在[mysqld]代碼塊中添加內容
]# cp support-files/my-huge.cnf /etc/mysql/my.cnf ]# vim /etc/mysql/my.cnf datadir = /mdata/data #指定數據庫目錄 skip_name_resolve = ON #忽略反解主機名 innodb_file_per_table = ON #開啓獨立表空間,默認是全部數據庫放在同一個文件中,ON的話會把每一個數據庫單獨放一個文件
必須在/usr/local/mysql目錄下操做,不然會報錯
]# cd /usr/local/mysql ]# ./scripts/mysql_install_db --datadir=/mdata/data --user=mysql
]# touch /var/log/mysqld.log ]# chown mysql:mysql /var/log/mysqld.log
先經過support-files/mysql.server 啓動mysql
./support-files/mysql.server start
成功後,經過ps命令查看
]# ps -ef|grep mysql
找到--pid-file=/mdata/data/centos7.qt.pid字樣 記住pid路徑和名稱/mdata/data/centos7.qt.pid
在/usr/lib/systemd/system目錄下增新建mysql.service,內容以下
]# vim /usr/lib/systemd/system/mysql.service [Unit] Description=Mysql After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/mdata/data/centos7.qt.pid ExecStart=/usr/local/mysql/support-files/mysql.server start ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=false [Install] WantedBy=multi-user.target PIDFile=/mdata/data/centos7.qt.pid #就是跟上上面記錄的內容
執行從新掃描,加載有變化的單元,使服務生效,以後即可以經過systemctl操做
]# systemctl daemon-reload ]# systemctl start mysql
運行腳本
]# /usr/local/mysql/bin/mysql_secure_installation
]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh ]# . /etc/profile.d/mysql.sh
]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 19 Server version: 10.2.12-MariaDB-log 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)]>