mysql升級到mariadb[同一臺機器上]

操做系統:centos6.5mysql

即將安裝的數據庫軟件:mysql 5.1和 mariadb 10.3.8c++

注意事項:mysql的端口不能和mariadb端口同樣,不然沒法啓動sql

我這裏mysql採用默認端口:3306數據庫

mariadb的端口我會指定爲:3307centos

 

1,配置yum源便於用yum進行安裝軟件瀏覽器

下面是個人/etc/yum.repos.d/Centos-Base.repo倉庫文件的內容bash

[root@rabbit ~]# cat /etc/yum.repos.d/CentOS6-Base-163.repo 
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-6 - Base - 163.com
baseurl=http://mirrors.163.com/centos/6/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#released updates 
[updates]
name=CentOS-6 - Updates - 163.com
baseurl=http://mirrors.163.com/centos/6/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-6 - Extras - 163.com
baseurl=http://mirrors.163.com/centos/6/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-6 - Plus - 163.com
baseurl=http://mirrors.163.com/centos/6/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users
[contrib]
name=CentOS-6 - Contrib - 163.com
baseurl=http://mirrors.163.com/centos/6/contrib/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=contrib
gpgcheck=1
enabled=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

 2,安裝mysqlless

yum安裝的過程已經省略,只給出安裝指令socket

[root@rabbit ~]# yum -y install mysql-server mysql

  

 3,檢查是否能夠啓動mysql並鏈接ui

下圖能夠看見直接敲mysql指令就進入了數據庫,這時由於剛安裝好以後尚未設置密碼

[root@rabbit ~]# ls /etc/init.d/mysql*
/etc/init.d/mysqld
[root@rabbit ~]# service mysqld start
正在啓動 mysqld:                                          [肯定]
[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> quit

  

 4,下載並編譯安裝mariadb10.3.8

官網下載地址:

https://downloads.mariadb.org/interstitial/mariadb-10.3.8/source/mariadb-10.3.8.tar.gz/from/http%3A//mirrors.tuna.tsinghua.edu.cn/mariadb/

在瀏覽器裏打開後可能須要你填信息,信息隨便填假信息也能夠,但得符合格式,人家讓你填電話你就別傻到填字母就行

下載後進行解壓並安裝

[root@rabbit ~]# ls /home/ting/下載/mariadb-*
/home/ting/下載/mariadb-10.3.8.tar.gz
[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# cd /root/
[root@rabbit ~]# 
[root@rabbit ~]# tar -xf mariadb-10.3.8.tar.gz
[root@rabbit ~]# 
[root@rabbit ~]# 

  

 編譯安裝以前須要安裝編譯環境

[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# yum -y install gcc gcc-c++ cmake bison bison-devel ncurses-devel openssl-devel openssl 

  

 開始編譯安裝

[root@rabbit mariadb-10.3.8]# 
[root@rabbit mariadb-10.3.8]# 
[root@rabbit mariadb-10.3.8]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb -DMYSQL_DATADIR=/var/lib/mariadb -DSYSCONFDIR=/etc/mariadb -DWITHOUT_TOKUDB=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_HEADLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/var/lib/mariadb/mariadb.sock -DMYSQL_TCP_PORT=3307 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
[root@rabbit mariadb-10.3.8]# 
[root@rabbit mariadb-10.3.8]# make 
[root@rabbit mariadb-10.3.8]# 
[root@rabbit mariadb-10.3.8]# make install

 

 5,編寫mariadb的啓動管理腳本

目的是便於啓動或中止mariadb

編寫以前先創建相關目錄和受權

[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# mkdir /var/lib/mariadb 
[root@rabbit ~]# 
[root@rabbit ~]# mkdir /var/run/mariadb
[root@rabbit ~]# 
[root@rabbit ~]# mkdir /var/log/mariadb
[root@rabbit ~]# 
[root@rabbit ~]# chown mysql.mysql  /var/lib/mariadb 
[root@rabbit ~]# 
[root@rabbit ~]# chown mysql.mysql /var/run/mariadb
[root@rabbit ~]# 
[root@rabbit ~]# chown mysql.mysql /var/log/mariadb

  

 開始編寫mariadb啓動管理腳本,腳本內容以下:

我這而腳本名稱是:mariadb

cat mariadb的內容以下:

#!/bin/bash
#
# chkconfig: 2345 10 90
# description: mariadb service manager
# Usage: service mariadb <status | start | stop | restart>

port=3307
basedir=/usr/local/mariadb
datadir=/var/lib/mariadb
u=mysql
log_err=/var/log/mariadb/mariadb_err.log
pid_file=/var/run/mariadb/mariadb.pid
sock=/var/lib/mariadb/mariadb.sock

function check(){
mysql_proc_num=`ps -ef|grep 'mysqld '|grep mariadb|grep -v grep|wc -l`
mysql_proc_pid=`ps -ef|grep 'mysqld '|grep mariadb|grep -v grep|awk '{print $2}'`
if [ -f $pid_file ]
then
pid_file_pid=`cat $pid_file`
else
echo "MariaDB stopped ..."
return
fi

if [ $mysql_proc_pid -eq $pid_file_pid ] && [ $mysql_proc_num -ne 0 ]
then
echo "MariaDB is running ..."
else
echo "MariaDB stopped ..."
fi
}

function start(){
cd $basedir
./bin/mysqld_safe --datadir=$datadir --user=u --log-error=$log_err --pid-file=$pid_file --socket=$sock --port=$port &
sleep 3;
check;
}

function stop(){
kill  `cat $pid_file`
sleep 2;
check;
}

function restart(){
kill -HUP `cat $pid_file`
sleep 2
echo -e "重啓完畢:\c "
check;
}

function status(){
check;
}

case $1 in
status)
status;;
stop)
stop;;
start)
start;;
restart)
restart;;
*)
echo "用法1:/etc/init.d/mariadb <status | stop | start | restart>"
echo "用法2:service mariadb <status | stop | start | restart>"
;;
esac

