//卸載mysql
$ brew remove mysql
//安裝mysql
$ brew install mysql
//啟動(dòng)mysql服務(wù)
$ mysql.server start
//重置root密碼
$ mysqld_safe --skip-grant-tables
//無(wú)密碼登陸mysql
$ mysql -u root
2.mysql管理
//設(shè)置或更改密碼
mysql> SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
//創(chuàng)建用戶
mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
//授權(quán)用戶(所有權(quán)限all)
mysql> GRANT all privileges ON databasename.* TO 'username'@'localhost'
//創(chuàng)建用戶同時(shí)授權(quán)
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON 數(shù)據(jù)庫(kù)名.* TO 'username'@'localhost' IDENTIFIED BY 'password';
//退出mysql
mysql> exit
3.mysql數(shù)據(jù)庫(kù)操作
//登陸mysql
$ mysql -h localhost -u kevin -p
Enter password:
//顯示所有數(shù)據(jù)庫(kù)
mysql> show databases;
//創(chuàng)建數(shù)據(jù)庫(kù)
mysql> create database dbname;
//刪除數(shù)據(jù)庫(kù)
mysql> drop database dbname;
//刪除數(shù)據(jù)庫(kù)如果存在
mysql> drop database if exists dbname;
//使用數(shù)據(jù)庫(kù)
mysql> use run_db;
//當(dāng)前數(shù)據(jù)庫(kù)
mysql> select database();
4.mysql表操作
//顯示所有表
mysql> show tables;
//表創(chuàng)建
mysql> create table tname (name varchar(20), sex char(1));
//查看表結(jié)構(gòu)
mysql> describe tname;
//刪除表如果存在
mysql> drop table tname;
mysql索引
mysql其他操作
//當(dāng)前時(shí)間
mysql> select now();
//顯示年月日
mysql> select year(current_date);//年
mysql> select month(current_date);//月
mysql> select dayofmonth(current_date);//日
//顯示字符串
mysql> select "Hello world!";
//當(dāng)計(jì)算器用
mysql> select ((4 * 4) / 10 ) + 25;
//串接字符串
mysql> select CONCAT(f_name, " ", l_name) AS Name from employee_data where title = 'Marketing Executive';
注:這里用到CONCAT()函數(shù)己肮,用來(lái)把字符串串接起來(lái)雷恃。另外,我們還用到以前學(xué)到的AS給結(jié)果列'CONCAT(f_name, " ", l_name)'起了個(gè)假名