MySQL的安装与配置及登陆与退出

1、安装与配置MySQL:

/usr/bin:客户端程序和脚本

/usr/sbin:mysql 服务器

/var/lib/mysql:日志文件、数据库文件

/usr/share/mysql:错误消息和字符集文件

/etc/my.cnf:配置文件

2、修改数据库的密码:

登陆数据库:

[[email protected]_0_13_centos ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

选择mysql数据库:

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

修改root密码为123456:

3、启动、终止mysql:

终止mysql之后,就不能登陆mysql了:

[[email protected]_0_13_centos ~]# service mysqld stop
Redirecting to /bin/systemctl stop mysqld.service
[[email protected]_0_13_centos ~]# mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

重启mysql,然后就可以登陆mysql了:

[[email protected]_0_13_centos ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
[[email protected]_0_13_centos ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

4、使用\s查看数据库的状态,字符集编码方式不是utf8会出现乱码。

mysql> \s;
--------------
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper

Connection id:          2
Current database:
Current user:           [email protected]
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.23 MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 2 min 32 sec

Threads: 1  Questions: 5  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.032
--------------

ERROR: 
No query specified

5、将编码方式改成utf8,否则会出现乱码现象:

找到my.cnf配置文件:/etc

MySQL的安装与配置及登陆与退出

vim打开配置文件,设置字符集:

客户端字符集[mysql]:default-character-set=utf8;

服务器端字符集[mysqld]:character-set-server=utf8;

MySQL的安装与配置及登陆与退出

然后终止和重启mysql,再来看一下状态,可以使用\s或者status:

mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper

Connection id:          2
Current database:
Current user:           [email protected]
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.23 MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 38 sec

Threads: 1  Questions: 5  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.131
--------------

现在编码方式都变成utf8了,就不会出现乱码现象了。

6、登陆/退出MySQL:

登陆方式:

A、使用用户名和密码:

[[email protected]_0_13_centos ~]# mysql -uroot -p

 B、使用IP地址登陆:

#mysql -hIP地址 -uroot -p;

C、使用IP地址和端口号:

#mysql -hIP地址 -P端口号 -uroot -p

退出的三种方式:

exit;quit;\q

7、输出mysql版本信息并且退出:

[[email protected]_0_13_centos ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper