MySql初始化

被弃用使用--initialize  初始化的时候指定的数据目录要保证是空的不然会报错


[root@master ~]# mysql_install_db
2022-02-26 12:58:35 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2022-02-26 12:58:35 [ERROR] The data directory needs to be specified.





[root@master yum.repos.d]# mysqld --initialize --datadir=/var/lib/mysql --user=mysql
2022-02-26T04:37:30.183514Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-02-26T04:37:30.184840Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2022-02-26T04:37:30.184861Z 0 [ERROR] Aborting





[root@master yum.repos.d]# mysqld --initialize --datadir=/var/lib/mysql --user=mysql
[root@master yum.repos.d]#
[root@master yum.repos.d]#
[root@master yum.repos.d]#
[root@master yum.repos.d]#
[root@master yum.repos.d]#
[root@master yum.repos.d]#
[root@master yum.repos.d]#
[root@master yum.repos.d]# cat /var/log/mysqld.log
2022-02-26T03:10:53.361354Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-02-26T03:10:53.662363Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-02-26T03:10:53.721663Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-02-26T03:10:53.794756Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b55f613f-96b1-11ec-a7ba-525400487d4b.
2022-02-26T03:10:53.799435Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-02-26T03:10:54.646591Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-02-26T03:10:54.646605Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-02-26T03:10:54.647190Z 0 [Warning] CA certificate ca.pem is self signed.
2022-02-26T03:10:54.802256Z 1 [Note] A temporary password is generated for root@localhost: puRdPD8Xx-Um
2022-02-26T03:10:58.884539Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-02-26T03:10:58.886313Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.37) starting as process 10333 ...
2022-02-26T03:10:58.889287Z 0 [Note] InnoDB: PUNCH HOLE support available
2022-02-26T03:10:58.889315Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2022-02-26T03:10:58.889319Z 0 [Note] InnoDB: Uses event mutexes
2022-02-26T03:10:58.889323Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2022-02-26T03:10:58.889332Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2022-02-26T03:10:58.889335Z 0 [Note] InnoDB: Using Linux native AIO
2022-02-26T03:10:58.889592Z 0 [Note] InnoDB: Number of pools: 1
2022-02-26T03:10:58.889701Z 0 [Note] InnoDB: Using CPU crc32 instructions
2022-02-26T03:10:58.891228Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2022-02-26T03:10:58.898628Z 0 [Note] InnoDB: Completed initialization of buffer pool
2022-02-26T03:10:58.900715Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2022-02-26T03:10:58.912412Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2022-02-26T03:10:58.922018Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2022-02-26T03:10:58.922066Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2022-02-26T03:10:58.952515Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2022-02-26T03:10:58.953164Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2022-02-26T03:10:58.953176Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2022-02-26T03:10:58.953934Z 0 [Note] InnoDB: Waiting for purge to start
2022-02-26T03:10:59.004085Z 0 [Note] InnoDB: 5.7.37 started; log sequence number 2750122
2022-02-26T03:10:59.004585Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2022-02-26T03:10:59.004883Z 0 [Note] Plugin 'FEDERATED' is disabled.
2022-02-26T03:10:59.006413Z 0 [Note] InnoDB: Buffer pool(s) load completed at 220226 11:10:59
2022-02-26T03:10:59.011032Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2022-02-26T03:10:59.011045Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2022-02-26T03:10:59.011050Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-02-26T03:10:59.011053Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-02-26T03:10:59.011702Z 0 [Warning] CA certificate ca.pem is self signed.
2022-02-26T03:10:59.011743Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2022-02-26T03:10:59.012140Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2022-02-26T03:10:59.012175Z 0 [Note] IPv6 is available.
2022-02-26T03:10:59.012183Z 0 [Note] - '::' resolves to '::';
2022-02-26T03:10:59.012200Z 0 [Note] Server socket created on IP: '::'.
2022-02-26T03:10:59.026300Z 0 [Note] Event Scheduler: Loaded 0 events
2022-02-26T03:10:59.026436Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.37' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
2022-02-26T04:29:08.794471Z 0 [Note] Giving 0 client threads a chance to die gracefully
2022-02-26T04:29:08.794489Z 0 [Note] Shutting down slave threads
2022-02-26T04:29:08.794499Z 0 [Note] Forcefully disconnecting 0 remaining clients
2022-02-26T04:29:08.794507Z 0 [Note] Event Scheduler: Purging the queue. 0 events
2022-02-26T04:29:08.794935Z 0 [Note] Binlog end
2022-02-26T04:29:08.795394Z 0 [Note] Shutting down plugin 'validate_password'
2022-02-26T04:29:08.795410Z 0 [Note] Shutting down plugin 'ngram'
2022-02-26T04:29:08.795414Z 0 [Note] Shutting down plugin 'partition'
2022-02-26T04:29:08.795417Z 0 [Note] Shutting down plugin 'BLACKHOLE'
2022-02-26T04:29:08.795422Z 0 [Note] Shutting down plugin 'ARCHIVE'
2022-02-26T04:29:08.795426Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2022-02-26T04:29:08.795454Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
2022-02-26T04:29:08.795457Z 0 [Note] Shutting down plugin 'MyISAM'
2022-02-26T04:29:08.795464Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
2022-02-26T04:29:08.795468Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2022-02-26T04:29:08.795471Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2022-02-26T04:29:08.795474Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2022-02-26T04:29:08.795477Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2022-02-26T04:29:08.795480Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2022-02-26T04:29:08.795483Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2022-02-26T04:29:08.795486Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2022-02-26T04:29:08.795489Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2022-02-26T04:29:08.795492Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2022-02-26T04:29:08.795495Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2022-02-26T04:29:08.795497Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2022-02-26T04:29:08.795501Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2022-02-26T04:29:08.795504Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2022-02-26T04:29:08.795507Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2022-02-26T04:29:08.795509Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2022-02-26T04:29:08.795512Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
2022-02-26T04:29:08.795515Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
2022-02-26T04:29:08.795518Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2022-02-26T04:29:08.795521Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2022-02-26T04:29:08.795525Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2022-02-26T04:29:08.795527Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2022-02-26T04:29:08.795530Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2022-02-26T04:29:08.795534Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2022-02-26T04:29:08.795536Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
2022-02-26T04:29:08.795539Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2022-02-26T04:29:08.795542Z 0 [Note] Shutting down plugin 'INNODB_CMP'
2022-02-26T04:29:08.795545Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2022-02-26T04:29:08.795548Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
2022-02-26T04:29:08.795551Z 0 [Note] Shutting down plugin 'INNODB_TRX'
2022-02-26T04:29:08.795554Z 0 [Note] Shutting down plugin 'InnoDB'
2022-02-26T04:29:08.795611Z 0 [Note] InnoDB: FTS optimize thread exiting.
2022-02-26T04:29:08.795871Z 0 [Note] InnoDB: Starting shutdown...
2022-02-26T04:29:08.896307Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
2022-02-26T04:29:08.896533Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 220226 12:29:08
2022-02-26T04:29:10.207553Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2750150
2022-02-26T04:29:10.209191Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2022-02-26T04:29:10.209208Z 0 [Note] Shutting down plugin 'MEMORY'
2022-02-26T04:29:10.209213Z 0 [Note] Shutting down plugin 'CSV'
2022-02-26T04:29:10.209217Z 0 [Note] Shutting down plugin 'sha256_password'
2022-02-26T04:29:10.209220Z 0 [Note] Shutting down plugin 'mysql_native_password'
2022-02-26T04:29:10.209384Z 0 [Note] Shutting down plugin 'binlog'
2022-02-26T04:29:10.209783Z 0 [Note] /usr/sbin/mysqld: Shutdown complete

