Linux下安装MariaDB10.3/MySQL5.6

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Powerful_Fy/article/details/102408292

MariaDB官网:https://mariadb.org/
可以直接在Linux系统中使用wget下载,也可以在官网选择对应的包下载,官网下载包有:源码包、zip包、rpm包(源码包编译比较耗费时间)

*本文中以MariaDB-10.3.18版本zip包为例

mariadb-10.3.18-linux-x86_64.tar.gz包存放路径 /usr/local/src/

创建MariaDB数据存放目录:

[root@linux ~]# mkdir -p /data/mysql

创建用户:

[root@linux ~]# useradd -M -s /sbin/nologin mysql

# -M:不创建用户家目录,-s:指定shell为nologin(禁止登陆)

更改/data/mysql/目录所属主和用户组:

[root@linux ~]# chown -R mysql:mysql /data/mysql/

解压:

[root@linux ~]# cd /usr/local/src/
[root@linux src]# tar -zxvf mariadb-10.3.18-linux-x86_64.tar.gz

移动MariaDB目录并更名为mysql: (定义目录名为mysql属于习惯,也便于他人维护)

[root@linux src]# mv mariadb-10.3.18-linux-x86_64 /usr/local/mysql

初始化:

[root@linux src]# cd /usr/local/mysql/
[root@linux mysql]# ./scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql

# 指定数据存放目录和用户,如果在这步出现报错:libaio.so.1 ……的错误信息,安装libaio即可,命令:yum -y install libaio libaio-devel

拷贝启动脚本并更名为mysqld:

root@linux mysql]# cp support-files/mysql.server /etc/init.d/mysqld

编辑启动脚本:

[root@linux mysql]# vi /etc/init.d/mysqld 

添加MariaDB程序目录和数据存放目录:

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

编辑配置文件:

[root@linux mysql]# vi /etc/my.cnf

更改以下项的对应路径:

[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/data/mysql/mariadb.log
pid-file=/data/mysql/mariadb.pid

启动MariaDB:

[root@linux mysql]# /etc/init.d/mysqld start
Reloading systemd:                                         [  确定  ]
Starting mysqld (via systemctl):                           [  确定  ]

补充:
查看是否启动成功:ps -aux|grep mysql
或者查看3306端口是否被监听:netstat -lnp

添加到服务:

[root@linux ~]# chkconfig --add mysqld
[root@linux ~]# chkconfig --list

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关

# 添加到服务的前提:文件需要在/etc/init.d/目录下,且有执行权限

添加服务完成后即可使用service启动/关闭/重启mysql:

[root@linux ~]# service mysqld start
Starting mysqld (via systemctl):                           [  确定  ]

登录MySQL/MariaDB:

[root@linux ~]# /usr/local/mysql/bin/mysql -u root

补充:
如需直接使用mysql -u root 登录可以使用两种方法解决:
1.添加软连接后即可直接使用mysql -u root 登录MySQL/MariaDB

[root@linux ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
[root@linux ~]# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.18-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)]> 

2.将mysql程序目录添加到系统环境变量后即可直接使用mysql -u root 登录MySQL/MariaDB

[root@linux ~]# PATH=$PATH:/usr/local/mysql/bin
[root@linux ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@linux ~]# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.3.18-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)]> 

# 该方法只在当前终端生效

使其永久生效:(更改变量配置文件/etc/profile)

[root@linux ~]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile

立即生效:(无需断开终端重连)

[root@linux ~]# source /etc/profile

设置MariaDB数据库的root用户密码:

[root@linux ~]# mysqladmin -u root password "123456"

重新登录即需要使用-p 参数来验证密码:

[root@linux ~]# mysql -u root -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 10.3.18-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)]>

远程登录其他机器的MariaDB数据库:

[root@linux ~]# mysql -uroot -p123456 -h192.168.234.130 -P3306

# -h:指定登录用户的host,-P:指定端口号

补充:
启动mysql的第二种方式:
1.进入mysql的bin目录下:
/usr/local/mysql/bin/
2.启动
mysqld_safe --defaults-file=/etc/my.cnf --datadir=/data/mysql --user=mysql
#在使用service 或者 systemctl启动报错启动不了的时候,使用mysql_safe

猜你喜欢

转载自blog.csdn.net/Powerful_Fy/article/details/102408292