设为首页 收藏本站
查看: 842|回复: 0

[经验分享] mysql5.7 升级到 mariadb 10.2.16-yuweibing的技术博客

[复制链接]

尚未签到

发表于 2018-10-5 08:41:46 | 显示全部楼层 |阅读模式
DSC0000.jpg DSC0001.jpg

  上面左边是我的个人微信,如需进一步沟通,请加微信。  右边是我的公众号“Openstack私有云”,如有兴趣,请关注。
  记录一个mysql数据库升级操作。
  客户环境不能连外网,因此所有程序应用的安装都只能通过提前下载离线软件安装包上传到目标服务器上进行安装。原来的mysql5.7数据库也是通过rpm软件包进行安装的。
  数据库服务器操作系统是红帽的rhel7.2 ,64位。
  客户的一台mysql服务器漏洞扫描发现有漏洞,数据库版本如下:
mysql> status  
--------------
  
mysql  Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using  EditLine wrapper
  
Connection id:15176291
  
Current database:
  
Current user:mysql@localhost
  
SSL:Not in use
  
Current pager:stdout
  
Using outfile:''
  
Using delimiter:;
  
Server version:5.5.5-10.0.21-MariaDB-log MariaDB Server
  
Protocol version:10
  
Connection:Localhost via UNIX socket
  
Server characterset:latin1
  
Db     characterset:latin1
  
Client characterset:utf8
  
Conn.  characterset:utf8
  
UNIX socket:/opt/beh/core/mysql/sock/mysql.sock
  
Uptime:107 days 2 hours 11 min 53 sec
  版本有点奇怪,直接使用mysql --version看到的版本是mysql server 5.7.2 ,但是上面status中的Server version:却是5.5.5-10.0.21-MariaDB-log MariaDB Server。反正不管怎么样,都是需要升级到 mariadb 10.1以上的版本。到官网上查看最新的稳定版本,官网下载地址如下:
  https://downloads.mariadb.org/
  看到10.2.16是稳定版本,该版本的下载地址如下:
  http://mariadb.mirror.iweb.com//mariadb-10.2.16/bintar-linux-systemd-x86_64/mariadb-10.2.16-linux-systemd-x86_64.tar.gz
  升级思路:
  由于版本跨度太大,不适合逐个版本升级,采用重新安装的方法,即将原来的版本卸载,然后重新安装新的版本。
  为了保证数据安全,选择另一台同版本操作系统的服务器作为备机,安装一模一样的数据库版本,并且将原数据库服务器的数据库通过mysqldump命令导出,并导入的备机中。备机准备好之后,业务应用程序修改数据库连接配置连接到备机数据库。然后再对原数据库服务器进行数据库升级操作。
  操作记录:
  数据库备份:
    将原数据库进行全库备份:  
    mysqldump -u root -p --all-databases >mysqldump20180712.sql
  
    对业务数据库hive也进行单独备份:
  
    mysqldump -u root -p hive >hive20180712.sql
  数据库备机安装:
  原数据库服务器已经安装的rpm包如下:
  [root@hadoop001 dbdata]# rpm -qa|grep mysql
  mysql-community-client-5.7.20-1.el7.x86_64
  mysql-community-common-5.7.20-1.el7.x86_64
  mysql-community-libs-5.7.20-1.el7.x86_64
  mysql-community-devel-5.7.20-1.el7.x86_64
  mysql-community-server-5.7.20-1.el7.x86_64
原数据库服务器中还保留着最初的mysql5.7的所有安装rpm包,将原数据库服务器中的rpm安装包文件全部拷贝到备机中:  
    scp mysql-community-* hadoop002:/data_china/ywb/
  
    在备机中安装mysql相关软件包:
  
    yum localinstall mysql-community-*
  

  
    备机启动mysql数据库:
  
    systemctl start mysql
  数据库备机数据导入:
将原数据库中的数据库备份文件传到备机:  
    scp mysqldump20180712.sql  hadoop002:/data_china/ywb/
  

  
    在备机上恢复mysql数据库:
  
    mysql -u root -p show databases;
  
    mysql>use hive;
  
    mysql>show tables;
  
    mysql>quit;
  验证成功之后,通知业务应用端修改程序端数据库连接配置,使业务应用连接到备机数据库。一切正常之后,开始对原数据库服务器进行升级。
  原数据库升级:
  1、删除原mysql5.7版本数据库:
首先,将原数据库服务器的mysql5.7程序和数据库彻底删除:  
rpm -e mysql-community-server-5.7.20-1.el7.x86_64
  
rpm -e mysql-community-client-5.7.20-1.el7.x86_64
  
rpm -e mysql-community-common-5.7.20-1.el7.x86_64
  
rpm -e mysql-community-libs-5.7.20-1.el7.x86_64
  
rpm -e mysql-community-devel-5.7.20-1.el7.x86_64
  
    注意,执行上面的命令的时候其中有几个命令会失败,是由于有依赖关系,相关顺序调整一下即可。
删除残余文件,通过下面2条命令查找残余文件:  
    find / -name mysql
  
    whereis mysql
  上面两个命令会查找mysql相关文件,将相关的文件能删除的都删除,如果为了保险,可以将一些不确定能删的文件mv到一个特定目录。谨慎些还是不会错。
  mysql5.7删除干净之后,接下来就是按照新的mariadb 10.2.16版本的数据库:
  2、安装新版本mariadb 10.2.16 数据库
  将上面准备好的软件安装包mariadb-10.2.16-linux-systemd-x86_64.tar.gz 上传到原数据库服务器hadoop001中,然后解压:
  tar xzvf mariadb-10.2.16-linux-systemd-x86_64.tar.gz
