Ubuntu下安装MariaDB

环境&版本
MairaDB 10.1.43
Ubuntu 20

更新仓库(非必要)

这一步其实不是必须的, 而是MariaDB推荐的. Ubuntu的Repository里的稳定版可能会比较老, 不是社区最新的稳定版. 因此MairaDB提供了更新仓库的脚步.
执行

curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash

这个脚本会设置3个不同的仓库到配置文件中, 包括:

MariaDB Repository
MariaDB MaxScale Repository
MariaDB Tools Repository
安装服务

sudo apt install mariadb-server

这里我曾经使用sudo apt install mariadb-server-10.4进行安装, 然而后续操作时出现了问题, 导致卸载重装了

启动服务

启动mariadb

sudo systemctl start mariadb

允许mariadb自启动

sudo systemctl enable mariadb

初始化配置

这一步很重要

$ sudo 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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): //因为之前没有设置密码,所以直接回车
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n //不使用unix_socket authentication
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y //设置root密码
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] y //禁止远程登录root
 ... 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 //删除测试数据库
 - 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 //刷新权限
 ... 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!

启动&重启关闭

查看状态

sudo systemctl status mysql

启动

sudo systemctl start mysql

重启

sudo systemctl restart mysql

关闭

sudo systemctl stop mysql

配置
登录
sudo mysql -u root -p #输入root 用户密码

创建远程登录用户 ,

格式:create user ‘用户名’@’%’ identified by ‘密码’;

> create user 'us'@'%' identified by '123';

授权(如果权限不足)

> GRANT ALL PRIVILEGES ON *.* TO 'us'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
> FLUSH PRIVILEGES;

不要开放root 远程登录权限

查看监听

netstat -an | grep 3306

tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN

如果显示 监听端口 是127.0.0.1:3306 那么其他计算机无法连接

修改配置

vi /etc/mysql/mariadb.conf.d/50-server.cnf#修改结束记得用重启指令重启mysql服务

Instead of skip-networking the default is now to listen only on
localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1 #注释掉这一行

重启后显示

tcp6 0 0 :::3306 ::? LISTEN

此时就可以远程访问mariadb了

远程访问
直接登录
mysql -h localhost -u us -p 123 #输入us 用户密码123
用客户端工具登录
sqlyog navicat hs
异常处理
Access denied for user ‘root’@‘localhost’

$ sudo systemctl stop mariadb # 停止服务
$ sudo mysqld_safe --skip-grant-tables & # 进入安全模式,并设置为后台进程
$ mysql -u root   # 登陆mysql

> select Host,User,plugin from mysql.user where User='root';  # 查询用户
> update mysql.user set plugin='mysql_native_password';  #重置加密模式
> update mysql.user set password=PASSWORD("newpassword") where User='root';  #重置密码
> flush privileges;  #刷新权限信息
> exit
$ sudo kill -9 $(pgrep mysql) # 杀掉进程
$ sudo service mariadb start # 重新启动服务
$ mysql -u root -p #登陆mysql, 安装unix_soket
$ install plugin unix_socket soname ‘auth_socket’;

猜你喜欢

转载自blog.csdn.net/BigData_Mining/article/details/107314383
今日推荐