#安装mariadb数据库
apt install mariadb-server启动mariadb数据库
sudo systemctl status mariadb
sudo systemctl status mariadb
初始设置
mysql_secure_installation
mysql_secure_installation
Enter current password for root (enter for none):(设置数据库root密码)
Switch to unix_socket authentication [Y/n] y
Change the root password? [Y/n] n
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
命令行登录(输入数据库root密码登录)
mysql -uroot -p
输入密码成功登录后,建立一个新的用户,并给予最大的权限:
CREATE USER '用户名'@'%' IDENTIFIED BY '密码';
GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%' WITH GRANT OPTION;
创建数据库示例:
登录:
mysql -uroot -p
新建utf8mb4 数据库:
create database testdb default character set utf8mb4 collate utf8mb4_bin;
或
CREATE DATABASE IF NOT EXISTS test CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
删除数据库:
DROP DATABASE testdb;刷新权限:
FLUSH PRIVILEGES;
退出:
quit;
mariadb数据库的相关启动命令
systemctl start mariadb #启动MariaDB
systemctl stop mariadb #停止MariaDB
systemctl restart mariadb #重启MariaDB
systemctl enable mariadb #设置开机启动
mysql -u root -p #使用密码进入root数据库
DROP USER dbuser; #删除用户dbuser
数据库的web管理
安装phpMyAdmin实现
打开网站目录
cd /var/www/html
获取程序包
wget https://files.phpmyadmin.net/phpMyAdmin/5.2.1/phpMyAdmin-5.2.1-all-languages.zip
解压到指定目录
unzip -d /var/www/html/phpMyAdmin phpMyAdmin-5.2.1-all-languages.zip
可能会遇到的问题:
phpmyadmin上传到网站目录后登录提示phpmyadmin mysqli_real_connect(): (HY000/2002): No such file or directory的错误解决,
解决方法把phpmyadmin目录中的配置文件config.sample.inc.php改成config.inc.php
打开编辑config.inc.php
找到:
$cfg['Servers'][$i]['host'] = 'localhost';
改成:
$cfg['Servers'][$i]['host'] = '127.0.0.1';
再次刷新页面就不会出现mysqli_real_connect(): (HY000/2002): No such file or directory的错误提示了。
#卸载mariadb数据库
卸载:
sudo apt autoremove mariadb-server
清理相关配置:
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P mariadb-serve