2022-02-26T04:37:23.615785Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-02-26T04:37:23.617597Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.37) starting as process 19934 ...
2022-02-26T04:37:23.620516Z 0 [Note] InnoDB: PUNCH HOLE support available
2022-02-26T04:37:23.620545Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2022-02-26T04:37:23.620549Z 0 [Note] InnoDB: Uses event mutexes
2022-02-26T04:37:23.620553Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2022-02-26T04:37:23.620558Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2022-02-26T04:37:23.620564Z 0 [Note] InnoDB: Using Linux native AIO
2022-02-26T04:37:23.620852Z 0 [Note] InnoDB: Number of pools: 1
2022-02-26T04:37:23.620966Z 0 [Note] InnoDB: Using CPU crc32 instructions
2022-02-26T04:37:23.622458Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2022-02-26T04:37:23.629861Z 0 [Note] InnoDB: Completed initialization of buffer pool
2022-02-26T04:37:23.632012Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2022-02-26T04:37:23.643769Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2022-02-26T04:37:23.660462Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2022-02-26T04:37:23.660533Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2022-02-26T04:37:23.690976Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2022-02-26T04:37:23.691640Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2022-02-26T04:37:23.691653Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2022-02-26T04:37:23.692340Z 0 [Note] InnoDB: Waiting for purge to start
2022-02-26T04:37:23.742534Z 0 [Note] InnoDB: 5.7.37 started; log sequence number 2750150
2022-02-26T04:37:23.743397Z 0 [Note] Plugin 'FEDERATED' is disabled.
2022-02-26T04:37:23.745626Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2022-02-26T04:37:23.747611Z 0 [Note] InnoDB: Buffer pool(s) load completed at 220226 12:37:23
2022-02-26T04:37:23.749982Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2022-02-26T04:37:23.749996Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2022-02-26T04:37:23.750001Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-02-26T04:37:23.750004Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-02-26T04:37:23.750656Z 0 [Warning] CA certificate ca.pem is self signed.
2022-02-26T04:37:23.750697Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2022-02-26T04:37:23.751087Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2022-02-26T04:37:23.751121Z 0 [Note] IPv6 is available.
2022-02-26T04:37:23.751130Z 0 [Note] - '::' resolves to '::';
2022-02-26T04:37:23.751148Z 0 [Note] Server socket created on IP: '::'.
2022-02-26T04:37:23.760006Z 0 [Note] Event Scheduler: Loaded 0 events
2022-02-26T04:37:23.760148Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.37' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
2022-02-26T04:38:11.658367Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-02-26T04:38:11.956808Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-02-26T04:38:12.026900Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-02-26T04:38:12.096623Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e7a4cff5-96bd-11ec-9bf3-525400487d4b.
2022-02-26T04:38:12.101504Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-02-26T04:38:12.784312Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-02-26T04:38:12.784324Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-02-26T04:38:12.784915Z 0 [Warning] CA certificate ca.pem is self signed.
2022-02-26T04:38:13.251775Z 1 [Note] A temporary password is generated for root@localhost: pV5I2VUXQg-(




[root@master ~]# 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.37

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> select version();
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> alter user user() identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.37 |
+-----------+
1 row in set (0.00 sec)


mysql> show variables like 'default_%';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| default_authentication_plugin | mysql_native_password |
| default_password_lifetime | 0 |
| default_storage_engine | InnoDB |
| default_tmp_storage_engine | InnoDB |
| default_week_format | 0 |
+-------------------------------+-----------------------+
5 rows in set (0.00 sec)



如果想用远程工具链接记得防火墙开放3306端口,然后授权

grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;

测试环境可以上面用法如果是生产的话尽量单独建立用户然后把想给那个库单独授权database name.* 不要*.*,机器部分的%就写本机ip就行。