由于mariadb不支持json操作符->>,只得卸载mariadb,并安装mysql

1.玛莉亚(maria)不喜欢->>

今天我的一个基于mysql的程序,在mariadb上跑,结果报错了,查了一下,原来是不支持如下sql:

SELECT object.id as u1_rva_object_id, 
object.no as u1_rva_object_no, 
object.name as u1_rva_object_name, 
object.data->>'$.propNameIndex' as u1_rva_object_prop_name_index, 
FROM rva_object `object` WHERE (object.id = 'rva_ceshi')

这个sql有个特殊的操作符 ->> ,是操作json类型的数据库字段data的,获取其json属性propNameIndex。

这种操作在mysql5.7是支持的妥妥的,但是到了mariadb10.2.*,却是折戟沉沙了。

于是,我只能在CentOS7上干掉mariadb,重装mysql5.7了。

2.下载mysql5.7

MySQL官网下载地址

下载需要注册oracle账号,所以无法在CentOS上下载,只能在windows机器上下载,然后再上传到CentOS机器上。

如何上传我就不多说了,请参考我之前的文档中,WinSCP工具的安装和使用:

在centos7上安装开发环境(jdk8、mariadb和tomcat9)​​​​​​

3.安装mysql

3.1.解压

我的mysql上传路径:/home/software/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

tar zxvf /home/software/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local
mv mysql-5.7.36-linux-glibc2.12-x86_64 mysql

3.2.跟玛莉亚say bye bye

systemctl stop mariadb
yum remove mariadb-server

3.3.创建用户组和用户

# 创建组
groupadd mysql

#创建用户
useradd -r -g mysql mysql

其实之前的玛莉亚已经创建了上述用户和组,所以不执行也可以,但如果是全新安装mysql,则还是需要!

这样做是出于安全性的考虑,给mysql数据库创建专有用户,该用户只能访问mysql目录,不能访问系统其它目录

如果直接用root初始化mysql,连接mysql时会报错:[ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!

3.4.给mysql目录指定专有用户和用户组

# 首先创建data目录:

mkdir /usr/local/mysql/data

# 指定用户和用户组:

cd /usr/local

chown -R mysql mysql/

chgrp -R mysql mysql/

-R包含目录下所有和目录和文件

3.5.初始化mysql

cd /usr/local/mysql/bin

./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US

系统打印信息如下:

2021-11-26T09:41:53.959092Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-11-26T09:41:56.761359Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-11-26T09:41:57.302162Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-11-26T09:41:57.443285Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 18cb00ba-4e9d-11ec-911f-60eb696a0041.
2021-11-26T09:41:57.472893Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-11-26T09:41:58.602919Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2021-11-26T09:41:58.602962Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2021-11-26T09:41:58.626580Z 0 [Warning] CA certificate ca.pem is self signed.
2021-11-26T09:41:58.925864Z 1 [Note] A temporary password is generated for root@localhost: wWQ7r1rghk?(

最后一行的最后一个字符串,就是mysql的root用户初始化密码!

如果忘记密码或者想重新初始化,可以先将mysql/data目录中文件删除,然后再执行初始化命令

3.6.配置mysql

修改配置文件:/etc/my.cnf

vi /etc/my.cnf

修改信息如下:

[mysqld]
basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data
init_connect='SET collation_connection = utf8_unicode_ci' 
init_connect='SET NAMES utf8' 
character-set-server=utf8 
collation-server=utf8_unicode_ci 
skip-character-set-client-handshake
lower_case_table_names=1

[mysql]
no-auto-rehash
default-character-set=utf8
safe-updates

[client]
default-character-set=utf8

注意[mysqld],[mysql],[client]等模块,相应的配置必须如上对应才行

下面是一个完成文件样例:

[mysqld]
basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data
# 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
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
lower_case_table_names=1

[mysql]
no-auto-rehash
default-character-set=utf8
safe-updates

[client]
default-character-set=utf8

[mysqld_safe]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

3.7.设置mysql自启动

cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql

修改/etc/init.d/mysql,将mysql目录填上:

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

下面是完整文件示例:

#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind

# MySQL daemon start/stop script.

# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.

# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.

# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: mysql
# Required-Start: $local_fs $network $remote_fs
# Should-Start: ypbind nscd ldap ntpd xntpd
# Required-Stop: $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop MySQL
# Description: MySQL is a very fast and reliable SQL database engine.
### END INIT INFO

# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]
#   basedir=
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don't wait at all

3.8.启停服务

启动服务:service mysql start

停止服务:service stop start

重启服务:service mysql restart

3.9.设置远程访问权限

cd /usr/local/mysql/bin

# 登录:
./mysql -u root -p

#然后输入之前初始化mysqld时生成的密码:
wWQ7r1rghk?(

#修改密码为xxx:
set password=password("xxx");

# 登录授权:
grant all privileges on *.* to 'root'@'%' identified by 'xxx';

# 授权生效:
flush privileges;

然后就可使用navicat或sqlyog等工具进行登录,注意关闭防火墙或开放3306端口,请参考:

在centos7上安装开发环境(jdk8、mariadb和tomcat9)​​​​​​

其中第五节。

3.10 使用中的问题

在mysql数据库中建表时,报错如下(表可以成功创建,只是报错而已,晕!!!):

[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

虽然这个问题不影响使用,但为了谨慎期间,还是处理一下比较好!

根据上面错误提示,查询mysql中的sql_mode:

select @@sql_mode

# 返回结果:
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

去掉查询结果中的ONLY_FULL_GROUP_BY,在/etc/my.cnf中配置如下参数:

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

记得配置到[mysqld]模块中哟!

[mysqld]
basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data
# 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
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

[mysql]
no-auto-rehash
default-character-set=utf8
safe-updates

[client]
default-character-set=utf8

[mysqld_safe]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

重启mysqld服务

service mysql restart

# 系统提示如下
Shutting down MySQL.... SUCCESS! 
Starting MySQL.. SUCCESS! 

然后再执行建表脚本,没有出现错误提示了!

Bingo!

你可能感兴趣的:(MySQL,CentOs7,mysql安装,mariadb,不支持json操作符)