day03-MySQL连接启动多实例

day03-MySQL连接启动多实例


加我微信:loveoracle11g(请备注:进mysql微信群)
拉你进MySQL高级DBA(技术开车群)微信群呕!!!

[root@mysql-node1 ~]# ss -tunlp | grep mysql
tcp    LISTEN     0      80       :::3306                 :::*                   users:(("mysqld",pid=5985,fd=31))

[root@mysql-node1 ~]# mysql -uroot -p -S /tmp/mysql.sock

mysql> grant all on *.* to root@'10.0.0.%' identified by '123' ;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> select user,host from mysql.user ;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | 10.0.0.%  |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

mysql> exit ;
Bye

1、MySQL连接工具-mysql命令使用
自带客户端命令
mysql命令常用参数:
-u  用户
-p  密码
-h  IP
-P  端口
-S  socket文件
-e  免交互执行命令
<   导入SQL脚本

远程登录
[root@mysql-node1 ~]# mysql -uroot -p -h10.0.0.201 -P3306
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> select @@socket ;
+-----------------+
| @@socket        |
+-----------------+
| /tmp/mysql.sock |
+-----------------+
1 row in set (0.00 sec)

mysql> exit ;
Bye

本地登录
[root@mysql-node1 ~]# mysql -uroot -p -S /tmp/mysql.sock
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> exit ;
Bye

mysql -uroot -p 默认走的是socket

[root@mysql-node1 ~]# mysql -uroot -p -h10.0.0.201 -P3306 -S /tmp/mysql.sock  # 走的是远程
Enter password:

mysql> show processlist ;
+----+------+-------------------+------+---------+------+----------+------------------+
| Id | User | Host              | db   | Command | Time | State    | Info             |
+----+------+-------------------+------+---------+------+----------+------------------+
|  8 | root | mysql-node1:39084 | NULL | Query   |    0 | starting | show processlist |
+----+------+-------------------+------+---------+------+----------+------------------+
1 row in set (0.00 sec)

[root@mysql-node1 ~]# mysql -uroot -p
Enter password:

mysql> show processlist ;
+----+------+-------------------+------+---------+------+----------+------------------+
| Id | User | Host              | db   | Command | Time | State    | Info             |
+----+------+-------------------+------+---------+------+----------+------------------+
|  8 | root | mysql-node1:39084 | NULL | Query   |    0 | starting | show processlist |
|  9 | root | localhost         | NULL | Sleep   |    4 |          | NULL             |
+----+------+-------------------+------+---------+------+----------+------------------+
2 rows in set (0.00 sec)

[root@mysql-node1 ~]# mysql -uroot -p -e "select user,host from mysql.user;"
Enter password:
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | 10.0.0.%  |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+

[root@mysql-node1 ~]# rz -y
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring world.sql...
  100%     388 KB     388 KB/sec    00:00:01       0 Errors

[root@mysql-node1 ~]# ls -l world.sql
-rw-r--r-- 1 root root 397334 Apr  4 15:14 world.sql

导入SQL脚本
[root@mysql-node1 ~]# mysql -uroot -p < world.sql
Enter password:

[root@mysql-node1 ~]# mysql -uroot -p -e "show databases;"
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| world              |
+--------------------+

[root@mysql-node1 ~]# mysql -uroot -p -e "show tables from world;"
Enter password:
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
+-----------------+


2、MySQL连接工具-SQLyog和Navicat
这两个工具自己点八点吧


3、MySQL启动方式
[root@mysql-node1 ~]# cd /app/mysql/support-files/
[root@mysql-node1 /app/mysql/support-files]# ls -l mysql.server
-rwxr-xr-x 1 mysql mysql 10576 Sep 14  2017 mysql.server


[root@mysql-node1 ~]# cd /app/mysql/bin/
[root@mysql-node1 /app/mysql/bin]# ls -l mysqld_safe
-rwxr-xr-x 1 mysql mysql 28494 Sep 14  2017 mysqld_safe

