Linux 安装配置MariaDB

1、安装

[root@dbServer ~]# yum install -y mariadb mariadb-server

已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
........ 
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                         1.8 MB/s |  20 MB  00:00:11     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                                       1/4
  正在安装    : perl-DBD-MySQL-4.023-5.el7.x86_64                                                                                                                        2/4
  正在安装    : 1:mariadb-5.5.52-1.el7.x86_64                                                                                                                            3/4
  正在安装    : 1:mariadb-server-5.5.52-1.el7.x86_64                                                                                                                     4/4
  验证中      : 1:mariadb-server-5.5.52-1.el7.x86_64                                                                                                                     1/4
  验证中      : perl-DBD-MySQL-4.023-5.el7.x86_64                                                                                                                        2/4
  验证中      : 1:mariadb-5.5.52-1.el7.x86_64                                                                                                                            3/4
  验证中      : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                                       4/4
已安装:
  mariadb.x86_64 1:5.5.52-1.el7                                                     mariadb-server.x86_64 1:5.5.52-1.el7                                                   
作为依赖被安装:
  mariadb-libs.x86_64 1:5.5.52-1.el7                                                   perl-DBD-MySQL.x86_64 0:4.023-5.el7                                                 
完毕!
[root@dbServer ~]#


2、配置

[root@dbServer ~]# systemctl start mariadb
[root@dbServer ~]# systemctl enable mariadb

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

[root@dbServer ~]# 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
 ... 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] 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!

3、字符集

[root@dbServer ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> show variables like "%character%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

字符集备注:

character_set_client:客户端请求数据的字符集。
character_set_connection:从客户端接收到数据,然后传输的字符集。
character_set_database:默认数据库的字符集,无论默认数据库如何改变,都是这个字符集;如果没有默认数据库,使character_set_server指定的字符集,此参数无需设置。
character_set_filesystem:把character_set_client转换character_set_filesystem,默认binary即可
character_set_results:结果集的字符集。
character_set_server:数据库服务器的默认字符集。
character_set_system:这个值总是utf8,不需要设置,存储系统元数据的字符集

MariaDB [(none)]> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)


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

MariaDB [(none)]> use jobHunter;
Database changed
MariaDB [jobHunter]> source /root/createtable.sql
Query OK, 0 rows affected (0.01 sec)


可能存在问题:

修改好了字符集,但是重新进入数据库时,编码丢失。

如下,也就是可能SET character_set_database = utf8;命令失效

MariaDB [(none)]> SET character_set_database = utf8;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> commit;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show variables like "%character%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

