大家好,又见面了,我是你们的朋友全栈君。
一、下载安装
mariadb是属于mysql的一个分支,是其创始人在mysql被卖给oracle之后重新分出来的,maria取自于他女儿的名字。mariadb完全兼容于mysql,在很多新版本的linux系统中,mysql都已经被替换成了mariadb。
mariadb的官网:mariadb官网,下载地址:下载地址。最新稳定版本的下载直链为:
wget https://downloads.mariadb.com/MariaDB/mariadb-10.5.0/bintar-linux-systemd-x86_64/mariadb-10.5.0-linux-systemd-x86_64.tar.gz
1
wgethttps://downloads.mariadb.com/MariaDB/mariadb-10.5.0/bintar-linux-systemd-x86_64/mariadb-10.5.0-linux-systemd-x86_64.tar.gz
首先把安装包下载到本地,然后解压到/usr/local目录:
tar -zxvf mariadb-10.5.0-linux-systemd-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/mariadb-10.5.0-linux-systemd-x86_64/ /usr/local/mysql
1
2
tar-zxvfmariadb-10.5.0-linux-systemd-x86_64.tar.gz-C/usr/local/
ln-s/usr/local/mariadb-10.5.0-linux-systemd-x86_64//usr/local/mysql
初始化数据库,设定数据存储目录为/appdata/mysql,启动用户为mysql:
# 创建mysql用户
useradd -s /sbin/nologin -M mysql
# 创建数据库文件夹
mkdir /appdata/mysql -p
# 初始化数据库
/usr/local/mysql/scripts/mysql_install_db \
–basedir=/usr/local/mysql \
–datadir=/appdata/mysql \
–user=mysql
1
2
3
4
5
6
7
8
9
# 创建mysql用户
useradd-s/sbin/nologin-Mmysql
# 创建数据库文件夹
mkdir/appdata/mysql-p
# 初始化数据库
/usr/local/mysql/scripts/mysql_install_db\
–basedir=/usr/local/mysql\
–datadir=/appdata/mysql\
–user=mysql
初始化数据库的过程中如果报错:
> Installing MariaDB/MySQL system tables in ‘/xxx/mariadb’ …
/usr/local/mariadb/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
Installation of system tables failed! Examine the logs in
/udata/mariadb for more information.
…
1
2
3
4
5
6
7
>InstallingMariaDB/MySQLsystemtablesin’/xxx/mariadb’…
/usr/local/mariadb/bin/mysqld:errorwhileloadingsharedlibraries:libaio.so.1:cannotopensharedobjectfile:Nosuchfileordirectory
Installationofsystemtablesfailed!Examinethelogsin
/udata/mariadbformoreinformation.
…
说明系统缺少组件库libaio,需要安装手动安装:
# centos
yum install libaio libaio-devel
# ubuntu
apt install libaio1
1
2
3
4
# centos
yuminstalllibaiolibaio-devel
# ubuntu
aptinstalllibaio1
执行成功后输出:
Installing MariaDB/MySQL system tables in ‘/appdata/mysql’ …
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
…
1
2
3
4
5
6
7
InstallingMariaDB/MySQLsystemtablesin’/appdata/mysql’…
OK
Tostartmysqldatboottimeyouhavetocopy
support-files/mysql.servertotherightplaceforyoursystem
…
到这里数据库就已经安装完成了,接下来要做的就是配置。
二、配置
修改my.cnf,设置pid/socket/log等文件的路径,把它们统一存到/appdata/mysql/run/下:
[mysqld]
datadir=/appdata/mysql
socket=/appdata/mysql/run/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=/appdata/mysql/run/mysql.log
pid-file=/appdata/mysql/run/mysql.pid
[mysql]
socket=/appdata/mysql/run/mysql.sock
[mysqladmin]
socket=/appdata/mysql/run/mysql.sock
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[mysqld]
datadir=/appdata/mysql
socket=/appdata/mysql/run/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=/appdata/mysql/run/mysql.log
pid-file=/appdata/mysql/run/mysql.pid
[mysql]
socket=/appdata/mysql/run/mysql.sock
[mysqladmin]
socket=/appdata/mysql/run/mysql.sock
#
# include all files from the config directory
#
!includedir/etc/my.cnf.d
注意:
/appdata/mysql/run目录要提前创建
如果修改了socket的路径,还要修改[mysql]和[mysqladmin]段的socket路径,要和[mysqld]中的socket路径一致
设置路径权限:
chown mysql.mysql -R /usr/local/mysql /appdata/mysql
1
chownmysql.mysql-R/usr/local/mysql/appdata/mysql
添加mysql命令到系统路径,修改/etc/profile文件:
MYSQL_HOME=/usr/local/mysql
export PATH=PATH:MYSQL_HOME/bin
1
2
MYSQL_HOME=/usr/local/mysql
exportPATH=PATH:MYSQL_HOME/bin
修改后source /etc/profile生效。
三、添加系统服务
3.1 service系统服务
对于使用service命令启动的服务,复制mysql主目录下的support/mysql.server文件到/etc/init.d/:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
1
2
cp/usr/local/mysql/support-files/mysql.server/etc/init.d/mysqld
chmod+x/etc/init.d/mysqld
然后修改文件中的配置:
basedir=/usr/local/mysql # 安装目录
datadir=/appdata/mysql # 数据目录
mysqld_pid_file_path=/appdata/mysql/run/mysql.pid # pid文件目录
1
2
3
basedir=/usr/local/mysql# 安装目录
datadir=/appdata/mysql# 数据目录
mysqld_pid_file_path=/appdata/mysql/run/mysql.pid# pid文件目录
注意:配置要和上面my.cnf中的配置一一对应
启动:service mysqld start。
添加到开机启动:
chkconfig –add mysqld
chkconfig mysqld on
1
2
chkconfig–addmysqld
chkconfigmysqldon
3.2 systemd系统服务
systemd服务的文件在安装路径/support-files/systemd/mariadb.service:
cp support-files/systemd/mariadb.service /etc/systemd/system/mysqld.service
1
cpsupport-files/systemd/mariadb.service/etc/systemd/system/mysqld.service
复制完后执行systemctl start mysqld启动服务,然后设置开机启动:
systemctl enable mysqld
1
systemctlenablemysqld
四、设置root用户密码
系统服务起来后,可以使用mysqladmin初始化root用户的密码:
mysqladmin -u root password ‘123456’
1
mysqladmin-urootpassword’123456′
如果出现:
说明没有没有权限登录,需要通过安全模式启动mysql来修改root密码,在my.cnf中添加以下内容:
[mysqld]
skip-grant-tables
skip-networking
1
2
3
[mysqld]
skip-grant-tables
skip-networking
然后重启服务,使用root身份登录(不用密码),再执行命令修改密码:
use mysql;
# 刷新权限
flush privileges;
# 设置密码
set password for ‘root’@’localhost’ = password(‘123456’);
# 刷新权限
flush privileges;
1
2
3
4
5
6
7
usemysql;
# 刷新权限
flushprivileges;
# 设置密码
set password for’root’@’localhost’=password(‘123456’);
# 刷新权限
flushprivileges;
如果执行命令的时候出现报错:
ERROR 1290 (HY000): The MariaDB server is running with the –skip-grant-tables option so it cannot execute this statement
1
ERROR1290(HY000):TheMariaDBserverisrunningwiththe–skip-grant-tablesoptionsoitcannotexecutethisstatement
说明安全模式下的权限还没有更新,要先刷新一下权限才行:
flush privileges;
1
flushprivileges;
修改完成后去掉my.cnf中添加的参数,重启服务,使用上面设置的密码登陆就可以了:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.4.8-MariaDB MariaDB Server
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)]>
1
2
3
4
5
6
7
8
9
WelcometotheMariaDBmonitor.Commandsendwith;or\g.
YourMariaDBconnectionidis11
Serverversion:10.4.8-MariaDBMariaDBServer
Copyright(c)2000,2018,Oracle,MariaDBCorporationAbandothers.
Type’help;’or’\h’forhelp.Type’\c’toclearthecurrentinputstatement.
MariaDB[(none)]>
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133664.html原文链接:https://javaforall.cn