[root@mysql-node1 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!

[root@mysql-node1 ~]# mysqld_safe
2019-06-01T05:06:46.172948Z mysqld_safe Logging to '/data/mysql/mysql-node1.err'.
2019-06-01T05:06:46.210137Z mysqld_safe Starting mysqld daemon with databases from /data/mysql
^Z
[1]+  Stopped                 mysqld_safe

[root@mysql-node1 ~]# ss -tunlp | grep mysqld
tcp    LISTEN     0      80       :::3306                 :::*                   users:(("mysqld",pid=9768,fd=16))

[root@mysql-node1 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!

[root@mysql-node1 ~]# mysqld_safe &
[2] 9821
[root@mysql-node1 ~]# 2019-06-01T05:08:16.932459Z mysqld_safe Logging to '/data/mysql/mysql-node1.err'.
2019-06-01T05:08:16.977111Z mysqld_safe Starting mysqld daemon with databases from /data/mysql

[root@mysql-node1 ~]#

[root@mysql-node1 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> exit ;
Bye
[root@mysql-node1 ~]#

[root@mysql-node1 ~]# ps -ef | grep mysqld
root      9627  8031  0 13:06 pts/0    00:00:00 /bin/sh /app/mysql/bin/mysqld_safe
mysql     9768  9627  0 13:06 pts/0    00:00:00 [mysqld] <defunct>
root      9821  8031  0 13:08 pts/0    00:00:00 /bin/sh /app/mysql/bin/mysqld_safe
mysql     9962  9821  0 13:08 pts/0    00:00:00 /app/mysql/bin/mysqld --basedir=/app/mysql --datadir=/data/mysql --plugin-dir=/app/mysql/lib/plugin --user=mysql --log-error=mysql-node1.err --pid-file=mysql-node1.pid --socket=/tmp/mysql.sock --port=3306
root      9996  8031  0 13:09 pts/0    00:00:00 grep --color=auto mysqld


[root@mysql-node1 ~]# /etc/init.d/mysqld stop
Shutting down MySQL..2019-06-01T05:11:33.403598Z mysqld_safe mysqld from pid file /data/mysql/mysql-node1.pid ended
 SUCCESS!
[2]-  Done                    mysqld_safe
[root@mysql-node1 ~]# mysqld &
[2] 10030
[root@mysql-node1 ~]# 2019-06-01T05:11:47.429588Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-01T05:11:47.429678Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2019-06-01T05:11:47.429702Z 0 [Note] mysqld (mysqld 5.7.20) starting as process 10030 ...
2019-06-01T05:11:47.434190Z 0 [Note] InnoDB: PUNCH HOLE support available
2019-06-01T05:11:47.434214Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-06-01T05:11:47.434219Z 0 [Note] InnoDB: Uses event mutexes
2019-06-01T05:11:47.434222Z 0 [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
2019-06-01T05:11:47.434226Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2019-06-01T05:11:47.434229Z 0 [Note] InnoDB: Using Linux native AIO
2019-06-01T05:11:47.434618Z 0 [Note] InnoDB: Number of pools: 1
2019-06-01T05:11:47.434709Z 0 [Note] InnoDB: Using CPU crc32 instructions
2019-06-01T05:11:47.436093Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2019-06-01T05:11:47.443029Z 0 [Note] InnoDB: Completed initialization of buffer pool
2019-06-01T05:11:47.444830Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2019-06-01T05:11:47.457782Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2019-06-01T05:11:47.468611Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2019-06-01T05:11:47.468694Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2019-06-01T05:11:47.504091Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2019-06-01T05:11:47.505672Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2019-06-01T05:11:47.505699Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2019-06-01T05:11:47.506843Z 0 [Note] InnoDB: Waiting for purge to start
2019-06-01T05:11:47.557708Z 0 [Note] InnoDB: 5.7.20 started; log sequence number 3498147
2019-06-01T05:11:47.558860Z 0 [Note] Plugin 'FEDERATED' is disabled.
2019-06-01T05:11:47.566963Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
2019-06-01T05:11:47.567003Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2019-06-01T05:11:47.567072Z 0 [Note] IPv6 is available.
2019-06-01T05:11:47.567089Z 0 [Note]   - '::' resolves to '::';
2019-06-01T05:11:47.567115Z 0 [Note] Server socket created on IP: '::'.
2019-06-01T05:11:47.579677Z 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql/ib_buffer_pool
2019-06-01T05:11:47.581069Z 0 [Note] InnoDB: Buffer pool(s) load completed at 190601 13:11:47
2019-06-01T05:11:47.593310Z 0 [Note] Event Scheduler: Loaded 0 events
2019-06-01T05:11:47.594073Z 0 [Note] mysqld: ready for connections.
Version: '5.7.20'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)
2019-06-01T05:11:47.594100Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check.
2019-06-01T05:11:47.594108Z 0 [Note] Beginning of list of non-natively partitioned tables
2019-06-01T05:11:47.613943Z 0 [Note] End of list of non-natively partitioned tables

[root@mysql-node1 ~]# netstat -tunlp | grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      10030/mysqld
[root@mysql-node1 ~]#

[root@mysql-node1 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> exit ;
Bye
[root@mysql-node1 ~]#

[root@mysql-node1 ~]# pkill mysqld
[root@mysql-node1 ~]# pkill mysqld
[root@mysql-node1 ~]# pkill mysqld

以上多种方式,都可以单独启动MySQL服务
mysqld_safe和mysqld一般是在临时维护时使用。
另外,从CentOS7系统开始,支持systemd直接调用mysqld的方式进行启动数据库。


4、MySQL数据库root密码忘记了?
[root@mysql-node1 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

[root@mysql-node1 ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

跳过授权表登录数据库,不让它验证登录
把验证模块给关掉

--skip-grant-tables : 连接层关闭验证模块,所有验证表不加载。
--skip-networking   : 连接层关闭tcp/ip协议,禁止远程访问。

第一步:挂维护页.html
第二步:关数据库。
第三步:跳过授权表。
[root@mysql-node1 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!

[root@mysql-node1 ~]# mysqld_safe --skip-grant-tables --skip-networking &
[2] 10317
[root@mysql-node1 ~]# 2019-06-01T05:29:02.130528Z mysqld_safe Logging to '/data/mysql/mysql-node1.err'.
2019-06-01T05:29:02.164585Z mysqld_safe Starting mysqld daemon with databases from /data/mysql
[root@mysql-node1 ~]#

[root@mysql-node1 ~]# netstat -tunlp | grep mysql
[root@mysql-node1 ~]# ss -tunlp | grep mysql
[root@mysql-node1 ~]# ps -ef | grep mysql
root      9627  8031  0 13:06 pts/0    00:00:00 /bin/sh /app/mysql/bin/mysqld_safe
mysql     9768  9627  0 13:06 pts/0    00:00:00 [mysqld] <defunct>
root     10317  8031  0 13:29 pts/0    00:00:00 /bin/sh /app/mysql/bin/mysqld_safe --skip-grant-tables --skip-networking
mysql    10488 10317  0 13:29 pts/0    00:00:00 /app/mysql/bin/mysqld --basedir=/app/mysql --datadir=/data/mysql --plugin-dir=/app/mysql/lib/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=mysql-node1.err --pid-file=mysql-node1.pid --socket=/tmp/mysql.sock --port=3306
root     10532  8031  0 13:31 pts/0    00:00:00 grep --color=auto mysql

[root@mysql-node1 ~]# ls -l /tmp/
total 4
srwxrwxrwx 1 mysql mysql 0 Jun  1 13:29 mysql.sock
-rw------- 1 mysql mysql 6 Jun  1 13:29 mysql.sock.lock

密码为空或者随便输入都能登录数据库了这次
[root@mysql-node1 ~]# mysql -uroot -p123456
[root@mysql-node1 ~]# mysql -uroot -p
[root@mysql-node1 ~]# mysql
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>

但是远程不能登录数据库(没有端口)
[root@mysql-node1 ~]# mysql -uroot -p -h 10.0.0.201 -P3306
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '10.0.0.201' (111)
[root@mysql-node1 ~]# mysql -uroot -p123456 -h 10.0.0.201 -P3306
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on '10.0.0.201' (111)

修改密码
[root@mysql-node1 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> alter user root@'localhost' identified by '456' ;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
不能正常去修改密码了,因为你的授权表根本就没有加载到内存,加载不了。

mysql> flush privileges ;
Query OK, 0 rows affected (0.00 sec)

mysql> alter user root@'localhost' identified by '456' ;
Query OK, 0 rows affected (0.00 sec)
哎!先把授权表给加载一下,再来改密码,就可以了。

mysql> exit ;
Bye
[root@mysql-node1 ~]# pkill mysqld
[root@mysql-node1 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

[root@mysql-node1 ~]# mysql -uroot -p123
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

[root@mysql-node1 ~]# mysql -uroot -p456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> exit ;
Bye
[root@mysql-node1 ~]#


5、初始化配置方法
预编译
命令行
配置文件

初始化配置优先级:命令行 > 配置文件 > 预编译

我们验证一下
[root@mysql-node1 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!

[root@mysql-node1 ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/app/mysql
datadir=/data/mysql
server_id=1
port=3306
socket=/tmp/mysql.sock

[mysql]
socket=/tmp/mysql.sock

[root@mysql-node1 ~]# mysqld_safe --socket=/data/mysql/mysql.sock &

[root@mysql-node1 ~]# ls -l /tmp/
total 0
[root@mysql-node1 ~]# ls -l /data/mysql/mysql.*
srwxrwxrwx 1 mysql mysql 0 Jun  1 14:10 /data/mysql/mysql.sock
-rw------- 1 mysql mysql 6 Jun  1 14:10 /data/mysql/mysql.sock.lock
[root@mysql-node1 ~]# pkill mysqld


6、初始化配置文件
初始化配置文件的默认读取路径/etc/my.cnf

查看帮助结合grep命令
[root@mysql-node1 ~]# mysqld --verbose --help
[root@mysql-node1 ~]# mysqld --verbose --help | grep my.cnf
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
他们的优先级是什么呢?后者覆盖前者。
默认情况下,MySQL启动时,会依次读取以上配置文件,如果有重复选项,会以最后一个设置的为准。
但是,如果启动时加入了--defaults-file=xxx时,以上所有的文件都不会读取了!
除非你明确告诉他,默认位置不要读这些文件了,读你自己定制的配置文件路径。
验证测试一下
[root@mysql-node1 ~]# vim ~/.my.cnf
[mysqld]
socket=/data/mysql/mysql.sock

[root@mysql-node1 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@mysql-node1 ~]# ls -l /tmp/
total 0
[root@mysql-node1 ~]# ls -l /data/mysql/mysql.*
srwxrwxrwx 1 mysql mysql 0 Jun  1 14:32 /data/mysql/mysql.sock
-rw------- 1 mysql mysql 6 Jun  1 14:32 /data/mysql/mysql.sock.lock

[root@mysql-node1 ~]# cat /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/app/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000


[root@mysql-node1 ~]# cp /etc/my.cnf my.txt
[root@mysql-node1 ~]# vim my.txt
[mysqld]
user=mysql
basedir=/app/mysql
datadir=/data/mysql
server_id=1
port=3306
socket=/tmp/my.sock

[root@mysql-node1 ~]# pkill mysqld
[root@mysql-node1 ~]# mysqld_safe --defaults-file=/root/my.txt &
[2] 12254
[root@mysql-node1 ~]# 2019-06-01T06:44:48.766911Z mysqld_safe Logging to '/data/mysql/mysql-node1.err'.
2019-06-01T06:44:48.803444Z mysqld_safe Starting mysqld daemon with databases from /data/mysql
[root@mysql-node1 ~]#
[root@mysql-node1 ~]# ls -l /tmp/
total 4
srwxrwxrwx 1 mysql mysql 0 Jun  1 14:44 my.sock
-rw------- 1 mysql mysql 6 Jun  1 14:44 my.sock.lock

[root@mysql-node1 ~]# rm -rf my.txt
[root@mysql-node1 ~]# rm -rf .my.cnf
[root@mysql-node1 ~]# pkill mysqld
[root@mysql-node1 ~]# pkill mysqld
[root@mysql-node1 ~]# pkill mysqld


初始化配置文件的书写方式:
[标签]
配置项=xxxxxx

标签类型:服务端的、客户端的

服务端标签:
[mysqld]
[mysqld_safe]
[server]

客户端标签:
[mysql]
[mysqldump]
[client]


[root@mysql-node1 ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/app/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
server_id=6
port=3306
log_error=/data/mysql/mysql.log

[mysql]
user=root
password=456
socket=/tmp/mysql.sock
prompt=MySQL [\\d]> \


[root@mysql-node1 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql.log'.
 SUCCESS!

[root@mysql-node1 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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 [(none)]> select user() ;
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

MySQL [(none)]> exit ;
Bye
[root@mysql-node1 ~]#


7、MySQL多实例
[root@mysql-node1 ~]# mkdir -p /data/330{7,8,9}/data
[root@mysql-node1 ~]# ls -l /data/
total 0
drwxr-xr-x 3 root  root   18 Jun  1 15:51 3307
drwxr-xr-x 3 root  root   18 Jun  1 15:51 3308
drwxr-xr-x 3 root  root   18 Jun  1 15:51 3309
drwxr-x--- 6 mysql mysql 237 Jun  1 15:20 mysql


[root@mysql-node1 ~]# cat > /data/3307/my.cnf <<EOF
[mysqld]
basedir=/app/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
log_error=/data/3307/mysql.log
port=3307
server_id=7
log_bin=/data/3307/mysql-bin
EOF

[root@mysql-node1 ~]# cat > /data/3308/my.cnf <<EOF
[mysqld]
basedir=/app/mysql
datadir=/data/3308/data
socket=/data/3308/mysql.sock
log_error=/data/3308/mysql.log
port=3308
server_id=8
log_bin=/data/3308/mysql-bin
EOF

[root@mysql-node1 ~]# cat > /data/3309/my.cnf <<EOF
[mysqld]
basedir=/app/mysql
datadir=/data/3309/data
socket=/data/3309/mysql.sock
log_error=/data/3309/mysql.log
port=3309
server_id=9
log_bin=/data/3309/mysql-bin
EOF


[root@mysql-node1 ~]# mv /etc/my.cnf /etc/my.cnf.bak
[root@mysql-node1 ~]# mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/3307/data
[root@mysql-node1 ~]# mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/3308/data
[root@mysql-node1 ~]# mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/3309/data


[root@mysql-node1 ~]# cp /etc/systemd/system/mysqld.service /etc/systemd/system/mysqld3307.service
[root@mysql-node1 ~]# cp /etc/systemd/system/mysqld.service /etc/systemd/system/mysqld3308.service
[root@mysql-node1 ~]# cp /etc/systemd/system/mysqld.service /etc/systemd/system/mysqld3309.service

[root@mysql-node1 ~]# vim /etc/systemd/system/mysqld3307.service
ExecStart=/app/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf

[root@mysql-node1 ~]# vim /etc/systemd/system/mysqld3308.service
ExecStart=/app/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf

[root@mysql-node1 ~]# vim /etc/systemd/system/mysqld3309.service
ExecStart=/app/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf


[root@mysql-node1 ~]# chown -R mysql:mysql /data
[root@mysql-node1 ~]# ll -d /data
drwxr-xr-x. 6 mysql mysql 55 Jun  1 15:51 /data


[root@mysql-node1 ~]# systemctl enable mysqld3307.service
[root@mysql-node1 ~]# systemctl enable mysqld3308.service
[root@mysql-node1 ~]# systemctl enable mysqld3309.service

[root@mysql-node1 ~]# systemctl start mysqld3307.service
[root@mysql-node1 ~]# systemctl start mysqld3308.service
[root@mysql-node1 ~]# systemctl start mysqld3309.service


[root@mysql-node1 ~]# netstat -tunlp | grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      5975/mysqld
tcp6       0      0 :::3307                 :::*                    LISTEN      6382/mysqld
tcp6       0      0 :::3308                 :::*                    LISTEN      6417/mysqld
tcp6       0      0 :::3309                 :::*                    LISTEN      6452/mysqld


mysql -S /data/3307/mysql.sock -e "show variables like 'server_id';"
mysql -S /data/3308/mysql.sock -e "show variables like 'server_id';"
mysql -S /data/3309/mysql.sock -e "show variables like 'server_id';"


[root@mysql-node1 ~]# mysql -S /data/3307/mysql.sock -e "show variables like 'server_id';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 7     |
+---------------+-------+
[root@mysql-node1 ~]# mysql -S /data/3308/mysql.sock -e "show variables like 'server_id';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 8     |
+---------------+-------+
[root@mysql-node1 ~]# mysql -S /data/3309/mysql.sock -e "show variables like 'server_id';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 9     |
+---------------+-------+

[root@mysql-node1 ~]# mv /etc/my.cnf.bak /etc/my.cnf


8、数据库启动不了
(1).先看日志
(2).看配置文件
(3).数据问题
(4).终极大招mysqld --defaults-file=xxxxxx -user=mysql &

猜你喜欢

转载自www.cnblogs.com/zhouwanchun/p/10960592.html