Centos7.2 使用 yum 安装 MariaDB10.0

点击数:14

①添加MariaDB的yum源

1.创建MariaDB.repo

sudo vi /etc/yum.repos.d/Mariadb.repo

2.将以下文件中的字段添加到MariaDB.repo文件中

# MariaDB 10.0 CentOS repository list - created 2016-05-30 02:16 UTC# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
#baseurl = http://yum.mariadb.org/10.0/centos7-amd64
baseurl = http://archive.mariadb.org/mariadb-10.0.10/yum/centos6-amd64/
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck= 0

②yum安装MariaDB

# 直接安装方式:
sudo yum -y install MariaDB-server MariaDB-client
# 先下载后安装(适用于安装之后要在其他机器上安装的场景,比如POC):
sudo yum install --downloadonly --downloaddir=~/software/ MariaDB-server MariaDB-client MariaDB-shared MariaDB-common

注意:不装 MariaDB-shared 不会生成 mysql.service。

如果是通过yum install --downloadonly选项安装,这里仅仅是下载到指定的目录,需要执行安装命令将下载的软件安装本服务。

sudo rpm -ivh *.rpm
或者
sudo yum install *.rpm

安装输出的结果如下:

$ sudo rpm -ivh *.rpm
warning: MariaDB-10.0.38-centos73-x86_64-client.rpm: Header V4 DSA/SHA1 Signature, key ID 1bb943db: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:MariaDB-common-10.0.38-1.el7.cent################################# [ 33%]
   2:MariaDB-client-10.0.38-1.el7.cent################################# [ 67%]
   3:MariaDB-server-10.0.38-1.el7.cent################################# [100%]

④新建数据目录并创建mysql用户

mkdir -p /data01/mysql/data && useradd mysql -s  /sbin/nologin

⑤初始化数据库目录

使用mysql_install_db配置MariaDB服务

mysql_install_db   --datadir=\'/data01/mysql/data/\'

输出错误信息如下:

Neither host \’baymax-d-010108.hz.td\’ nor \’localhost\’ could be looked up with
\’/usr/sbin/resolveip\’
Please configure the \’hostname\’ command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the –force option

The latest information about mysql_install_db is available at
https://mariadb.com/kb/en/installing-system-tables-mysql_install_db

解决方法,加hosts无用,直接用--force

sudo /usr/bin/mysql_install_db --user=mysql  --datadir=\'/data01/mysql/data/\' --force

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

\’/usr/sbin/mysqladmin\’ -u root password \’new-password\’
\’/usr/sbin/mysqladmin\’ -u root -h baymax-d-010108.hz.td password \’new-password\’

Alternatively you can run:
\’/usr/sbin/mysql_secure_installation\’

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd \’/usr\’ ; /usr/sbin/mysqld_safe –datadir=\’/data01/mysql/data\’

You can test the MariaDB daemon with mysql-test-run.pl
cd \’/usr/mysql-test\’ ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB\’s strong and vibrant community:
https://mariadb.org/get-involved/

⑥修改权限并手工命令启动

chown mysql:mysql /data01/mysql/data/ -R
cd /usr ; /usr/bin/mysqld_safe --datadir=\'/data01/mysql/data\' &

⑦初始化数据库权限

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.

#由于一开始安装MariaDB数据库后, root用户默认密码为空, 所以只需要按Enter键
Enter current password for root (enter for none):

#是否设置root用户的新密码
Set root password? [Y/n] y

#录入新密码
New password:

#确认新密码
Re-enter new password:

#是否删除匿名用户,生产环境建议删除
Remove anonymous users? [Y/n] y

#是否禁止root远程登录,根据自己的需求选择
Disallow root login remotely? [Y/n] n

#是否删除test数据库
Remove test database and access to it? [Y/n] y

#是否重新加载权限表
Reload privilege tables now? [Y/n] y

⑧启动\\重启\\关闭mysql服务

#注意,不是 systemctl start mariadb
sudo systemctl start mysql
sudo systemctl restart mysql
sudo systemctl stop mysql
sudo systemctl status mysql

如果在centos中安装mariadb后,无法启动Mariadb,提示Unit not found。请安装mariadb-shared组件。

sudo yum install mariadb-shared

另外一个需要注意,在centos7中,使用mysql.service替换了mariadb.service,因为centos7默认已经用mariadb替换了mysql,所以如果你想安装mysql,其实也是安装的mariadb。

⑨修改编码集

查看/etc/my.cnf文件内容,其中包含一句!includedir /etc/my.cnf.d说明在该配置文件中引入/etc/my.cnf.d目录下的配置文件。

1)使用vi server.cnf命令编辑server.cnf文件,在[mysqld]标签下添加:

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

如果/etc/my.cnf.d目录下无server.cnf文件,则直接在/etc/my.cnf文件的[mysqld]标签下添加以上内容。

2)用vi client.cnf命令编辑/etc/my.cnf.d/client.cnf文件,在[client]标签下添加:

default-character-set=utf8

3)用vi mysql-clients.cnf命令编辑/etc/my.cnf.d/mysql-clients.cnf文件,在[mysql]标签下添加:

default-character-set=utf8

配置完成后sudo systemctl restart mariadb重启服务。

⑩开启远程访问

1.防火墙添加3306端口

查看firewall状态

firewall-cmd --state

状态是not running,启动firewall

systemctl start firewalld

状态是running
开放3306端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent

重新载入

firewall-cmd --reload

2.root如果没有开启远程且需要远程访问,则需要重新去MariaDB赋权限

#进入Mariadb
mysql -uroot -p
#选择数据库
use mysql;
#添加权限
Grant all on *.* to \'root\'@\'%\' identified by \'root用户的密码\' with grant option;
#重新载入
flush privileges;

此外,如果要更改root用户的密码,需要格外注意,执行update user set Password=password(\'ABC@123\') where User = \'root\';的语句会出错:ERROR 1348 (HY000): Column \'Password\' is not updatable

当然如果你想ALTER user \'root\'@\'localhost\' identified by \'ABC@123\';这样修改,也会报错:ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement

所以,必须要先执行:flush privileges;,然后ALTER user \'root\'@\'localhost\' identified by \'ABC@123\';来修改 root 密码,不支持update修改root密码

附上错误信息:

MariaDB [mysql]> update user set Password=password(\’ABC@123\’) where User = \’root\’;
ERROR 1348 (HY000): Column \’Password\’ is not updatable
MariaDB [mysql]>
MariaDB [mysql]> update user set authentication_string=password(\’ABC@123\’) where User = \’root\’;
ERROR 1348 (HY000): Column \’authentication_string\’ is not updatable
MariaDB [mysql]>
MariaDB [mysql]> ALTER user \’root\’@\’localhost\’ identified by \’ABC@123\’;
ERROR 1290 (HY000): The MariaDB server is running with the –skip-grant-tables option so it cannot execute this statement
MariaDB [mysql]> ALTER user \’root\’@\’localhost\’ identified by \’ABC@123\’;
ERROR 1290 (HY000): The MariaDB server is running with the –skip-grant-tables option so it cannot execute this statement

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据