centOS7安装mysql及使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36014192/article/details/78811042

$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

安装mysql时网上搜索了很多种方法,总是出现各种问题,总结了一种比较简单的,能用,但是不知道靠不靠谱。

提示:我使用root登录的系统,其他账户不确定能不能用。

1.下载mysql的repo源

$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

2.安装mysql-community-release-el7-5.noarch.rpm包

$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

3.安装mysql

$ sudo yum install mysql-server


根据提示安装就可以了,不过安装完成后没有密码,需要重置密码


4.重置mysql密码


$ mysql -u root


登录时有可能报这样的错:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2),原因是/var/lib/mysql的访问权限问题。下面的命令把/var/lib/mysql的拥有者改为当前用户:


$ sudo chown -R root:root /var/lib/mysql


重启mysql服务


$ service mysqld restart


接下来登录重置密码:


$ mysql -u root 
mysql > use mysql;
mysql > update user set password=password('123456') where user='root';

mysql > exit;

mysqlf服务安装完成。下面介绍初次进入数据库及使用。


1打开mysqld


service mysqld start

2.登录

mysql -u -p

3.可以为所欲为了

1.6.2 常用命令

创建一个数据库:

mysql> create database [databasename];

列出所有数据库:

mysql> show databases;

切换到一个数据库:

mysql> use [db name];

显示一个数据库的所有表:

mysql> show tables;

查看数据表的字段格式:

mysql> describe [table name];

删除一个数据库:

mysql> drop database [database name];

删除一个数据表:

mysql> drop table [table name];

显示一个数据表的所有数据:

mysql> SELECT * FROM [table name];

返回指定数据表的各列信息:

mysql> show columns from [table name];

使用值“whatever”过滤显示选定的某些行:

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

显示所有包含name为”Bob”和phone number为“3444444”的记录:

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';

显示所有不包含name为”Bob”和phone number为“3444444”的记录,并以phone_number字段排序:

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;

显示所有的name以字母“bob”开头和phone number为“3444444”的记录:

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';

显示name以字母“bob”开头和phone number为“3444444”的第1至第5条记录:

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;

使用正则表达式查找记录:使用“正则表达式二进制”强制区分大小写:此命令查找以a开头的任何记录:

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

返回唯一不同的记录:

mysql> SELECT DISTINCT [column name] FROM [table name];

以升序或降序显示选定的记录:

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

返回行数:

mysql> SELECT COUNT(*) FROM [table name];

统计指定列值的总和:

mysql> SELECT SUM(*) FROM [table name];

联结表:

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;

新建一个用户:以root登录:切换到mysql数据库,创建用户,刷新权限:

# mysql -u root -p

mysql> use mysql;

mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));

mysql> flush privileges;

从unix命令行更改用户密码:

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'

从mysql命令行更改用户密码:以root登录,设置密码,更新权限:

# /etc/init.d/mysql stop

# mysqld_safe --skip-grant-tables &

# mysql -u root

mysql> use mysql;

mysql> update user set password=PASSWORD("newrootpassword") where User='root';

mysql> flush privileges;

mysql> quit

# /etc/init.d/mysql stop

# /etc/init.d/mysql start

root密码为空时,设置root密码:

# mysqladmin -u root password newpassword

更新root密码:

# mysqladmin -u root -p oldpassword newpassword

允许用户“bob”从localhost以密码“passwd”连接服务器:以root登录,切换mysql数据库:设置权限,更新权限:

# mysql -u root -p

mysql> use mysql;

mysql> grant usage on *.* to bob@localhost identified by 'passwd';

mysql> flush privileges;

为数据库db设置权限:以root登录,切换到mysql数据库,授予权限,更新权限:

# mysql -u root -p

mysql> use mysql;

mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');

mysql> flush privileges;

或者

mysql> grant all privileges on databasename.* to username@localhost;

mysql> flush privileges;

更新已存在表的数据:

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';

删除表中[field name] = ‘whatever’的行:

mysql> DELETE from [table name] where [field name] = 'whatever';

更新数据库的权限/特权:

mysql> flush privileges;

删除列:

mysql> alter table [table name] drop column [column name];

新增列到db:

mysql> alter table [table name] add column [new column name] varchar (20);

更改列名:

mysql> alter table [table name] change [old column name] [new column name] varchar (50);

增加唯一的列:

mysql> alter table [table name] add unique ([column name]);

设置列值大点:

mysql> alter table [table name] modify [column name] VARCHAR(3);

删除唯一列:

mysql> alter table [table name] drop index [colmn name];

导入一个CSV文件到表:

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);

导出所有数据库到sql文件:

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

导出一个数据库:

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

从一个数据库导出一个表:

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

从sql文件还原数据库(数据表):

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

创建数据表例1:

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));

创建数据表例2:

mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');


展开阅读全文

没有更多推荐了,返回首页