Google Translate

2021年11月24日星期三

Mysql on Ubuntu 20.04

## installation

sudo apt install mysql-server

pip install mysql-server


## set up security

sudo mysql_secure_installation


## login

sudo mysql -u root


## create user

  • CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';


## Create mysql db


## Change mysql==8.0.27 password in Ubuntu 20.04.1

sudo /etc/init.d/mysql stop

(maybe): sudo mkdir /var/run/mysqld && sudo chown mysql /var/run/mysqld

sudo mysqld_safe --user=mysql --skip-grant-tables --skip-networking &

mysql -u root mysql

flush privileges;

ALTER USER  'root'@'localhost' IDENTIFIED BY 'Password=1234';

sudo killall -u mysql

sudo systemctl restart mysql.service

sudo mysql -u root -p


################## Clone DB ################

  1. Make a dump of your source database:

    mysqldump -uroot -p my_project -r my_project.sql
    

    Or, if you only want to dump the database's table structure (schema) without any contents:

    mysqldump -uroot -p my_project -r my_project.sql --no-data
    
  2. Open up a MySQL shell:

    mysql -uroot -p
    
  3. From the MySQL shell, create a new database and populate it with the dumped data:

    CREATE DATABASE my_project_copy;
    USE my_project_copy;
    SOURCE my_project.sql;
    
  4. Create a user and give it permissions to the new database:

    CREATE USER new_user IDENTIFIED BY 'some_password';
    GRANT ALL ON my_project_copy.* TO 'new_user'@'localhost' IDENTIFIED BY 'some_password';