1. 卸载系统默认安装MariaDB
1.1. 列出所有被安装的rpm package
[root@localhost yum.repos.d]# rpm -qa | grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
1.2. 强制卸载所有列出的包
[root@localhost yum.repos.d]# rpm -e --nodeps mariadb-libs
错误:未安装软件包 mariadb-libs
2. 安装MariaDB
2.1. MariaDB.repo 文件
2.1.1 创建MariaDB.repo 文件
[root@localhost yum.repos.d]# vi /etc/yum.repos.d/MariaDB.repo
2.1.2 将以下内容粘贴到MariaDB.repo文件中(用于指定mariadb安装版本)
[mariadb]
name=MariaDB
baseurl=http://yum.mariadb.org/10.2.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
2.2. 安装server和client
[root@localhost yum.repos.d]# yum install MariaDB-server MariaDB-client
3.安装成功后启动Mariedb server
systemctl start mariadb #启动服务
systemctl enable mariadb #设置开机启动
systemctl restart mariadb #重新启动
systemctl stop mariadb.service #停止MariaDB
4.登录到数据库
用mysql -uroot命令登录到MariaDB,此时root账户的密码为空。
5.进行MariaDB的相关简单配置,使用mysql_secure_installation命令进行配置。
mysql_secure_installation
首先是设置密码,会提示先输入密码
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完成,接下来测试登录
mysql -uroot -ppassword
完成。
6.配置MariaDB的字符集
查看/etc/my.cnf文件内容,其中包含一句!includedir /etc/my.cnf.d 说明在该配置文件中引入/etc/my.cnf.d 目录下的配置文件。
1)使用vi server.cnf命令编辑server.cnf文件,在[mysqld]标签下添加
init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake
如果/etc/my.cnf.d 目录下无server.cnf文件,则直接在/etc/my.cnf文件的[mysqld]标签下添加以上内容。
2)文件/etc/my.cnf.d/client.cnf
vi /etc/my.cnf.d/client.cnf
在[client]中添加
default-character-set=utf8
3)文件/etc/my.cnf.d/mysql-clients.cnf
vi /etc/my.cnf.d/mysql-clients.cnf
在[mysql]中添加
default-character-set=utf8
全部配置完成,重启mariadb
systemctl restart mariadb
之后进入MariaDB查看字符集
mysql> show variables like "%character%";show variables like "%collation%";
显示为
+--------------------------+----------------------------+ | 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) +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec)
字符集配置完成。
7. 添加用户,设置权限
创建用户命令
mysql>create user username@localhost identified by 'password';
直接创建用户并授权的命令
mysql>grant all on *.* to username@localhost indentified by 'password';
授予外网登陆权限
mysql>grant all privileges on *.* to username@'%' identified by 'password';
授予权限并且可以授权
mysql>grant all privileges on *.* to username@'hostname' identified by 'password' with grant option;
MariaDB [mysql]> select host,user,password from user; +-----------------------+-------+------------------------+ | host | user | password | +-----------------------+-------+------------------------+ | localhost | root | *E87F9354F7E889A65E... | | localhost.localdomain | root | *E87F9354F7E889A65E... | | 127.0.0.1 | root | *E87F9354F7E889A65E... | | ::1 | root | *E87F9354F7E889A65E... | | localhost | | | | localhost.localdomain | | | +-----------------------+-------+------------------------+ 7 rows in set (0.00 sec)
查询各Schema和Table占用的空间:
MariaDB [information_schema]> use information_schema;
MariaDB [information_schema]> select table_schema,round(sum(DATA_LENGTH/1024/1024),2) as datasize from tables group by table_schema;
+--------------------+----------+
| table_schema | datasize |
+--------------------+----------+
| common | 0.05 |
| information_schema | 0.09 |
| mysql | 9.11 |
| nemo | 103.23 |
| river | 3.78 |
+--------------------+----------+
5 rows in set (2.026 sec)
MariaDB [information_schema]> select table_name,concat(round(sum(data_length/1024/1024),2),'MB') as datasize from tables where table_schema='nemo' group by table_name;
+---------------------------------+----------+
| table_name | datasize |
+---------------------------------+----------+
| actions | 0.05MB |
| addresses | 0.02MB |
| addressfieldattributes | 0.02MB |
| addressprops | 0.02MB |
| composedtypes | 1.52MB |
| composedtypeslp | 1.52MB |
| comptypegrp2comptype | 0.05MB |
| config | 0.02MB |
| itemcockpittemplrels | 0.02MB |
| itemsynctimestamps | 2.52MB |
| keyfeature | 0.02MB |
| keyfeaturelp | 0.02MB |
| keyvaluemap | 2.52MB |
| keywords | 0.02MB |
| keywordsuggestionrule | 0.02MB |
+---------------------------------+----------+
15 rows in set (0.544 sec)
简单的用户和权限配置基本就这样了。
其中只授予部分权限把 其中 all privileges或者all改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。
Linux系统教程:如何检查MariaDB服务端版本 http://www.linuxidc.com/Linux/2015-08/122382.htm
MariaDB Proxy读写分离的实现 http://www.linuxidc.com/Linux/2014-05/101306.htm
Linux下编译安装配置MariaDB数据库的方法 http://www.linuxidc.com/Linux/2014-11/109049.htm
CentOS系统使用yum安装MariaDB数据库 http://www.linuxidc.com/Linux/2014-11/109048.htm
安装MariaDB与MySQL并存 http://www.linuxidc.com/Linux/2014-11/109047.htm
忘记root用户名和密码
首先用 killall -TERM mysqld 向mysqld server 发送kill命令关掉mysqld server(不是 kill -9),你必须是UNIX的root用户或者是你所运行的SERVER上的同等用户,才能执行这个操作
然后 /usr/bin/mysqld_safe --skip-grant-tables --skip-networking &
登录 : mysql -p或者使用mysql无密码登录
>use mysql
>update user set password=password("new_pass") where user="root";
>flush privileges;
exit;
修改完成之后重启数据库,即可用修改好 root 密码登录 .