#更改安装目录:注意不要提前建立/usr/local/mysql目录,直接放过去即可  
mv mariadb-10.2.16-linux-systemd-x86_64 /usr/local/mysql/
  

  
#进入/usr/local/mysql目录
  
[root@toydns mysql]#cd  /usr/local/mysql
  
#在/usr/local/mysql/support-files/下的配置文件模板,已经配置好的部分参数,分别用于不同的环境,这里说明一下:
  
my-small.cnf 这个是为小型数据库或者个人测试使用的,不能用于生产环境
  
my-medium.cnf 这个适用于中等规模的数据库,比如个人项目或者小型企业项目中,
  
my-large.cnf 一般用于专门提供SQL服务的服务器中,即专门运行数据库服务的主机,配置要求要更高一些,适用于生产环境
  
my-huge.cnf 用于企业级服务器中的数据库服务,一般更多用于生产环境使用
  
所以根据以上几个文件,如果个人使用或者测试,那么可以使用前两个模板;企业服务器或者64G以上的高配置服务器可以使用后面两个模板,另外也可以根据自己的需求来加大参数和扩充配置获得更好的性能。
  
[root@toydns mysql]# ll support-files/
  
-rw-r--r--. 1 1021 1004  4914 Nov 27 18:32 my-huge.cnf
  
-rw-r--r--. 1 1021 1004 20421 Nov 27 18:32 my-innodb-heavy-4G.cnf
  
-rw-r--r--. 1 1021 1004  4901 Nov 27 18:32 my-large.cnf
  
-rw-r--r--. 1 1021 1004  4914 Nov 27 18:32 my-medium.cnf
  
-rw-r--r--. 1 1021 1004  2840 Nov 27 18:32 my-small.cnf
  
#复制my-medium.cnf到etc下并改名为my.cnf,修改数据库文件存放的目录
  
cp support-files/my-medium.cnf  /etc/my.cnf
  
vi /etc/my.cnf
  
# The MariaDB server
  
[mysqld]
  
port            = 3306
  
socket          = /tmp/mysql.sock
  
basedir = /usr/local/mysql   //mysql目录
  
datadir= /var/lib/mysql  //数据存放位置
  
innodb_file_per_table = on  //每张表一个单独文件,便于管理
  
skip_name_resolve = on  //忽略反向解析,加快访问速度
  
skip-external-locking
  
key_buffer_size = 16M
  
max_allowed_packet = 1M
  
table_open_cache = 64
  
sort_buffer_size = 512K
  
net_buffer_length = 8K
  
read_buffer_size = 256K
  
read_rnd_buffer_size = 512K
  
myisam_sort_buffer_size = 8M
  
:wq保存后给权限
  
注意,datadir= /var/lib/mysql 这个目录需要提前准备好:
  
mkdir /var/lib/mysql
  
chown mysql:mysql /var/lib/mysql
  
#添加MySQL到PATH环境变量里面去,省的以后敲命令麻烦
  
[root@toydns mysql]# vi /etc/profile.d/mysql.sh
  
export  PATH=/usr/local/mysql/bin:$PATH
  
source /etc/profile.d/mysql.sh  使这个配置文件及时生效
  
#初始化mysql
  
/usr/local/mysql/scripts/mysql_install_db --user=mysql
  
#创建mariadb自启动脚本,并加入开机启动
  
cp /usr/local/mysql/support-files/systemd/mariadb.service  /usr/lib/systemd/system/
  
此处有坑,特别注意:
  
/usr/lib/systemd/system/mariadb.service 这个脚本中有一个参数 ProtectSystem=full ,需要改为false
  
# Prevent writes to /usr, /boot, and /etc
  
ProtectSystem=false
  
默认情况下ProtectSystem=full,/usr目录被保护不能写入数据,上面设置的datadir目录最初是设置在/usr/local/mysql/data中,执行
  
systemctl start mariadb
  
的时候会报错无法写入XXX文件,因为/usr整个目录都被写保护。解决方法有两个,一个是将ProtectSystem=full 改为ProtectSystem=false ,一个就是更改datadir目录位置,不要放在/usr目录下。我在这里是2个地方都改掉。
  
最后使能服务,使之主机重启后能自动启动。
  
systemctl enable mariadb
  
启动mysql数据库:
  
systemctl start mariadb
  MySQL的安全设置
[root@toydns mysql]# ./bin/mysql_secure_installation  
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     //是否设置MySQL管理员root的密码,y设置,输入2次
  
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    //是否删除匿名账户 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   //是否不允许root用户远程登陆,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] y   //是否删除test测试数据库,y删除
  
- Dropping test database...
  
... Success!
  
- Removing privileges on test database...
  
... Success!
  
Reloading the privilege tables will ensure that all changes made so far
  
will take effect immediately.
  
Reload privilege tables now? [Y/n] y   //重新加载可用的数据库表  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!
  3、导入数据库备份
  新版本数据库mariadb 10.2.16 安装完成之后,将原来备份的数据库导入:
    mysql -u root -p

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-612470-1-1.html 上篇帖子: linux安装mysql5.7.22详细步骤 下篇帖子: Linux下卸载残废的MySQL-OptimusSnow
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表