MariaDB [(none)]> exit
Bye
[root@hadron ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show variables like "%character%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)


如果出现上面问题,那只好去修改配置文件了,如下:

[root@dbServer /]#vi /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

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8


[root@dbServer /]# systemctl restart mariadb
[root@hadron /]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show variables like "%collation%";
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

MariaDB [(none)]>  show variables like "%character%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
MariaDB [(none)]>


 4、远程登录

本地可以登录数据库,但是远程不能登录访问。(192.168.1.120是上面Mariadb机器的IP

[root@nb0 ~]# mysql -h192.168.1.120 -uroot -p
Enter password:
ERROR 1130 (HY000): Host '192.168.1.160' is not allowed to connect to this MariaDB server
[root@nb0 ~]#

解决办法

(1)本地登录MySQL/MariaDB

[root@dbServer ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

(2)查询mysql.user

MariaDB [(none)]> select Host,User,Password from mysql.user;
+-----------+------+-------------------------------------------+
| Host      | User | Password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| node3     | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| 127.0.0.1 | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| ::1       | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+------+-------------------------------------------+
4 rows in set (0.00 sec)

(3)删除多余用户

如果存在用户名或密码为空值的记录,需要删除。(实际上在执行mysql_secure_installation时已经删除了多余用户

MariaDB [(none)]> delete from mysql.user where user='';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> delete from mysql.user where password='';
Query OK, 0 rows affected (0.00 sec)

(4)远程访问授权

MariaDB [(none)]> grant all on *.* to root@'%' identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)

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

MariaDB [(none)]>

(5)测试远程访问


[root@nb0 ~]# mysql -h192.168.1.120 -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>



5、远程登录MySQL/MariaDB数据库,指定端口号

执行命令:mysql -h192.168.3.160 -P3308 -uroot -p
默认端口号不是3306时,需要通过大P参数指定端口号

[root@hadron comm_api]# mysql -h192.168.3.160 -P3308 -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 186677
Server version: 5.6.35 MySQL Community Server (GPL)


Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mts                |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.14 sec)


MySQL [(none)]>



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Linux上离线安装MariaDB,您需要按照以下步骤操作: 1.下载MariaDB二进制文件:您可以从MariaDB官方网站下载适用于您的Linux发行版的二进制文件。 2.将二进制文件复制到Linux服务器:将下载的二进制文件复制到您的Linux服务器上。 3.解压缩二进制文件:使用tar命令解压缩二进制文件。 4.创建MariaDB数据目录:使用mkdir命令创建一个新的目录,用于存储MariaDB数据。 5.初始化MariaDB数据库:使用mysql_install_db命令初始化MariaDB数据库。 6.启动MariaDB服务:使用systemctl命令启动MariaDB服务。 7.配置MariaDB:使用mysql_secure_installation命令配置MariaDB,包括设置root密码和删除测试数据库等。 8.测试MariaDB:使用mysql命令测试MariaDB是否正常工作。 以上是在Linux上离线安装MariaDB的基本步骤。请注意,具体步骤可能因Linux发行版而异。 ### 回答2: MariaDB是一个免费的、开源的关系型数据库管理系统,它是MySQL的一个分支。在Linux操作系统上,安装MariaDB有多种方法,本文将介绍如何离线安装MariaDB。 一、下载MariaDB安装包 在安装MariaDB之前,我们需要下载安装包。可以在官方网站(https://mariadb.org/download/)上下载最新的稳定版本的MariaDB的tar.gz压缩包。在下载页面中,有多个选项,可以根据自己的需要选择对应的版本。在本次安装中,我们选择了10.3.17版本的MariaDB。 二、解压安装包 下载完成后,我们进入下载目录,在终端中输入以下命令进行解压安装包: $ tar -zxvf mariadb-10.3.17.tar.gz 此命令将解压后的文件放置在当前目录下的“mariadb-10.3.17”文件夹中。 三、创建用户与组 接下来,我们需要创建一个MariaDB用户和一个MariaDB组,用来运行MariaDB服务。在终端中输入以下命令: $ sudo groupadd mysql $ sudo useradd -g mysql mysql 四、安装依赖库 在离线安装中,我们需要手动安装MariaDB所依赖的库。在终端中输入以下命令: $ sudo yum install libaio $ sudo yum install libaio-devel $ sudo yum install ncurses-devel 五、配置MariaDB 接下来,我们需要进入到MariaDB文件夹中,对其进行配置。在终端中输入以下命令: $ cd mariadb-10.3.17 $ sudo cmake . –DCMAKE_INSTALL_PREFIX=/usr/local/mysql –DMYSQL_DATADIR=/usr/local/mysql/data –DSYSCONFDIR=/etc –DWITH_INNOBASE_STORAGE_ENGINE=1 –DWITH_MYISAM_STORAGE_ENGINE=1 –DWITH_EMBEDDED_SERVER=1 –DENABLED_LOCAL_INFILE=1 注:其中,-DCMAKE_INSTALL_PREFIX表示安装目录,-DMYSQL_DATADIR表示数据库文件存放目录,-DSYSCONFDIR表示配置文件存放目录,以上路径可自行更改 六、编译并安装MariaDB 配置完成后,我们需要编译并安装MariaDB。在终端中输入以下命令: $ sudo make $ sudo make install 安装完成后,我们需要对MariaDB进行初始化。在终端中输入以下命令: $ sudo /usr/local/mysql/scripts/mysql_install_db –user=mysql –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data 接下来,我们需要将MariaDB安装目录添加到系统环境变量中。在终端中输入以下命令: $ sudo vi /etc/profile 在文件末尾加入以下代码: export PATH=$PATH:/usr/local/mysql/bin 保存并退出。在终端中输入以下命令使配置生效: $ source /etc/profile 七、启动MariaDB 现在,我们可以启动MariaDB服务,使用以下命令: $ sudo /usr/local/mysql/support-files/mysql.server start 使用以下命令查看MariaDB服务状态: $ sudo /usr/local/mysql/support-files/mysql.server status 八、测试连接 现在,我们可以测试连接到MariaDB。在终端中输入以下命令: $ mysql -u root -p 此命令将提示输入root用户的密码,输入密码后,将会进入MariaDB的命令行模式。 至此,MariaDB的离线安装完成。通过以上步骤,我们可以在Linux系统中顺利地安装MariaDB,为后续的数据库操作提供基础支持。 ### 回答3: MariaDB是一个开源的关系型数据库管理系统,是MySQL项目的社区分支。相比于MySQL,MariaDB更加稳定,性能更优秀,使用也更加方便。本文将详细介绍如何在Linux系统中离线安装MariaDB。 1. 下载MariaDB 首先,需要下载MariaDB的二进制安装包。可以从官方网站 https://downloads.mariadb.org/ 下载安装包,选择适合自己系统的版本和对应的版本号,下载到本地。 2. 安装依赖包 在安装MariaDB之前,需要确保系统已经安装了以下依赖包: - zlib-devel: 用于压缩和解压缩数据的库。 - openssl-devel: 用于支持SSL连接的库。 - ncurses-devel: 用于支持终端界面的库。 如果没有安装上述依赖包,可以在命令行中使用以下命令进行安装: ``` yum install zlib-devel openssl-devel ncurses-devel -y ``` 3. 安装MariaDB 在下载了MariaDB的二进制安装包和安装了依赖包之后,可以进行MariaDB安装。在命令行中执行以下命令: ``` tar -xzvf mariadb-10.3.9-linux-x86_64.tar.gz cd mariadb-10.3.9-linux-x86_64/ ./bin/mysql_install_db --user=mysql --basedir=/opt/mariadb --datadir=/opt/mariadb/data ``` 执行完上述命令之后,会将MariaDB安装到/opt/mariadb目录下,并初始化数据目录/opt/mariadb/data。 4. 修改配置文件 MariaDB配置文件为my.cnf,需要根据实际需要进行修改。可以将my.cnf复制到/opt/mariadb/etc/目录下,并进行修改。 以下是一个简单的my.cnf配置文件: ``` [client] port = 3306 socket = /opt/mariadb/data/mysql.sock [mysqld] port = 3306 socket = /opt/mariadb/data/mysql.sock pid-file = /opt/mariadb/data/mysql.pid character-set-server = utf8mb4 collation-server = utf8mb4_general_ci datadir = /opt/mariadb/data log-error = /opt/mariadb/logs/mysqld.log log-bin = /opt/mariadb/logs/mariadb-bin expire_logs_days = 14 max_binlog_files = 100 max_connections = 300 query_cache_size = 256M innodb_buffer_pool_size = 2G innodb_flush_log_at_trx_commit = 2 innodb_flush_method = O_DIRECT [safe_mysqld] err-log = /opt/mariadb/logs/mysqld_safe.log ``` 5. 启动MariaDB 在完成了依赖包安装MariaDB安装和my.cnf配置之后,可以启动MariaDB。在命令行中执行以下命令: ``` /opt/mariadb/bin/mysqld_safe --defaults-file=/opt/mariadb/etc/my.cnf & ``` 启动成功后,可以通过命令行或其他客户端工具连接到MariaDB。默认的用户名为root,密码为空。 6. 添加MariaDB到系统服务 为了方便启动和关闭MariaDB,可以将MariaDB添加到系统服务中。可以创建一个mariadb.service文件,将其保存到/etc/systemd/system/目录下。 以下是mariadb.service文件的内容: ``` [Unit] Description=MariaDB database server After=network.target [Service] Type=simple User=root WorkingDirectory=/opt/mariadb PIDFile=/opt/mariadb/data/mysql.pid ExecStart=/opt/mariadb/bin/mysqld_start ExecStop=/opt/mariadb/bin/mysqld_stop [Install] WantedBy=multi-user.target ``` 保存后,执行以下命令使mariadb.service生效: ``` systemctl daemon-reload systemctl enable mariadb.service ``` 现在,可以使用以下命令管理MariaDB: ``` systemctl start mariadb.service # 启动MariaDB服务 systemctl stop mariadb.service # 停止MariaDB服务 systemctl status mariadb.service # 查看MariaDB服务状态 systemctl restart mariadb.service # 重启MariaDB服务 ``` 至此,Linux系统中离线安装MariaDB的过程就介绍完毕了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值