mysql/MariaDB学习1.1 Centos7下MariaDB的安装和使用

环境:

CentOS 7,mariadb-server.x86_64 1:5.5.56-2.el7

摘要说明:

MariaDB:MariaDB和mysql开发团队一致,区别在于mysql已经被oracle闭源了而mariaDB是开源的。MySQL之父Widenius先生离开了Sun之后,觉得依靠Sun/Oracle来发展MySQL,实在很不靠谱,于是决定从新开发代码全部开源免费关系型数据库,这就是MariaDB。在navicat中操作mariaDB的界面和提示符还是mysql端口也是3306 外人看起来除了数据库名字改了其他和mysql完全一致。

mariaDB在各方面都是mysql创新和提高版本 而不是简单的替代品。

本篇文章主要讲述在CentOS 7系统下默认因musql闭源而无musql的yum源,默认存在MariaDB的yum源,本篇讲述如何使用yum安装及设置MariaDB数据库。

步骤:

1.安装MariaDB

安装之前我们可以试着安装mysql可能出现下述情况:

No package mysql-server available.
Error: Nothing to do

或使用 yum repolist all | grep mysql查看mysql源;

但可以发现无mysql的安装源,这里先安装MariaDB;

使用yum安装MariaDB:

yum -y install mariadb-server

2.启动MariaDB及设置开机启动

启动MariaDB:

systemctl start mariadb

设置开机启动:

systemctl enable mariadb

3.初始化MariaDB

使用mysql_secure_installation初次配置MariaDB:

[root@iZbp1dham6enej0lrs00riZ mariadb]# 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
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
 ... 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
 ... 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] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 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!

注:

1.Enter current password for root (enter for none)  --》第一次直接回车即可;

2.Set root password? [Y/n] y  --》是否设置root密码,输入y设置;

3.New password:   --》第一次输入密码;

4.Re-enter new password:   --》再次输入密码;

5.Remove anonymous users? [Y/n] y  --》是否删除匿名用户,输入y;

6.Disallow root login remotely? [Y/n] n  --》是否允许远程登录,输入y;

7.Remove test database and access to it? [Y/n] n  --》是否移出test数据库,输入n;

8.Reload privilege tables now? [Y/n] y  --》是否重新加载权限表,输入y;

4.配置MariaDB

a.设置去掉表名的大小写敏感(否则莫名其妙的表不存在):

使用vi在/etc/my.cnf的mysqld下加入:

vi /etc/my.cnf
lower_case_table_names=1

重启MariaDB

systemctl restart mariadb

b.设置远程连接

使用grant all privileges on *.* to root@"%" identified by "new password"进行远程连接设置;

[root@iZbp1dham6enej0lrs00riZ ~]# mysql -u root -p
Enter password: 
MariaDB [(none)]> use mysql;
MariaDB [mysql]> select host,user from user;
MariaDB [mysql]> grant all privileges on *.* to root@"%" identified by "new password";
MariaDB [mysql]> select host,user from user;
MariaDB [mysql]> flush privileges;

5.测试MariaDB

使用Navicat连接测试:连接方式如mysql一致连接成功;

使用jdbc的mysql测试连接仍然可以连接:com.mysql.jdbc.Driver;

上述测试mysql的使用在MariaDB下仍可使用;

但MariaDB也有自己的连接jar:使用mariadb-java-client连接mariadb数据库;

<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <version>1.7.4</version>
</dependency>

MariaDB自己的连接驱动为:org.mariadb.jdbc.Driver

MariaDB自己的连接地址为:jdbc:mariadb://localhost:3306/test

猜你喜欢

转载自blog.csdn.net/u010904188/article/details/81480316