[数据库] MariaDB安装及使用

一、安装MariaDB

1.使用官方源安装marisdb

如果使用阿里云的源,目前的版本号为5.5.64。如果想安装最新的10.x版本,则需要使用MariaDB的官方源。

1)配置官方源:

在/etc/yum.repos.d/下创建MariaDB.repo:

[root@centos-db ~]# cd /etc/yum.repos.d/
[root@centos-db yum.repos.d]# touch MariaDB.repo
[root@centos-db yum.repos.d]# vi MariaDB.repo

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/PRM-GPG-KEY-MariaDB
gpgcheck=1

执行命令:

[root@centos-db yum.repos.d]# yum clean all
[root@centos-db yum.repos.d]# yum makecache

2)安装MariaDB-server和MariaDB-client

配置好MariaDB官方源以后,使用以下命令安装:

[root@centos-db yum.repos.d]# yum install MariaDB-server MariaDB-client

可以看到具体的版本号:

============================================================================================================================================================================================================================================
 Package                                                         Arch                                           Version                                                               Repository                                       Size
============================================================================================================================================================================================================================================
Installing:
 MariaDB-client                                                  x86_64                                         10.1.44-1.el7.centos                                                  mariadb                                          10 M
 MariaDB-server                                                  x86_64                                         10.1.44-1.el7.centos                                                  mariadb                                          24 M

2.使用阿里源安装mariadb

如果官方源速度慢,并且我们对新版本没有特别要求,则可以使用阿里源进行安装。

1)删除官方源的repo文件

[root@centos-db yum.repos.d]# cd /etc/yum.repos.d/
[root@centos-db yum.repos.d]# mv MariaDB.repo MariaDB.repo.bk

2)清理缓存

[root@centos-db yum.repos.d]# yum clean all

3)安装mariadb

[root@centos-db yum.repos.d]# yum install mariadb-server mariadb

注意,使用阿里源安装时,名字和官方的不一样,官方的名字是MariaDB-server、MariaDB-client。而阿里源为mariadb-server、mariadb。注意区分大小写和名称。

3.启动MariaDB

使用官方源或阿里源安装好mariadb后,使用以下命令来管理MariaDB服务:

systemctl status mariadb  # 查看状态
systemctl start mariadb  # 启动
systemctl stop mariadb  # 停止
systemctl restart mariadb  # 重启
systemctl enable mariadb  # 设置开机启动
systemctl disable mariadb  # 取消开机启动

启动mariadb:

[root@centos-db yum.repos.d]# systemctl status mariadb
鈼[0m mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-01-29 13:40:59 CST; 2s ago
  Process: 1703 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1615 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 1702 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           鈹溾攢1702 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           鈹斺攢1864 /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.pid --socket=/var/lib/mysql/mysql.sock

Jan 29 13:40:57 centos-db mariadb-prepare-db-dir[1615]: MySQL manual for more instructions.
Jan 29 13:40:57 centos-db mariadb-prepare-db-dir[1615]: Please report any problems at http://mariadb.org/jira
Jan 29 13:40:57 centos-db mariadb-prepare-db-dir[1615]: The latest information about MariaDB is available at http://mariadb.org/.
Jan 29 13:40:57 centos-db mariadb-prepare-db-dir[1615]: You can find additional information about the MySQL part at:
Jan 29 13:40:57 centos-db mariadb-prepare-db-dir[1615]: http://dev.mysql.com
Jan 29 13:40:57 centos-db mariadb-prepare-db-dir[1615]: Consider joining MariaDB's strong and vibrant community:
Jan 29 13:40:57 centos-db mariadb-prepare-db-dir[1615]: https://mariadb.org/get-involved/
Jan 29 13:40:57 centos-db mysqld_safe[1702]: 200129 13:40:57 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Jan 29 13:40:57 centos-db mysqld_safe[1702]: 200129 13:40:57 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Jan 29 13:40:59 centos-db systemd[1]: Started MariaDB database server.

二、初始化

使用命令:

mysql_secure_installation
[root@centos-db yum.repos.d]# 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):   # 这里直接enter就可以了
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   # 1.是否设置root密码,选择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  # 2.是否删除匿名账户,因为不安全,选择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] y  # 3.是否允许root用户远程登录,因为要在windows机器上去开发,所以选择Y
 ... Success!

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] y  # 4.删除测试数据库,选择Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

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

Reload privilege tables now? [Y/n] y  # 5.是否立即刷新权限表,选择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!

三、登录和使用mysql

1.登录mysql

[root@centos-db yum.repos.d]# mysql -uroot -p
Enter password:   # 输入root密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.64-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.修改服务器字符编码

1)查看服务器字符编码

MariaDB [(none)]> \s
--------------
mysql  Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:          11
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server:                 MariaDB
Server version:         5.5.64-MariaDB MariaDB Server
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 10 min 24 sec

