美文网首页
树莓派安装MySQL

树莓派安装MySQL

作者: hellity | 来源:发表于2022-05-15 00:27 被阅读0次
    安装:
    sudo apt-get install mariadb-server
    
    安装后无密码直接登录:
    sudo mysql # sudo 必填 否则登录不上
    
    初始化(可设置 root 密码):
    sudo mysql_secure_installation # sudo 必填 否则出错。会询问你是否删除或者保留测试用数据库和用户
    
    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] y
     ... 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!
    
    一. 创建用户

    命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password';

    说明:
    username:你将创建的用户名
    host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
    password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
    例子:

    CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
    CREATE USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456';
    CREATE USER 'pig'@'%' IDENTIFIED BY '123456';
    CREATE USER 'pig'@'%' IDENTIFIED BY '';
    CREATE USER 'pig'@'%';
    
    三.设置与更改用户密码

    命令:SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
    如果是当前登陆用户用:SET PASSWORD = PASSWORD("newpassword");
    例子:SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");

    查看当前的数据库:show databases;
    创建数据库:create database MyDB_one;
    导入sql文件:mysql> source /opt/test.sql;
    查看表结构:DESCRIBE 表名; 或者desc 表名;
    增加字段(列):alter table purchase add warehoused int(2);
    修改表数据:

    update purchase set warehoused='1' where purchase_id=22;
    ('update purchase set warehoused={0} where purchase_id={1}').format('1',ids) # flask 中sql语句的写法
    

    删除表、表数据的方式:

    1. 当你不再需要该表时, 用 drop; drop table 表名;
    2. 当你仍要保留该表,但要删除所有数据表记录时, 用 truncate; truncate table 表名;
    3. 当你要删除部分记录或者有可能会后悔的话, 用 delete。delete from 表名 where id='1'; 或者 delete from 表名;

    Sqlalchemy filter与filter_by查询语法

    Mysql查询、修改

    创建一个表

    DROP TABLE IF EXISTS `category`;
    CREATE TABLE `category`  (
      `category_id` int(11) NOT NULL AUTO_INCREMENT,
      `category_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      PRIMARY KEY (`category_id`) USING BTREE,
      UNIQUE INDEX `category_name`(`category_name`) USING BTREE
    ) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    

    相关文章

      网友评论

          本文标题:树莓派安装MySQL

          本文链接:https://www.haomeiwen.com/subject/kudvurtx.html