Linux 安装配置MariaDB

2022-05-06 17:10:35 浏览数 (1)

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 人点赞