Threads: 1  Questions: 30  Slow queries: 0  Opens: 1  Flush tables: 2  Open tables: 27  Queries per second avg: 0.048
--------------

可以看到,mysql服务器字符编码为latin1,无法显示中文。并且在这种字符编码下,创建的数据库和表都不能插入和显示中文。

2)修改配置文件

[root@centos-db etc]# vi my.cnf  # 修改/etc/my.cnf配置文件

[mysqld]
character-set-server=utf8

datadir=/var/lib/mysql
socket=/var/lib/mysql/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=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

添加character-set-server=utf8配置。

3)重启mariadb服务

systemctl restart mariadb

4)再次查看字符编码

MariaDB [(none)]> \s
--------------
mysql  Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:          2
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server:                 MariaDB
Server version:         5.5.64-MariaDB MariaDB Server
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 14 sec

Threads: 1  Questions: 4  Slow queries: 0  Opens: 0  Flush tables: 2  Open tables: 26  Queries per second avg: 0.285
--------------

3.创建数据库和表

1)查看所有数据库

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

2)创建数据库

MariaDB [(none)]> create database mydb;
Query OK, 1 row affected (0.00 sec)

查看数据库创建过程:

MariaDB [mydb]> show create database mydb;
+----------+---------------------------------------------------------------+
| Database | Create Database                                               |
+----------+---------------------------------------------------------------+
| mydb     | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)

3)使用某个数据库

MariaDB [(none)]> use mydb;
Database changed
MariaDB [mydb]> 

4)查看所有表

MariaDB [mydb]> show tables;
Empty set (0.00 sec)  # 目前没有任何表

5)创建一张表

MariaDB [mydb]> create table user_info (id int,name char(50),age int);
Query OK, 0 rows affected (0.00 sec)
MariaDB [mydb]> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| user_info      |
+----------------+
1 row in set (0.00 sec)

查看表的创建过程:

MariaDB [mydb]> show create table user_info;
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table     | Create Table                                                                                                                                              |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_info | CREATE TABLE `user_info` (
  `id` int(11) DEFAULT NULL,
  `name` char(50) DEFAULT NULL,
  `age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

4.表操作

1)插入一条数据

MariaDB [mydb]> insert into user_info (id,name,age) values (1,"张三",32);
MariaDB [mydb]> select * from user_info;
+------+--------+------+
| id   | name   | age  |
+------+--------+------+
|    1 | 张三   |   32 |
+------+--------+------+
1 row in set (0.00 sec)

2)查询数据

MariaDB [mydb]> select * from user_info;
+------+--------+------+
| id   | name   | age  |
+------+--------+------+
|    1 | 张三   |   32 |
|    2 | 李四   |   22 |
+------+--------+------+
2 rows in set (0.00 sec)
MariaDB [mydb]> select name,age from user_info where id=1; 
+--------+------+
| name   | age  |
+--------+------+
| 张三   |   32 |
+--------+------+
1 row in set (0.00 sec)

3)删除一条数据

MariaDB [mydb]> delete from user_info where id=1;
Query OK, 1 row affected (0.00 sec)
MariaDB [mydb]> select * from user_info;
+------+--------+------+
| id   | name   | age  |
+------+--------+------+
|    2 | 李四   |   22 |
+------+--------+------+

4)查看表结构

MariaDB [mydb]> desc user_info;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| name  | char(50) | YES  |     | NULL    |       |
| age   | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

5.管理操作

1)修改用户密码

MariaDB [mydb]> set password = PASSWORD('123456');
Query OK, 0 rows affected (0.00 sec)

2)创建用户

MariaDB [(none)]> create user leokale@'%' identified by '11111111';  # '%'表示用户'leokale'在所有的主机地址都可以登录,使用密码是'11111111'。
Query OK, 0 rows affected (0.00 sec)

刚创建好的用户权限很低,无法创建数据库。

3)查看用户表

[root@centos-db etc]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.64-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 |
| mydb               |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

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]> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

查看用户表中的信息:

MariaDB [mysql]> select host,user,password from user;
+-----------+---------+-------------------------------------------+
| host      | user    | password                                  |
+-----------+---------+-------------------------------------------+
| localhost | root    | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 127.0.0.1 | root    | *4D77515AB2D393245315E18C62709EC959669A89 |
| ::1       | root    | *4D77515AB2D393245315E18C62709EC959669A89 |
| %         | leokale | *E9D057131C22A0D76B4AAD2C61655BDFA706E637 |
+-----------+---------+-------------------------------------------+
4 rows in set (0.00 sec)

可以看到我们创建的leokale用户。

猜你喜欢

转载自www.cnblogs.com/leokale-zz/p/12239396.html