Ubuntu Mysql安装教程

  • 1、安装

    sudo apt install mysql-server
    
  • 2、验证MySQL服务器状态

    sudo systemctl status mysql
    
  • 3、密码和配置设置

    root@wy:/home/wy/TinyWebServer-master# sudo mysql_secure_installation
    
    Securing the MySQL server deployment.
    
    Connecting to MySQL using a blank password.
    
    VALIDATE PASSWORD COMPONENT can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD component?
    
    Press y|Y for Yes, any other key for No: y
    
    There are three levels of password validation policy:
    
    LOW    Length >= 8
    MEDIUM Length >= 8, numeric, mixed case, and special characters
    STRONG Length >= 8, numeric, mixed case, special characters and dictionary                                                                                                                                                                                                                                 file
    
    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 12345678
    Please set the password for root here.
    
    New password:
    
    Re-enter new password:
    
    Estimated strength of the password: 50
    Do you wish to continue with the password provided?(Press y|Y for Yes, any othe                                                                                                                                                                                                               r key for No) : y
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : n
    
     ... skipping.
    By default, MySQL 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? (Press y|Y for Yes, any other key for No                                                                                                                                                                                                               ) : 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? (Press y|Y for Yes, any other key for No) : y
    Success.
    
    All done!
    
    
  • 4、登陆

    mysql -u root -p
    
  • 5、卸载

    删除mysql的数据文件
    sudo rm /var/lib/mysql/ -R
    
    删除mysql的配置文件
    sudo rm /etc/mysql/ -R
    
    自动卸载mysql(包括server和client)
    sudo apt-get autoremove mysql* --purge
    sudo apt-get remove apparmor
    
    然后在终端中查看MySQL的依赖项:dpkg --list|grep mysql
    
  • 6、创建用户

    命令:
    CREATE USER ‘username’@’host’ IDENTIFIED BY ‘password’;
    说明:
    username:你将创建的用户名
    host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
    password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
    例子:
    CREATE USER ‘hello’@’localhost’ IDENTIFIED BY ‘123456’;
    CREATE USER ‘hello’@’192.168.1.101_’ IDENDIFIED BY ‘123456’;
    CREATE USER ‘hello’@’%’ IDENTIFIED BY ‘123456’;
    CREATE USER ‘hello’@’%’ IDENTIFIED BY ”;
    CREATE USER ‘hello’@’%’;
    
  • 7、查看用户

    SELECT User,Host FROM mysql.user;
    
  • 8、创建数据库、表、插入数据

    // 建立yourdb库
    create database yourdb;
    
    // 创建user表
    USE yourdb;
    CREATE TABLE user(
        username char(50) NULL,
        passwd char(50) NULL
    )ENGINE=InnoDB;
    
    // 添加数据
    INSERT INTO user(username, passwd) VALUES('name', 'passwd');
    
  • 9、编译报错:fatal error: mysql/mysql.h: No such file or directory

    sudo apt-get install libmysqlclient-dev
    

你可能感兴趣的:(SQL,sql)