6,將腳本移動到/etc/init.d/目錄下去

[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# cp mariadb /etc/init.d/
[root@rabbit ~]# 
[root@rabbit ~]# chmod +x /etc/init.d/mariadb 
[root@rabbit ~]# 
[root@rabbit ~]# 

7,檢查mariadb服務是否能夠正常運行並鏈接

[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# ls -l /etc/init.d/mar*
-rwxr-xr-x. 1 root root 1303 8月   7 23:39 /etc/init.d/mariadb
[root@rabbit ~]# 
[root@rabbit ~]# service mariadb start
180807 23:42:42 mysqld_safe Logging to '/var/log/mariadb/mariadb_err.log'.
180807 23:42:42 mysqld_safe Starting mysqld daemon with databases from /var/lib/mariadb
MariaDB is running ...
[root@rabbit ~]# 
[root@rabbit ~]# ps -ef|grep mariadb
root      7261     1  0 23:42 pts/2    00:00:00 /bin/sh ./bin/mysqld_safe --datadir=/var/lib/mariadb --user=u --log-error=/var/log/mariadb/mariadb_err.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mariadb/mariadb.sock --port=3307
mysql     7365  7261  3 23:42 pts/2    00:00:00 /usr/local/mariadb/bin/mysqld --basedir=/usr/local/mariadb --datadir=/var/lib/mariadb --plugin-dir=/usr/local/mariadb/lib/plugin --user=u --log-error=/var/log/mariadb/mariadb_err.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mariadb/mariadb.sock --port=3307
root      7411  7054  0 23:42 pts/2    00:00:00 grep mariadb
[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# /usr/local/mariadb/bin/mysql -P 3307
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.8-MariaDB Source distribution

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)]> quit

8,設置mysql和mariadb的數據庫密碼

設置mysql的密碼

[root@rabbit ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password('123456') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2  Changed: 0  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> 

設置mariadb的密碼

[root@rabbit ~]# 
[root@rabbit ~]# /usr/local/mariadb/bin/mysql -P 3307
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 10.3.8-MariaDB Source distribution

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)]> 
MariaDB [(none)]> 
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> update user set password=password('123456') where user='root';
Query OK, 0 rows affected (0.045 sec)
Rows matched: 2  Changed: 0  Warnings: 0

MariaDB [mysql]> commit;
Query OK, 0 rows affected (0.000 sec)

MariaDB [mysql]> quit

 如今設在完了再不能無密碼登陸了,都須要-p參數指定密碼才行

9,開始升級操做

升級以前確定要先遷移數據

先導出mysql全部庫的數據

[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# mysqldump -A -uroot -p123456 >all.sql
[root@rabbit ~]# 

 

10,導入mysql說有數據到mariadb中

[root@rabbit ~]# 
[root@rabbit ~]# 
[root@rabbit ~]# /usr/local/mariadb/bin/mysql -h 127.0.0.1 -P 3307 <all.sql 
[root@rabbit ~]# 

 

11,驗證是否導入成功,且mariadb是否正常

由下面能夠看出mariadb沒有異常,而且mysql的tmp1庫也遷移到了mariadb中,1000條數據也都存在

注:我已經事先在mysql中建立量tmp1庫,並創建了1個stu表,插入了1000條數據。

[root@rabbit ~]# /usr/local/mariadb/bin/mysql -P 3307 -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 10.3.8-MariaDB Source distribution

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               |
| tmp1               |
+--------------------+
5 rows in set (0.001 sec)

MariaDB [(none)]> use tmp1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [tmp1]> show tables;
+----------------+
| Tables_in_tmp1 |
+----------------+
| stu            |
+----------------+
1 row in set (0.001 sec)

MariaDB [tmp1]> select count(*) from stu;
+----------+
| count(*) |
+----------+
|     1000 |
+----------+
1 row in set (0.001 sec)

MariaDB [tmp1]> quit

 

12,創建mysql和mariadb的主從同步(待做者更新加入操做細節)

讓mariadb實時的同步mysql的數據,直到數據徹底一致以後,選擇在業務低谷期,例如凌晨進行切換,即分離主從,中止mysql,啓用maraidb便可,操做步驟暫時沒時間寫

13,同步完成後解除主從關係(待做者更新加入操做細節)

14,中止mysql服務,保留mariadb(待做者更新加入操做細節)

15,變動應用的數據庫爲maraidb(待做者更新加入操做細節)

相關文章
相關標籤/搜索