在 CentOS 7(Linux)上安装MariaDB,搭建Mysql服务(Centos-7.6、Mysql)

 一、安装MariaDB

安装命令

yum -y install mariadb mariadb-server

安装完成MariaDB,首先启动MariaDB、然后查看服务启动状态

systemctl start mariadb
systemctl status mariadb
systemctl enable mariadb

具体操作如下


[root@centos001 ~]# systemctl start mariadb [root@centos001 ~]# systemctl status mariadb ● mariadb.service - MariaDB database server Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2019-05-08 15:23:10 CST; 14s ago Process: 13148 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS) Process: 13069 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS) Main PID: 13147 (mysqld_safe) CGroup: /system.slice/mariadb.service ├─13147 /bin/sh /usr/bin/mysqld_safe --basedir=/usr └─13309 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb... May 08 15:23:08 centos001 mariadb-prepare-db-dir[13069]: MySQL manual for more instructions. May 08 15:23:08 centos001 mariadb-prepare-db-dir[13069]: Please report any problems at http://mariadb.org/jira May 08 15:23:08 centos001 mariadb-prepare-db-dir[13069]: The latest information about MariaDB is available at http://mariadb.org/. May 08 15:23:08 centos001 mariadb-prepare-db-dir[13069]: You can find additional information about the MySQL part at: May 08 15:23:08 centos001 mariadb-prepare-db-dir[13069]: http://dev.mysql.com May 08 15:23:08 centos001 mariadb-prepare-db-dir[13069]: Consider joining MariaDB's strong and vibrant community: May 08 15:23:08 centos001 mariadb-prepare-db-dir[13069]: https://mariadb.org/get-involved/ May 08 15:23:08 centos001 mysqld_safe[13147]: 190508 15:23:08 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'. May 08 15:23:08 centos001 mysqld_safe[13147]: 190508 15:23:08 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql May 08 15:23:10 centos001 systemd[1]: Started MariaDB database server. [root@centos001 ~]#

设置开机启动

systemctl enable mariadb

具体操作如下

[root@centos001 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

接下来进行MariaDB的相关简单配置,首先是设置密码,会提示先输入密码

mysql_secure_installation

回车

 

 

[root@centos001 ~]# 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] ^[[A^
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!
[root@centos001 ~]#

Enter current password for root (enter for none):<–初次运行直接回车

设置密码

Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码

Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车

Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车,

Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车

Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车

初始化MariaDB完成,接下来测试登录

[root@centos001 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.60-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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]>

完成。

二、配置MariaDB

查看安装

[root@centos001 ~]#  rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
mariadb-5.5.60-1.el7_5.x86_64
mariadb-server-5.5.60-1.el7_5.x86_64

修改配置文件,设置root用户远程登录的权限,最后设置成功之后,刷新授权

[root@centos001 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.60-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)]> select host from user where user='root';
ERROR 1046 (3D000): No database selected
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> select host from user where user='root';
+-----------+
| host |
+-----------+
| 127.0.0.1 |
| ::1 |
| centos001 |
| localhost |
+-----------+
4 rows in set (0.00 sec)

MariaDB [mysql]> update user set host = '%' where user = 'root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
MariaDB [mysql]> select host from user where user='root';
+-----------+
| host |
+-----------+
| % |
| 127.0.0.1 |
| ::1 |
| centos001 |
+-----------+
4 rows in set (0.00 sec)

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

 注意:没有启动root远程权限的创建新用户,开启权限。

$ mysql_secure_installation

Disallow root login remotely? [Y/n] 

 

创建一个新的账号,变授权刷新授权

MariaDB [mysql]> insert into mysql.user(Host,User,Password) values('localhost','guest',password('123456'));
Query OK, 1 row affected, 4 warnings (0.00 sec)
MariaDB [mysql]> grant all privileges on xspeeder.* to guest@"%" identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> select host,user from user ;
+-----------+-------+
| host | user |
+-----------+-------+
| % | guest |
| % | root |
| 127.0.0.1 | root |
| ::1 | root |
| centos001 | root |
| localhost | guest |
+-----------+-------+
6 rows in set (0.00 sec)

MariaDB [mysql]> Ctrl-C -- exit!
Aborted

 

 

 

我这里所有配置都默认安装,开启远程登陆权限,使用root用户操作数据就可以了,对数据编码等设置都可以使用root 权限操作的。

 

 

相关参考文章:

https://tableplus.io/blog/2018/09/mariadb-how-to-create-new-user-and-grant-privileges.html

转载于:https://www.cnblogs.com/sopcce/p/Install-MariaDB-on-CentOS-7-and-build-Mysql-services.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值